render_turbo_stream 1.1.0 → 1.2.1

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: 8c88aab0f6268663008843f37d2b7cd052f5883d4751c8c1558a16a166e06752
4
- data.tar.gz: 97188c8d6d19998f7a6e617eb07a9de4a92c88b96d917908b738932301a0bc11
3
+ metadata.gz: 9a070060e95c95882e3bb687a37859fca7a46d6e4557695b5b1779fc2a37f9d9
4
+ data.tar.gz: cb01fb80b57bbc96d7418fed0164fff2ac72515ed13a1281672ea9a793e0ba00
5
5
  SHA512:
6
- metadata.gz: cd3b7d91aa83267ff6f6f5da938fbf8e65704396b60cba5ff14ca82f9d15ad88954b330bbe170043514cba4cb67f7971ac57b55062828860e9e13c7129ef91b7
7
- data.tar.gz: 3d7c1b8917846500a637dbb8ccb46b0535db82e4d64690c488d0fe8b98d070b51a88cb7d3c5cd41296c55f9f5d51b2bec66a0dea8fd981e9ab407e273568a93c
6
+ metadata.gz: 6ba050699fcda283665e128c364182ea115c5ba9d18090f2e17082597e182589f91a5a2165e6368fa5e4c6fc0a451d46fa869e3632cae50c9f91fb738a5eb3da
7
+ data.tar.gz: 33cdee02b396b110582ff6804e35702fb8d324148694f3c1b1246688a1ae757296167a22557b7b95703881de7cf0e7aa72ed812a989bc770684c0d58b03d9a85
data/README.md CHANGED
@@ -28,6 +28,7 @@ spec/rails_helper.rb
28
28
  ```ruby
29
29
  RSpec.configure do |config|
30
30
  config.include RenderTurboStream::Test::RequestHelpers, type: :request
31
+ config.include RenderTurboStream::Test::RspecRequestHelpers, type: :request #=> if you are on rspec
31
32
  end
32
33
  ```
33
34
 
@@ -100,19 +101,25 @@ If the request format is not `turbo_stream`, which is the case on request specs,
100
101
  that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look
101
102
  like this:
102
103
 
103
- **The easiest test**
104
+ **The fastest**
104
105
 
105
- If you want to check if a controller action succeeded, just check for `response.status`.
106
+ If you want to check if a controller action succeeded, just check for `response.status`. The `turbo_stream_save` method returns three statuses: `200` if `save_action` is true, otherwise `422` and `302` for redirect. If one of the declared partials does not exist or breaks, the server will respond with exception anyway.
107
+
108
+ **rspec**
109
+
110
+ For rspec there is a special helper, for a successful save action:
106
111
 
107
- The `turbo_stream_save` method returns three statuses: `200` if `save_action` is true, otherwise `422` and `302` for redirect. If one of the declared partials does not exist or breaks, the server will respond with exception anyway. So a test might look like this:
108
112
  ```ruby
109
- it 'update failed' do
110
- patch article_path(article, params: invalid_params)
111
- expect(response.status).to eq(422)
113
+ it 'update failed' do
114
+ patch article_path(article, params: valid_params)
115
+ expect_successful_saved('form', 'flash-box')
116
+ # expects response.status 200
117
+ # Make sure that the responses point to exactly these 2 IDs ('form' and 'flash-box').
118
+ # Make sure that each ID is responded to exactly once.
112
119
  end
113
120
  ```
114
121
 
115
- **Test redirection**
122
+ **Redirection**
116
123
  ```ruby
117
124
  it 'update success' do
118
125
  patch article_path(article, params: valid_params)
@@ -134,7 +141,7 @@ expect(partial_response('articles/form', id: 'form', css: '.field_with_errors',
134
141
  # Check the content inside at least one in any of the elements that match the given css and check the id that turbo has pointed to..
135
142
  ```
136
143
 
137
- **More detailed test**
144
+ **More detailed**
138
145
 
139
146
  Consider a controller action that should respond in 2 flashes.
140
147
 
@@ -155,6 +162,9 @@ expect(
155
162
  end
156
163
  ).to eq(1)
157
164
  ```
165
+ `partial_response_count` always returns the number of matched responses.
166
+
167
+ Possible matches can be found at [Nokogiri](https://nokogiri.org/tutorials/searching_a_xml_html_document.html)
158
168
 
159
169
  P.S.:
160
170
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  <% streams.each do |s| %>
4
4
 
5
- <% html = (render s[:partial], locals: s[:locals].symbolize_keys, formats: [:html]) %>
5
+ <% html = (render s[:partial], locals: s[:locals]&.symbolize_keys, formats: [:html]) %>
6
6
 
7
7
  <% rendered_partials.push({ html_response: html }.merge(s)) %>
8
8
 
@@ -17,21 +17,21 @@
17
17
  <% ctl[:action] = :replace %>
18
18
  <% Rails.logger.debug(" • render-turbo-stream REPLACE => #{info}") %>
19
19
  <%= turbo_stream.replace s[:id] do %>
20
- <%= render s[:partial], locals: s[:locals].symbolize_keys %>
20
+ <%= render s[:partial], locals: s[:locals]&.symbolize_keys %>
21
21
  <% end %>
22
22
 
23
23
  <% elsif s[:action].to_sym == :prepend %>
24
24
  <% ctl[:action] = :prepend %>
25
25
  <% Rails.logger.debug(" • render-turbo-stream PREPEND => #{info}") %>
26
26
  <%= turbo_stream.prepend s[:id] do %>
27
- <%= render s[:partial], locals: s[:locals].symbolize_keys %>
27
+ <%= render s[:partial], locals: s[:locals]&.symbolize_keys %>
28
28
  <% end %>
29
29
 
30
30
  <% elsif s[:action].to_sym == :append %>
31
31
  <% ctl[:action] = :append %>
32
32
  <% Rails.logger.debug(" • render-turbo-stream APPEND => #{info}") %>
33
33
  <%= turbo_stream.prepend s[:id] do %>
34
- <%= render s[:partial], locals: s[:locals].symbolize_keys %>
34
+ <%= render s[:partial], locals: s[:locals]&.symbolize_keys %>
35
35
  <% end %>
36
36
 
37
37
  <% else %>
@@ -58,6 +58,9 @@ module RenderTurboStream
58
58
  c == 1
59
59
  end
60
60
 
61
+ def partial_ids
62
+ RenderTurboStream::Libs.partial_ids(response)
63
+ end
61
64
  end
62
65
  end
63
66
  end
@@ -0,0 +1,26 @@
1
+ module RenderTurboStream
2
+ module Test
3
+ module RspecRequestHelpers
4
+
5
+
6
+ # expect status 200 and a each one time response to given ids
7
+
8
+ def expect_successful_saved(*ids)
9
+ responses = RenderTurboStream::Libs.all_responses(response)
10
+ counts = {}
11
+ responses.each do |r|
12
+ counts[r['id']] ||= 0
13
+ counts[r['id']] +=1
14
+ end
15
+
16
+ expect(response.status).to eq(200)
17
+ expect(counts.keys.length).to eq(ids.length)
18
+ ids.each do |id|
19
+ expect(counts.key?(id)).to be_truthy
20
+ expect(counts[id]).to eq(1)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -2,6 +2,7 @@ require "render_turbo_stream/version"
2
2
  require "render_turbo_stream/railtie"
3
3
  require 'render_turbo_stream/engine'
4
4
  require 'render_turbo_stream/test/request_helpers'
5
+ require 'render_turbo_stream/test/rspec_request_helpers'
5
6
 
6
7
  module RenderTurboStream
7
8
 
@@ -233,6 +234,18 @@ module RenderTurboStream
233
234
  part.length
234
235
  end
235
236
 
237
+ def self.partial_ids(response)
238
+ all = all_responses(response)
239
+ ids = []
240
+ all.each do |p|
241
+ _p = p['id']
242
+ unless ids.include?(_p)
243
+ ids.push(_p)
244
+ end
245
+ end
246
+ ids
247
+ end
248
+
236
249
  def self.partial_response_count(response, partial, id, total, &block)
237
250
  responses = select_responses(response, partial, id)
238
251
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_turbo_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
@@ -44,6 +44,7 @@ files:
44
44
  - lib/render_turbo_stream/engine.rb
45
45
  - lib/render_turbo_stream/railtie.rb
46
46
  - lib/render_turbo_stream/test/request_helpers.rb
47
+ - lib/render_turbo_stream/test/rspec_request_helpers.rb
47
48
  - lib/render_turbo_stream/version.rb
48
49
  - lib/tasks/render_turbo_stream_tasks.rake
49
50
  homepage: https://gitlab.com/sedl/renderturbostream
@@ -71,5 +72,5 @@ requirements: []
71
72
  rubygems_version: 3.4.12
72
73
  signing_key:
73
74
  specification_version: 4
74
- summary: Render turbo-stream partials directly from the controller. Test helpers included
75
+ summary: Render turbo-stream partials directly from the controller. Test helpers included.
75
76
  test_files: []