render_turbo_stream 0.1.46 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -47
- data/lib/render_turbo_stream/test/request_helpers.rb +16 -5
- data/lib/render_turbo_stream/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c88aab0f6268663008843f37d2b7cd052f5883d4751c8c1558a16a166e06752
|
4
|
+
data.tar.gz: 97188c8d6d19998f7a6e617eb07a9de4a92c88b96d917908b738932301a0bc11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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
|
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
|
-
|
110
|
-
require 'rails_helper'
|
111
|
-
include RenderTurboStream::TestHelpers
|
112
|
-
|
113
|
-
RSpec.describe "Articles", type: :request do
|
103
|
+
**The easiest test**
|
114
104
|
|
115
|
-
|
105
|
+
If you want to check if a controller action succeeded, just check for `response.status`.
|
116
106
|
|
117
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
128
|
-
|
129
|
-
expect(partials_count).to eq(2)
|
137
|
+
**More detailed test**
|
130
138
|
|
131
|
-
|
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
|
-
|
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
|
-
|
146
|
-
|
143
|
+
```ruby
|
144
|
+
expect(partials_count).to eq(1)
|
147
145
|
|
148
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
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
|
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:
|
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-
|
11
|
+
date: 2023-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|