Skip to content

Commit

Permalink
+ next/rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 30, 2024
1 parent fa3c4da commit f9c1c22
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

## main

## 0.1.1

- Added RSpec patch.

## 0.1.0

- Initial extraction.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ gem "rails", "~> 7.0"
```

Then, you can use Action Cable as before. Under the hood, the new implementation would be used.

### RSpec support

This gem also includes the corresponding patch for RSpec Rails (see [PR](https://github.com/palkan/rspec-rails/pull/1)). You MUST activate it explicitly
by adding the following line to your `spec/rails_helper.rb`:

```ruby
# after rspec-rails is loaded
require "rspec/rails"
require "action_cable/next/rspec"
```
1 change: 1 addition & 0 deletions lib/action_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"#{lib}/action_cable/version.rb",
"#{lib}/action_cable/deprecator.rb",
"#{lib}/actioncable-next.rb",
"#{lib}/action_cable/next",
)

loader.do_not_eager_load(
Expand Down
41 changes: 41 additions & 0 deletions lib/action_cable/next/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# RSpec patches from https://github.com/palkan/rspec-rails/pull/1

require "rspec/rails/example/channel_example_group"

if defined?(RSpec::Rails::ChannelExampleGroup)
RSpec::Rails::ChannelExampleGroup.prepend(Module.new do
def connection_class
(_connection_class || described_class).then do |klass|
# Connection class either specified explicitly or is a described class
next klass if klass && klass <= ::ActionCable::Connection::Base

# Otherwise, fallback to the default connection class
tests_connection ::ActionCable.server.config.connection_class.call

_connection_class
end
end
end)
end

RSpec::Rails::Matchers::ActionCable::HaveStream.prepend(Module.new do
def match(subscription)
case subscription
when ::ActionCable::Channel::Base
@actual = streams_for(subscription)
no_expected? ? actual.any? : actual.any? { |i| expected === i }
else
raise ArgumentError, "have_stream, have_stream_from and have_stream_from support expectations on subscription only"
end
end

private
def streams_for(subscription)
# In Rails 8, subscription.streams returns a real subscriptions hash,
# not a fake array of stream names like in Rails 6-7.
# So, we must use #stream_names instead.
subscription.respond_to?(:stream_names) ? subscription.stream_names : subscription.streams
end
end)
2 changes: 1 addition & 1 deletion lib/actioncable-next.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActionCableNext
VERSION = "0.1.0"
VERSION = "0.1.1"
end

0 comments on commit f9c1c22

Please sign in to comment.