render_turbo_stream 0.1.20 → 0.1.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -17
- data/app/views/render_turbo_stream_partials.html.erb +19 -5
- data/lib/render_turbo_stream/test_helpers.rb +36 -23
- data/lib/render_turbo_stream/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5656c817324a84016e78f1ab49a2f390448d79095f1c0b29a2ca9a459fdfdf04
|
4
|
+
data.tar.gz: a250f2ff61d1f9f0f622a097e9c725b9741169d794bc2a46f6fa792dfa2d33a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e999d3ce725859885ca2127c54ba2e79f8ea27104ca07ceeebfd750cb23734055ca43c38ed5e8d591a8b7efcd24cde28bc8bf4db75a787e917ea6a31147922e1
|
7
|
+
data.tar.gz: 8c6973e93202a10fc265fa7cc6a01cabe3ee125f3e48e8b7fd4511de7bdc05c8239146b834051321a47c883fbd2b55a3b731bc4bfab25dbb50c6f05d5d12e42f
|
data/README.md
CHANGED
@@ -79,36 +79,47 @@ stream_partial(
|
|
79
79
|
|
80
80
|
## Testing
|
81
81
|
|
82
|
-
For system testing,
|
82
|
+
For system testing we have Capybara. This works great and is necessary, but it is good practice to break tests into smaller pieces.
|
83
83
|
|
84
84
|
For request-level tests, there are some helpers:
|
85
85
|
|
86
|
-
If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special
|
86
|
+
If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look like this:
|
87
87
|
|
88
88
|
```ruby
|
89
89
|
require 'rails_helper'
|
90
90
|
include RenderTurboStream::TestHelpers
|
91
91
|
|
92
92
|
RSpec.describe "Articles", type: :request do
|
93
|
+
|
93
94
|
let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
|
95
|
+
|
94
96
|
it 'create failed' do
|
95
97
|
post articles_path(params: invalid_params)
|
98
|
+
|
96
99
|
expect(response.status).to eq(422)
|
97
|
-
expect(
|
98
|
-
|
99
|
-
|
100
|
-
expect(
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
expect(
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
expect(
|
109
|
-
|
110
|
-
|
111
|
-
|
100
|
+
expect(partials_count).to eq(2)
|
101
|
+
|
102
|
+
expect(partials_paths).to include('articles/form', 'layouts/flash')
|
103
|
+
expect(partials_ids).to include('form', 'flash-box')
|
104
|
+
|
105
|
+
# ..partials_params_* are the params by what partials are called
|
106
|
+
# id is the id where, by example, a turbo_stream.replace points to
|
107
|
+
expect(partials_params_by_id['flash-box'].length).to eq(1)
|
108
|
+
expect(partials_params_by_id['flash-box'].first['partial']).to eq('layouts/flash')
|
109
|
+
|
110
|
+
# ..partials_html_* is the html returned from the partial as string
|
111
|
+
expect(partials_html_by_id['flash-box'].length).to eq(1)
|
112
|
+
expect(partials_html_by_id['flash-box'].first).to include('Article could not be created')
|
113
|
+
|
114
|
+
# unique_partial returns false if this ID is cero or multiple times rendered to
|
115
|
+
expect(unique_partial_html_by_id('flash-box')).to include('Article could not be created')
|
116
|
+
|
117
|
+
# *_path is the controller_path for calling the partial
|
118
|
+
expect(partials_params_by_path['layouts/flash'].length).to eq(1)
|
119
|
+
expect(partials_params_by_path['layouts/flash'].first['partial']).to eq('layouts/flash')
|
120
|
+
|
121
|
+
expect(partials_html_by_path['layouts/flash'].length).to eq(1)
|
122
|
+
expect(partials_html_by_path['layouts/flash'].first).to include('Article could not be created')
|
112
123
|
end
|
113
124
|
end
|
114
125
|
```
|
@@ -1,19 +1,33 @@
|
|
1
1
|
<% partials_count = 0 %>
|
2
2
|
<% rendered_partials = [] %>
|
3
|
-
<% htmls = [] %>
|
4
3
|
<% htmls_by_id = {} %>
|
4
|
+
<% params_by_id = {} %>
|
5
|
+
<% htmls_by_path = {} %>
|
6
|
+
<% params_by_path = {} %>
|
7
|
+
|
5
8
|
<% streams.each do |s| %>
|
9
|
+
|
6
10
|
<% html = (render s[:partial], locals: s[:locals], formats: [:html ]) %>
|
7
|
-
<% htmls.push(html) %>
|
8
11
|
<% htmls_by_id[s[:id]] ||= [] %>
|
9
12
|
<% htmls_by_id[s[:id]].push(html) %>
|
13
|
+
<% params_by_id[s[:id]] ||= [] %>
|
14
|
+
<% params_by_id[s[:id]].push(s) %>
|
15
|
+
|
16
|
+
<% htmls_by_path[s[:partial]] ||= [] %>
|
17
|
+
<% htmls_by_path[s[:partial]].push(html) %>
|
18
|
+
<% params_by_path[s[:partial]] ||= [] %>
|
19
|
+
<% params_by_path[s[:partial]].push(s) %>
|
20
|
+
|
10
21
|
<% partials_count += 1 %>
|
11
22
|
<% rendered_partials.push(s[:partial]) %>
|
23
|
+
|
12
24
|
<% end %>
|
13
25
|
|
26
|
+
<%= content_tag :div, htmls_by_id.to_json, id: 'htmls_by_id' %>
|
27
|
+
<%= content_tag :div, params_by_id.to_json, id: 'params_by_id' %>
|
28
|
+
|
29
|
+
<%= content_tag :div, htmls_by_path.to_json, id: 'htmls_by_path' %>
|
30
|
+
<%= content_tag :div, params_by_path.to_json, id: 'params_by_path' %>
|
14
31
|
|
15
32
|
<%= content_tag :div, check.to_json, id: 'check-json' %>
|
16
|
-
<%= content_tag :div, htmls.to_json, id: 'htmls' %>
|
17
|
-
<%= content_tag :div, htmls_by_id.to_json, id: 'htmls_by_id' %>
|
18
33
|
<%= content_tag :div, rendered_partials.to_json, id: 'rendered-partials' %>
|
19
|
-
<%= content_tag :div, streams.to_json, id: 'streams' %>
|
@@ -1,44 +1,57 @@
|
|
1
1
|
module RenderTurboStream
|
2
2
|
module TestHelpers
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
|
4
|
+
# returns hash of arrays with responded html(as string) contents by ID
|
5
|
+
def partials_html_by_id
|
6
|
+
e = Nokogiri::HTML(response.body).search('#htmls_by_id').first
|
7
|
+
JSON.parse(e.inner_html)
|
8
|
+
end
|
9
|
+
|
10
|
+
# returns false if id is cero or multiple times rendered to
|
11
|
+
def unique_partial_html_by_id(id)
|
12
|
+
p = partials_html_by_id[id]
|
13
|
+
if p.length != 1
|
14
|
+
false
|
15
|
+
else
|
16
|
+
p.first
|
17
|
+
end
|
6
18
|
end
|
7
19
|
|
8
|
-
|
9
|
-
|
10
|
-
e = Nokogiri::HTML(response.body).search('#htmls').first
|
20
|
+
def partials_params_by_id
|
21
|
+
e = Nokogiri::HTML(response.body).search('#params_by_id').first
|
11
22
|
JSON.parse(e.inner_html)
|
12
23
|
end
|
13
24
|
|
14
|
-
# returns hash of arrays with responded html(as string) contents by
|
15
|
-
def
|
16
|
-
e = Nokogiri::HTML(response.body).search('#
|
25
|
+
# returns hash of arrays with responded html(as string) contents by partial-path
|
26
|
+
def partials_html_by_path
|
27
|
+
e = Nokogiri::HTML(response.body).search('#htmls_by_path').first
|
17
28
|
JSON.parse(e.inner_html)
|
18
29
|
end
|
19
30
|
|
20
|
-
#
|
21
|
-
def
|
22
|
-
e = Nokogiri::HTML(response.body).search('#
|
31
|
+
# returns hash of arrays with params by them rendered partials are called
|
32
|
+
def partials_params_by_path
|
33
|
+
e = Nokogiri::HTML(response.body).search('#params_by_path').first
|
23
34
|
JSON.parse(e.inner_html)
|
24
35
|
end
|
25
36
|
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
partials = Nokogiri::HTML(response.body).search('#streams').first
|
30
|
-
h = JSON.parse(partials.inner_html)
|
31
|
-
h.select{|e| e['id'] == id}
|
37
|
+
# count of rendered parials
|
38
|
+
def partials_count
|
39
|
+
partials_paths.length
|
32
40
|
end
|
33
41
|
|
34
|
-
#
|
35
|
-
def
|
36
|
-
|
42
|
+
# array of strings of rendered partial ids
|
43
|
+
def partials_ids
|
44
|
+
e = Nokogiri::HTML(response.body).search('#check-json').first
|
45
|
+
JSON.parse(e.inner_html).keys
|
37
46
|
end
|
38
47
|
|
39
|
-
|
40
|
-
|
48
|
+
# array of strings of paths by them rendered partials are called, example: ['articles/form']
|
49
|
+
def partials_paths
|
50
|
+
e = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
51
|
+
JSON.parse(e.inner_html)
|
41
52
|
end
|
53
|
+
|
54
|
+
# check response status and return redirected_to path
|
42
55
|
def turbo_redirected_to
|
43
56
|
expect(response.status).to eq(302)
|
44
57
|
r = JSON.parse(response.body)
|