render_turbo_stream 0.1.46 → 1.2.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: 3efa13a85fa7384096fdd81f4bceb089e73111d07b074ddc579f98e27af2511e
4
- data.tar.gz: 69f47bbda0dfc94d5af491dcce47cd2eb5df464d8e404058483adcd26b480cb9
3
+ metadata.gz: 1299653f6a308796e690ca3929cd4c1580f707604913583e223a4bf4d398f889
4
+ data.tar.gz: d6228eb9aeb0e64591b1a066d1007ab665ab634788f8645ad7124063c9ef2c0e
5
5
  SHA512:
6
- metadata.gz: b20d62b2f1fc7617050300d5960bbec912562b86a498b41652f1956e52459aa3ab63ecde9386ecbe7849eb66f8c6ddb3d139f4a03c79cf8761f8e06c672b0f80
7
- data.tar.gz: 2a8e3d7b5bc35b28e73e00da9c5c526a76bea089129c5c4fa8e8d93e50cb9359417b77c944935636d570c2f783ff7566236c9e7dad74e7ae0422b236ee51cfb1
6
+ metadata.gz: d660266f923b6e6efa8225162b9eab8f0eb56eb0ed9f289601106bc661abd74f2dd6832021133c5b9031314475c657c27a1d34388e2c063fbb84a65cb4680b40
7
+ data.tar.gz: c511dfd6b6799a9075ab594aa60d806c428cf63abfb42cc6f4cbcdcd718197e67f840e89a799a789375f21dd10bd4c78e7fe3c7e933dd132637a900ff2477909
data/README.md CHANGED
@@ -2,12 +2,7 @@
2
2
 
3
3
  Defining templates like `(create|update).turbo_stream.haml` annoyed me.
4
4
 
5
- Working consistently with turbo_stream means shooting lots of partials from the backend to the frontend, which always
6
- needs the same attributes: ID, partial, and maybe some locals. This gem serializes that: Partials can be controlled
7
- directly from the controller.
8
-
9
- It sets the status, generates a flash message, handles redirection, pushes it all to the front and comes with predefined
10
- helpers for enabling request-specs.
5
+ Working consistently with turbo_stream means shooting lots of partials from the backend to the frontend. This always requires the same attributes: the path to the partial, the html-id that turbo_stream points to, and maybe some locals. This gem serialises that: Partials can be controlled directly from the controller. It sets the status, generates a flash message, handles redirection, pushes it all to the front and comes with predefined helpers for enabling request-specs.
11
6
 
12
7
  An overview of how we design a rails-7 application with turbo
13
8
  is [published on dev.to](https://dev.to/chmich/rails-7-vite-wrapping-up-1pia).
@@ -33,6 +28,7 @@ spec/rails_helper.rb
33
28
  ```ruby
34
29
  RSpec.configure do |config|
35
30
  config.include RenderTurboStream::Test::RequestHelpers, type: :request
31
+ config.include RenderTurboStream::Test::RspecRequestHelpers, type: :request #=> if you are on rspec
36
32
  end
37
33
  ```
38
34
 
@@ -86,6 +82,8 @@ stream_partials(
86
82
 
87
83
  The `stream_partial` method is nothing else than a singular of `stream_partials`.
88
84
 
85
+ **locals**: The hash for locals goes through a `symbolize_keys`, so you need to use locals in used partials like this: `locals[:message]`.
86
+
89
87
  ```ruby
90
88
  stream_partial(
91
89
  id,
@@ -95,65 +93,78 @@ stream_partial(
95
93
  )
96
94
  ```
97
95
 
98
- ## Testing
99
-
100
- For system testing there is Capybara. This works great and is necessary for javascript and backend combined apps, but it
101
- is good practice to break tests into smaller pieces.
96
+ ## Testing Scenarios
102
97
 
103
- For the much faster request-level tests, there are some helpers:
98
+ For system testing there is Capybara. Its the only way to check if frontend and backend work together. But its a good practice to break tests into smaller pieces. The much larger number of tests we will write on the much faster request tests, examples here in rspec.
104
99
 
105
100
  If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html
106
101
  that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look
107
102
  like this:
108
103
 
109
- ```ruby
110
- require 'rails_helper'
111
- include RenderTurboStream::TestHelpers
112
-
113
- RSpec.describe "Articles", type: :request do
104
+ **The fastest**
114
105
 
115
- let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
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.
116
107
 
117
- it 'create failed' do
108
+ **rspec**
118
109
 
119
- post articles_path(params: invalid_params)
110
+ For rspec there is a special helper, for a successful save action:
120
111
 
121
- # partials_log: developer information about what the renderer did, and strings to copy and paste into test commands
122
- expect(partials_log.join('')).to include('Article could not be created')
123
- expect(partials_log.join('')).to include('layouts/flash')
112
+ ```ruby
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.
119
+ end
120
+ ```
124
121
 
125
- # THERE ARE 3 COUNTS
122
+ **Redirection**
123
+ ```ruby
124
+ it 'update success' do
125
+ patch article_path(article, params: valid_params)
126
+ expect(turbo_redirect_to).to eq(articles_path)
127
+ end
128
+ ```
126
129
 
127
- # A) Number of partials: "form" and "flash" are 2 partials
128
-
129
- expect(partials_count).to eq(2)
130
+ **Easier check what the partials have done**
130
131
 
131
- # B) Total count of responses per partial (1 => is default)
132
-
133
- expect(partial_response_count('articles/form', total: 1, id: 'form')).to be_truthy
134
- # id is the html id to which turbo-stream is pointing.
132
+ For checking a little more inside the partial responses, but with a simple syntax that checks for a one-time response from a specific partial and may be sufficient for most cases:
133
+ ```ruby
134
+ expect(partials_count).to eq(2)
135
+ # Check the total number of responding partials, in most cases it will be one form and one flash.
136
+
137
+ expect(partial_response('form')).to eq(true)
138
+ # Make sure that the form part (controller_path is not checked) has responded exactly once.
139
+
140
+ expect(partial_response('articles/form', id: 'form', css: '.field_with_errors', include_string: 'Title')).to eq(true)
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..
142
+ ```
135
143
 
136
- # C) If a block is given: the number of matching responses returned by "partial_response_count".
137
-
138
- expect(partial_response_count('flash'){|r|r.inner_html.include?('Article could not be created')}).to eq(1)
139
- # result is either nil (if 0 matched) or a integer >= 1
140
-
141
- expect(partial_response_count('flash'){|r|r.css('div').inner_html.include?('Article could not be created')}).to eq(1)
142
- #=> you can use the validators of nokogiri, check: https://nokogiri.org/tutorials/parsing_an_html_xml_document.html
143
- end
144
+ **More detailed**
144
145
 
145
- end
146
- ```
146
+ Consider a controller action that should respond in 2 flashes.
147
147
 
148
- The test for a redirection might look something like this:
148
+ Helper for writing the test: On Debugger, inside the test, copy and paste the output of `partials_log.join("\n")` to any place.
149
149
 
150
150
  ```ruby
151
- it 'update success' do
152
- a = FactoryBot.create(:article)
153
- patch article_path(a, params: valid_params)
154
- expect(turbo_redirect_to).to eq(articles_path)
155
- end
151
+ expect(partials_count).to eq(1)
152
+
153
+ expect(
154
+ partial_response_count('flash', total: 2) do |p|
155
+ p.css('.callout.success').inner_html.include?('All perfect')
156
+ end
157
+ ).to eq(1)
158
+
159
+ expect(
160
+ partial_response_count('layouts/flash', id: 'flash-box', total: 2) do |p|
161
+ p.css('.callout.alert').inner_html.include?('Something went wrong')
162
+ end
163
+ ).to eq(1)
156
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)
157
168
 
158
169
  P.S.:
159
170
 
@@ -10,8 +10,6 @@ module RenderTurboStream
10
10
  RenderTurboStream::Libs.partials_count(response)
11
11
  end
12
12
 
13
-
14
-
15
13
  # log as helper for the developer to see which flash is set and which partials are rendered to wich ids
16
14
  def partials_log
17
15
  all_responses = RenderTurboStream::Libs.all_responses(response)
@@ -34,7 +32,6 @@ module RenderTurboStream
34
32
  r
35
33
  end
36
34
 
37
-
38
35
  # Returns the path to which turbo_stream.redirect_to would be applied.
39
36
  def turbo_redirect_to
40
37
  expect(response.status).to eq(302)
@@ -42,11 +39,28 @@ module RenderTurboStream
42
39
  r['redirected_to']
43
40
  end
44
41
 
45
-
46
42
  def partial_response_count(partial, id: nil, total: 1, &block)
47
- RenderTurboStream::Libs.partial_response_count(response, partial, id, total, &block )
43
+ RenderTurboStream::Libs.partial_response_count(response, partial, id, total, &block)
48
44
  end
49
45
 
46
+ def partial_response(partial, id: nil, css: nil, include_string: nil)
47
+ c = partial_response_count(partial, id: id, total: 1) do |r|
48
+ if css && include_string
49
+ r.css(css).inner_html.include?(include_string)
50
+ elsif include_string
51
+ r.inner_html.include?(include_string)
52
+ elsif css
53
+ r.css(css)
54
+ else
55
+ r.inner_html
56
+ end
57
+ end
58
+ c == 1
59
+ end
60
+
61
+ def partial_ids
62
+ RenderTurboStream::Libs.partial_ids(response)
63
+ end
50
64
  end
51
65
  end
52
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 = "0.1.46"
2
+ VERSION = "1.2.0"
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_turbo_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.46
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-20 00:00:00.000000000 Z
11
+ date: 2023-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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: []