render_turbo_stream 0.1.15 → 0.1.17

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: 6b14cca0fede85f072fa0c0fd6e854067a48a6842c37bf1cc25a767202b9cd44
4
- data.tar.gz: 5f68d9d74db8da5da80ea51ce2427fc9c7863748ce19b214e3885f34197cd746
3
+ metadata.gz: da571baa70cde46b6b14ba89a0a03f03077c0793b10859635fbdaf63b6a3cfe9
4
+ data.tar.gz: 3693163b941c92eb9c2c7db6039b466bfeb89ab964653ab1c96ef76c41f035f9
5
5
  SHA512:
6
- metadata.gz: 5f54ca6c438da2e9b426a113c3793af72598ce200161b8f87f7252fb6e2378239541eeb8b0a1c56345e4bda4f8fc9107c26dc2c0fb371ef5d5966dfe4f9c64f5
7
- data.tar.gz: 15172f63ecb115b722f11fd0a1a43ce4bcc7a884d4abc065cf9d53dbe60487ab43650e96970a038acd7360e58e2e6453867be6b623fbe2e235c8095e25ec8462
6
+ metadata.gz: 36c87925c827b81a718e0d97cf302f31530541f359474d31a4ad3dc76012f3bd6d075d27b9f0d1d006033011d2096184264169988b87403de20080058b1877e8
7
+ data.tar.gz: bad186cba162fea280c24dfc14b9438b8bf60ce913fc97cb3571c0a0ac5de9432aa8bd1e58d86b8cb9991774a027fc7c61c64d97fa94127481124792e8e25d5d
data/README.md CHANGED
@@ -79,22 +79,26 @@ stream_partial(
79
79
 
80
80
  ## Testing
81
81
 
82
- For system testing you need capybara.
82
+ For system testing there is capybara.
83
83
 
84
- For enabling request specs:
84
+ For Tests on request level there are some helpers:
85
85
 
86
- If `request.format` is not `turbo_stream` the method responds as `json` with status.
87
- `response.body` returns a `json` where the key is the `html-ID` that is used by turbo-stream with the properties of the partials. Testing by rspec could look like this:
86
+ If request format is not `turbo_stream`, which is the case on request specs, the method responds in a html by a special format that can easily be checked by included test-helpers. So, tests could look like this:
88
87
 
89
88
  ```ruby
90
- it 'update failed' do
91
- a = FactoryBot.create(:article)
92
- patch article_path(a, params: invalid_params)
93
- expect(response.status).to eq(422)
94
- h = JSON.parse(response.body)
95
- expect(h['form']['partial']).to eq('articles/form')
96
- expect(h['flash-box']['partial']).to eq('layouts/flash')
97
- expect(h.keys.length).to eq(2)
89
+ require 'rails_helper'
90
+ include RenderTurboStream::TestHelpers
91
+
92
+ RSpec.describe "Articles", type: :request do
93
+ let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
94
+ it 'update failed' do
95
+ a = FactoryBot.create(:article)
96
+ patch article_path(a, params: invalid_params)
97
+ expect(response.status).to eq(422)
98
+ expect(rendered_partials_count).to eq(2)
99
+ expect(rendered_partials).to include('articles/form', 'layouts/flash')
100
+ expect(streamed_ids).to include('form', 'flash-box')
101
+ end
98
102
  end
99
103
  ```
100
104
 
@@ -102,11 +106,9 @@ Test for a redirection could look like this:
102
106
 
103
107
  ```ruby
104
108
  it 'update success' do
105
- a = FactoryBot.create(:article)
106
- patch article_path(a, params: valid_params)
107
- expect(response.status).to eq(302)
108
- r = JSON.parse(response.body)
109
- expect(r['redirected_to']).to eq(articles_path)
109
+ a = FactoryBot.create(:article)
110
+ patch article_path(a, params: valid_params)
111
+ expect(turbo_redirected_to).to eq(articles_path) #=> turbo_redirected_to includes a «expect(response.status).to eq(302)»
110
112
  end
111
113
  ```
112
114
 
@@ -0,0 +1,11 @@
1
+ <% partials_count = 0 %>
2
+ <% rendered_partials = [] %>
3
+ <% streams.each do |s| %>
4
+ <% render s[:partial], locals: s[:locals], formats: [:html ] %>
5
+ <% partials_count += 1 %>
6
+ <% rendered_partials.push(s[:partial]) %>
7
+ <% end %>
8
+
9
+
10
+ <%= content_tag :div, check.to_json, id: 'check-json' %>
11
+ <%= content_tag :div, rendered_partials.to_json, id: 'rendered-partials' %>
@@ -0,0 +1,21 @@
1
+ module RenderTurboStream
2
+ module TestHelpers
3
+ def streamed_ids
4
+ e = Nokogiri::HTML(response.body).search('#check-json').first
5
+ JSON.parse(e.inner_html).keys
6
+ end
7
+ def rendered_partials
8
+ e = Nokogiri::HTML(response.body).search('#rendered-partials').first
9
+ JSON.parse(e.inner_html)
10
+ end
11
+
12
+ def rendered_partials_count
13
+ rendered_partials.length
14
+ end
15
+ def turbo_redirected_to
16
+ expect(response.status).to eq(302)
17
+ r = JSON.parse(response.body)
18
+ r['redirected_to']
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "0.1.15"
2
+ VERSION = "0.1.17"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "render_turbo_stream/version"
2
2
  require "render_turbo_stream/railtie"
3
3
  require 'render_turbo_stream/engine'
4
+ require 'render_turbo_stream/test_helpers'
4
5
 
5
6
  module RenderTurboStream
6
7
 
@@ -155,7 +156,8 @@ module RenderTurboStream
155
156
  b.delete(:id)
156
157
  check[a[:id]] = b
157
158
  end
158
- render json: check
159
+ #render json: check
160
+ render template: 'render_turbo_stream_partials', locals: { check: check, streams: ary }, layout: false, formats: :html
159
161
  end
160
162
  end
161
163
 
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: 0.1.15
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
@@ -25,9 +25,9 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 7.0.4.3
27
27
  description: 'Writing views like (create|update).turbo_stream.haml annoyed me. This
28
- gem handles translated flash messages, sets status, and renders turbo-stream partials.
29
- You can control turbo-stream partials directly from the controller. It also handles
30
- redirection: turbo_power is included.'
28
+ gem handles translated flash messages, sets status, and renders partials by turbo-stream.
29
+ You can control them directly from the controller. It also handles redirection:
30
+ turbo_power is included.'
31
31
  email:
32
32
  - christian@sedlmair.ch
33
33
  executables: []
@@ -37,11 +37,13 @@ files:
37
37
  - README.md
38
38
  - Rakefile
39
39
  - app/controllers/render_turbo_stream/application_controller.rb
40
+ - app/views/render_turbo_stream_partials.html.erb
40
41
  - app/views/render_turbo_stream_partials.turbo_stream.erb
41
42
  - app/views/render_turbo_stream_redirect.turbo_stream.erb
42
43
  - lib/render_turbo_stream.rb
43
44
  - lib/render_turbo_stream/engine.rb
44
45
  - lib/render_turbo_stream/railtie.rb
46
+ - lib/render_turbo_stream/test_helpers.rb
45
47
  - lib/render_turbo_stream/version.rb
46
48
  - lib/tasks/render_turbo_stream_tasks.rake
47
49
  homepage: https://gitlab.com/sedl/renderturbostream
@@ -69,5 +71,5 @@ requirements: []
69
71
  rubygems_version: 3.4.12
70
72
  signing_key:
71
73
  specification_version: 4
72
- summary: Rendering turbo-stream partials directly from controller
74
+ summary: Render turbo-stream partials directly from the controller
73
75
  test_files: []