README.md in stimulus_reflex_testing-0.2.2 vs README.md in stimulus_reflex_testing-0.3.0

- old
+ new

@@ -16,34 +16,28 @@ ## Installation Add this line to your application's Gemfile: ```ruby -gem 'stimulus_reflex_testing', require: false +gem 'stimulus_reflex_testing' ``` ### Using 5.2 or below? Or using a version of RSpec Rails lower than 4? Both Rails 6 and RSpec Rails 4 introduce the `action-cable-testing` library. If you're using Rails 5.2 and a version of RSpec Rails lower than 4, include the `action-cable-testing` gem in your Gemfile. ```ruby -gem 'stimulus_reflex_testing', require: false +gem 'stimulus_reflex_testing' gem 'action-cable-testing' ``` And then execute: $ bundle install ### RSpec instructions -Require the library into your `rails_helper`: - -```ruby -require 'stimulus_reflex_testing/rspec' -``` - Rspec tests include the reflex testing functionality when `type: :reflex` is provided. If this type is not provided, your Reflex tests won't work. ## Usage ### `build_reflex` @@ -96,10 +90,112 @@ reflex = build_reflex(url: edit_post_url(post), params: { id: post.id }) reflex.run(:find_post) reflex.get(:post) #=> returns the @post instance variable ``` +## Matchers + +### `morph(selector)` + +You can assert that a "run" reflex morphed as you expected: + +```ruby +# app/reflexes/post_reflex.rb +class PostReflex < ApplicationReflex + def delete + @post = Post.find(params[:id]) + @post.destroy + morph dom_id(@post), "" + end +end + +# spec/reflexes/post_reflex_spec.rb +require 'rails_helper' + +RSpec.describe PostReflex, type: :reflex do + let(:post) { create(:post) } + let(:reflex) { build_reflex } + + describe '#delete' do + it 'morphs the post' do + subject = reflex.run(:delete) + expect(subject).to morph("#post_#{post.id}") + end + end +end +``` + +You can also assert the content you provided to morph: + +```ruby +# spec/reflexes/post_reflex_spec.rb +require 'rails_helper' + +RSpec.describe PostReflex, type: :reflex do + let(:post) { create(:post) } + let(:reflex) { build_reflex } + + describe '#delete' do + it 'morphs the post with an empty string' do + subject = reflex.run(:delete) + expect(subject).to morph("#post_#{post.id}").with("") + end + end +end +``` + +You can also run the expecation as a block: + +```ruby +# spec/reflexes/post_reflex_spec.rb +require 'rails_helper' + +RSpec.describe PostReflex, type: :reflex do + let(:post) { create(:post) } + let(:reflex) { build_reflex } + + describe '#delete' do + it 'morphs the post with an empty string' do + expect { reflex.run(:delete) }.to morph("#post_#{post.id}").with("") + end + end +end +``` + +### `broadcast(operations)` (Experimental) + +You can assert that a reflex will perform the CableReady operations you anticipate: + +```ruby +# app/reflexes/post_reflex.rb +class PostReflex < ApplicationReflex + def delete + @post = Post.find(params[:id]) + @post.destroy + cable_ready[PostsChannel].remove(selector: dom_id(@post)).broadcast + end +end + +# spec/reflexes/post_reflex_spec.rb +require 'rails_helper' + +RSpec.describe PostReflex, type: :reflex do + let(:post) { create(:post) } + let(:reflex) { build_reflex } + + describe '#delete' do + it 'broadcasts the CableReady operations' do + expect { reflex.run(:delete) }.to broadcast(:remove, :broadcast) + end + + it 'removes the post' do + expect { reflex.run(:delete) }.to broadcast(remove: { selector: "#post_#{post.id}" }, broadcast: nil) + end + end +end +``` + ## RSpec example ```ruby # app/reflexes/post_reflex.rb class PostReflex < ApplicationReflex @@ -122,14 +218,14 @@ before do reflex.element.dataset.post_id = post.id subject end - it 'should find the post' do + it 'finds the post' do expect(reflex.get(:post)).to eq(post) end - it 'should validate the post' do + it 'validates the post' do expect(reflex.get(:post).errors).to be_present end end end ```