render_turbo_stream 0.1.45 → 1.1.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: 30eb2a0495b25d67ff59b2194b7b54cf20c8d0f813ade47a6809616d13231ba3
4
- data.tar.gz: a934e1be22f6610a10fd16ce642a5601d8ef11224bfd1a9d70f1f1d471a83d71
3
+ metadata.gz: 8c88aab0f6268663008843f37d2b7cd052f5883d4751c8c1558a16a166e06752
4
+ data.tar.gz: 97188c8d6d19998f7a6e617eb07a9de4a92c88b96d917908b738932301a0bc11
5
5
  SHA512:
6
- metadata.gz: c12cc64cbf409f74a7bf8f2faf6c6aa63fbe063a0bca4d8ec5939fea06b77011f873633e71daa44d6ef518f70686e382a115ac9c0da6b426413fb8665486c770
7
- data.tar.gz: e6f689292afcc93064d1a67ee5d13a1bcd68a85cfb2b21a600624e5224585c4d5db7d6b21232ee30716924d5de82255ed321cab0a8ccf0d65c048aa7c9228325
6
+ metadata.gz: cd3b7d91aa83267ff6f6f5da938fbf8e65704396b60cba5ff14ca82f9d15ad88954b330bbe170043514cba4cb67f7971ac57b55062828860e9e13c7129ef91b7
7
+ data.tar.gz: 3d7c1b8917846500a637dbb8ccb46b0535db82e4d64690c488d0fe8b98d070b51a88cb7d3c5cd41296c55f9f5d51b2bec66a0dea8fd981e9ab407e273568a93c
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).
@@ -86,6 +81,8 @@ stream_partials(
86
81
 
87
82
  The `stream_partial` method is nothing else than a singular of `stream_partials`.
88
83
 
84
+ **locals**: The hash for locals goes through a `symbolize_keys`, so you need to use locals in used partials like this: `locals[:message]`.
85
+
89
86
  ```ruby
90
87
  stream_partial(
91
88
  id,
@@ -95,64 +92,68 @@ stream_partial(
95
92
  )
96
93
  ```
97
94
 
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.
95
+ ## Testing Scenarios
102
96
 
103
- For the much faster request-level tests, there are some helpers:
97
+ 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
98
 
105
99
  If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html
106
100
  that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look
107
101
  like this:
108
102
 
109
- ```ruby
110
- require 'rails_helper'
111
- include RenderTurboStream::TestHelpers
112
-
113
- RSpec.describe "Articles", type: :request do
103
+ **The easiest test**
114
104
 
115
- let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
105
+ If you want to check if a controller action succeeded, just check for `response.status`.
116
106
 
117
- it 'create failed' do
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
+ ```ruby
109
+ it 'update failed' do
110
+ patch article_path(article, params: invalid_params)
111
+ expect(response.status).to eq(422)
112
+ end
113
+ ```
118
114
 
119
- post articles_path(params: invalid_params)
115
+ **Test redirection**
116
+ ```ruby
117
+ it 'update success' do
118
+ patch article_path(article, params: valid_params)
119
+ expect(turbo_redirect_to).to eq(articles_path)
120
+ end
121
+ ```
120
122
 
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')
123
+ **Easier check what the partials have done**
124
124
 
125
- # THERE ARE 3 COUNTS
125
+ 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:
126
+ ```ruby
127
+ expect(partials_count).to eq(2)
128
+ # Check the total number of responding partials, in most cases it will be one form and one flash.
129
+
130
+ expect(partial_response('form')).to eq(true)
131
+ # Make sure that the form part (controller_path is not checked) has responded exactly once.
132
+
133
+ expect(partial_response('articles/form', id: 'form', css: '.field_with_errors', include_string: 'Title')).to eq(true)
134
+ # 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
+ ```
126
136
 
127
- # A) Number of partials: "form" and "flash" are 2 partials
128
-
129
- expect(partials_count).to eq(2)
137
+ **More detailed test**
130
138
 
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.
139
+ Consider a controller action that should respond in 2 flashes.
135
140
 
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
141
+ Helper for writing the test: On Debugger, inside the test, copy and paste the output of `partials_log.join("\n")` to any place.
144
142
 
145
- end
146
- ```
143
+ ```ruby
144
+ expect(partials_count).to eq(1)
147
145
 
148
- The test for a redirection might look something like this:
146
+ expect(
147
+ partial_response_count('flash', total: 2) do |p|
148
+ p.css('.callout.success').inner_html.include?('All perfect')
149
+ end
150
+ ).to eq(1)
149
151
 
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
152
+ expect(
153
+ partial_response_count('layouts/flash', id: 'flash-box', total: 2) do |p|
154
+ p.css('.callout.alert').inner_html.include?('Something went wrong')
155
+ end
156
+ ).to eq(1)
156
157
  ```
157
158
 
158
159
  P.S.:
@@ -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,9 +39,23 @@ 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)
44
+ end
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
48
59
  end
49
60
 
50
61
  end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "0.1.45"
2
+ VERSION = "1.1.0"
3
3
  end
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.45
4
+ version: 1.1.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
@@ -71,6 +71,5 @@ requirements: []
71
71
  rubygems_version: 3.4.12
72
72
  signing_key:
73
73
  specification_version: 4
74
- summary: Render turbo-stream partials directly from the controller, including test
75
- helpers
74
+ summary: Render turbo-stream partials directly from the controller. Test helpers included
76
75
  test_files: []