stimulus_reflex_testing 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b56ef411f6fb1022d9bdf542269eef6b3126e97366c5fbdea25105768a32718b
4
- data.tar.gz: 41fe7e27a5c7278b14180780d436a490493dd856862804e5973cd187c39d60fa
3
+ metadata.gz: ced61b97b3236cd9b8a700e22d4e2dd4f10d2f73dc22fdc2caa330b28ea3902e
4
+ data.tar.gz: 7a76197b1367354a4ebe5744845617102f4250269a7949f01909a9551340a1b6
5
5
  SHA512:
6
- metadata.gz: 2067450d8b6acdfa738b52254f0d68af93c4beffba9cbf43f4696500b1dd1491432a91af13f5fb2abf01b0d36849253f704113e3287871c59e7e256d97b5656d
7
- data.tar.gz: a047b2f4ba93d0c34584eafba3f68bb11bf8b1608665a6d40e444c02253eb7b77f41a240a06f3758809e8691d36dc9063d234a117f17724d31666e02940d9b30
6
+ metadata.gz: e6843170c7b98f8164971420c40e2b02cda726811e3eb268bcb29570aa597cbf3951a0c56a0b44453ba40ba3bf6fdaf9167fd738afebc63b55d4d78dadbca160
7
+ data.tar.gz: 435de1ea895aebc7e8cc7a859a2023c61aa26cb1a932b98e2bced24c418546c6db0ff2cfa7e29e35002ec52e2e1c18d57e7ee3af83e6e3f472dc3556324bb030
data/README.md CHANGED
@@ -18,7 +18,7 @@ Things we'd like to add/improve on:
18
18
  Add this line to your application's Gemfile:
19
19
 
20
20
  ```ruby
21
- gem 'stimulus_reflex_testing', require: false
21
+ gem 'stimulus_reflex_testing'
22
22
  ```
23
23
 
24
24
  ### Using 5.2 or below? Or using a version of RSpec Rails lower than 4?
@@ -26,7 +26,7 @@ gem 'stimulus_reflex_testing', require: false
26
26
  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.
27
27
 
28
28
  ```ruby
29
- gem 'stimulus_reflex_testing', require: false
29
+ gem 'stimulus_reflex_testing'
30
30
  gem 'action-cable-testing'
31
31
  ```
32
32
 
@@ -36,12 +36,6 @@ And then execute:
36
36
 
37
37
  ### RSpec instructions
38
38
 
39
- Require the library into your `rails_helper`:
40
-
41
- ```ruby
42
- require 'stimulus_reflex_testing/rspec'
43
- ```
44
-
45
39
  Rspec tests include the reflex testing functionality when `type: :reflex` is provided. If this type is not provided, your Reflex tests won't work.
46
40
 
47
41
  ## Usage
@@ -98,6 +92,108 @@ reflex.run(:find_post)
98
92
  reflex.get(:post) #=> returns the @post instance variable
99
93
  ```
100
94
 
95
+ ## Matchers
96
+
97
+ ### `morph(selector)`
98
+
99
+ You can assert that a "run" reflex morphed as you expected:
100
+
101
+ ```ruby
102
+ # app/reflexes/post_reflex.rb
103
+ class PostReflex < ApplicationReflex
104
+ def delete
105
+ @post = Post.find(params[:id])
106
+ @post.destroy
107
+ morph dom_id(@post), ""
108
+ end
109
+ end
110
+
111
+ # spec/reflexes/post_reflex_spec.rb
112
+ require 'rails_helper'
113
+
114
+ RSpec.describe PostReflex, type: :reflex do
115
+ let(:post) { create(:post) }
116
+ let(:reflex) { build_reflex }
117
+
118
+ describe '#delete' do
119
+ it 'morphs the post' do
120
+ subject = reflex.run(:delete)
121
+ expect(subject).to morph("#post_#{post.id}")
122
+ end
123
+ end
124
+ end
125
+ ```
126
+
127
+ You can also assert the content you provided to morph:
128
+
129
+ ```ruby
130
+ # spec/reflexes/post_reflex_spec.rb
131
+ require 'rails_helper'
132
+
133
+ RSpec.describe PostReflex, type: :reflex do
134
+ let(:post) { create(:post) }
135
+ let(:reflex) { build_reflex }
136
+
137
+ describe '#delete' do
138
+ it 'morphs the post with an empty string' do
139
+ subject = reflex.run(:delete)
140
+ expect(subject).to morph("#post_#{post.id}").with("")
141
+ end
142
+ end
143
+ end
144
+ ```
145
+
146
+ You can also run the expecation as a block:
147
+
148
+ ```ruby
149
+ # spec/reflexes/post_reflex_spec.rb
150
+ require 'rails_helper'
151
+
152
+ RSpec.describe PostReflex, type: :reflex do
153
+ let(:post) { create(:post) }
154
+ let(:reflex) { build_reflex }
155
+
156
+ describe '#delete' do
157
+ it 'morphs the post with an empty string' do
158
+ expect { reflex.run(:delete) }.to morph("#post_#{post.id}").with("")
159
+ end
160
+ end
161
+ end
162
+ ```
163
+
164
+ ### `broadcast(operations)` (Experimental)
165
+
166
+ You can assert that a reflex will perform the CableReady operations you anticipate:
167
+
168
+ ```ruby
169
+ # app/reflexes/post_reflex.rb
170
+ class PostReflex < ApplicationReflex
171
+ def delete
172
+ @post = Post.find(params[:id])
173
+ @post.destroy
174
+ cable_ready[PostsChannel].remove(selector: dom_id(@post)).broadcast
175
+ end
176
+ end
177
+
178
+ # spec/reflexes/post_reflex_spec.rb
179
+ require 'rails_helper'
180
+
181
+ RSpec.describe PostReflex, type: :reflex do
182
+ let(:post) { create(:post) }
183
+ let(:reflex) { build_reflex }
184
+
185
+ describe '#delete' do
186
+ it 'broadcasts the CableReady operations' do
187
+ expect { reflex.run(:delete) }.to broadcast(:remove, :broadcast)
188
+ end
189
+
190
+ it 'removes the post' do
191
+ expect { reflex.run(:delete) }.to broadcast(remove: { selector: "#post_#{post.id}" }, broadcast: nil)
192
+ end
193
+ end
194
+ end
195
+ ```
196
+
101
197
  ## RSpec example
102
198
 
103
199
  ```ruby
@@ -124,11 +220,11 @@ RSpec.describe PostReflex, type: :reflex do
124
220
  subject
125
221
  end
126
222
 
127
- it 'should find the post' do
223
+ it 'finds the post' do
128
224
  expect(reflex.get(:post)).to eq(post)
129
225
  end
130
226
 
131
- it 'should validate the post' do
227
+ it 'validates the post' do
132
228
  expect(reflex.get(:post).errors).to be_present
133
229
  end
134
230
  end
@@ -9,3 +9,82 @@ RSpec::Matchers.define :have_set do |instance_variable, expected_value|
9
9
  "set #{instance_variable} to #{expected_value}, got #{actual.get(instance_variable)}"
10
10
  end
11
11
  end
12
+
13
+ RSpec::Matchers.define :broadcast do |**broadcasts|
14
+ match do |block|
15
+ fable_ready = StimulusReflex::TestReflexPatches::FableReady.new
16
+
17
+ allow_any_instance_of(StimulusReflex::CableReadyChannels).to(
18
+ receive(:[]).and_return(fable_ready)
19
+ )
20
+
21
+ broadcasts.each do |broadcast|
22
+ if broadcast.is_a?(Array)
23
+ if broadcast[1].present?
24
+ expect(fable_ready).to receive(broadcast[0]).with(broadcast[1]).and_return(fable_ready)
25
+ else
26
+ expect(fable_ready).to receive(broadcast[0]).and_return(fable_ready)
27
+ end
28
+ else
29
+ expect(fable_ready).to receive(broadcast).and_return(fable_ready)
30
+ end
31
+ end
32
+
33
+ block.call
34
+
35
+ RSpec::Mocks.verify
36
+ end
37
+
38
+ def supports_block_expectations?
39
+ true
40
+ end
41
+ end
42
+
43
+ RSpec::Matchers.define :morph do |selector|
44
+ match do |morphs|
45
+ if morphs.is_a?(StimulusReflex::NothingBroadcaster)
46
+ return selector.to_s == "nothing"
47
+ end
48
+
49
+ morph = matching_morph(morphs, selector)
50
+
51
+ if morph.present? && @with_chain_called
52
+ @content == morph[1]
53
+ else
54
+ morph
55
+ end
56
+ end
57
+
58
+ description do |morphs|
59
+ morph = matching_morph(morphs, selector)
60
+
61
+ if @with_chain_called
62
+ if !morph
63
+ "morph #{selector} but a morph for that selector was not run"
64
+ else
65
+ "morph #{selector} with #{@content} but the value was: #{morph[1]}"
66
+ end
67
+ else
68
+ "morph #{selector} but a morph for that selector was not run"
69
+ end
70
+ end
71
+
72
+ chain :with do |content|
73
+ @with_chain_called = true
74
+ @content = content
75
+ end
76
+
77
+ def supports_block_expectations?
78
+ true
79
+ end
80
+
81
+ def matching_morph(morphs, selector)
82
+ if morphs.respond_to?(:call)
83
+ morphs.call.find { |morph| morph[0] == selector }
84
+ else
85
+ morphs.find { |morph| morph[0] == selector }
86
+ end
87
+ end
88
+
89
+ diffable
90
+ end
@@ -17,7 +17,7 @@ module StimulusReflex::TestReflexPatches
17
17
  end
18
18
 
19
19
  def method_missing(*)
20
- true || super
20
+ self
21
21
  end
22
22
 
23
23
  def respond_to_missing?(*)
@@ -1,3 +1,3 @@
1
1
  module StimulusReflexTesting
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus_reflex_testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Charnes
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-01 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stimulus_reflex