Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7141010

Browse files
committed
Make sure UJS is only ever loaded once
1 parent 36f51b4 commit 7141010

File tree

7 files changed

+56
-25
lines changed

7 files changed

+56
-25
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Sprockets 4 expects this file
22
//
33
//= link application.js
4+
//= link turbolinks_only.js
45
//= link application.css
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//= require turbolinks
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class PackComponentsController < ApplicationController
2+
# make sure Sprockets application.js isn't loaded:
3+
layout false
24
def show
35
end
46
end

‎test/dummy/app/views/layouts/application.html.erb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<html>
33
<head>
44
<title>Dummy</title>
5-
<% if SprocketsHelpers.available? %>
5+
<% if WebpackerHelpers.available? %>
6+
<%= javascript_include_tag "turbolinks_only", "data-turbolinks-track" => true %>
7+
<%= javascript_pack_tag "application" %>
8+
<% elsif SprocketsHelpers.available? %>
69
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
710
<% end %>
811
<%= csrf_meta_tags %>

‎test/react/rails/react_rails_ujs_test.rb‎

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,37 @@
44
class ReactRailsUJSTest < ActionDispatch::IntegrationTest
55
include Capybara::DSL
66

7+
compiled = false
78
setup do
8-
Capybara.current_driver = Capybara.javascript_driver
9-
WebpackerHelpers.compile_if_missing
9+
if !compiled
10+
React::ServerRendering.reset_pool
11+
WebpackerHelpers.compile
12+
end
13+
end
14+
15+
# Normalize for webpacker check:
16+
def assert_greeting(page, plain_greeting, refute: false)
17+
normalized_greeting = if WebpackerHelpers.available?
18+
greeting, name = plain_greeting.split(" ")
19+
"#{greeting} from Webpacker #{name}"
20+
else
21+
plain_greeting
22+
end
23+
24+
if refute
25+
assert page.has_no_content?(normalized_greeting), page.body
26+
else
27+
assert page.has_content?(normalized_greeting), page.body
28+
end
29+
end
30+
31+
def refute_greeting(page, greeting)
32+
assert_greeting(page, greeting, refute: true)
1033
end
1134

1235
test 'ujs object present on the global React object and has our methods' do
1336
visit '/pages/1'
14-
assertpage.has_content?('Hello Bob')
37+
assert_greeting(page,'Hello Bob')
1538

1639
# the exposed ujs object is present
1740
ujs_present = page.evaluate_script('typeof ReactRailsUJS === "object";')
@@ -34,82 +57,82 @@ class ReactRailsUJSTest < ActionDispatch::IntegrationTest
3457

3558
test 'react_ujs works with rendered HTML' do
3659
visit '/pages/1'
37-
assertpage.has_content?('Hello Bob')
60+
assert_greeting(page,'Hello Bob')
3861

3962
page.click_button 'Goodbye'
40-
assertpage.has_no_content?('Hello Bob')
41-
assertpage.has_content?('Goodbye Bob')
63+
refute_greeting(page,'Hello Bob')
64+
assert_greeting(page,'Goodbye Bob')
4265
end
4366

4467
test 'react_ujs works with Turbolinks' do
4568
visit '/pages/1'
46-
assertpage.has_content?('Hello Bob')
69+
assert_greeting(page,'Hello Bob')
4770
assert page.evaluate_script("Turbolinks.supported")
4871

4972
# Try clicking links.
5073
page.click_link('Alice')
5174
wait_for_turbolinks_to_be_available
52-
assertpage.has_content?('Hello Alice')
75+
assert_greeting(page,'Hello Alice')
5376

5477
page.click_link('Bob')
5578
wait_for_turbolinks_to_be_available
56-
assertpage.has_content?('Hello Bob')
79+
assert_greeting(page,'Hello Bob')
5780

5881
# Try going back.
5982
page.execute_script('history.back();')
6083
wait_for_turbolinks_to_be_available
61-
assertpage.has_content?('Hello Alice')
84+
assert_greeting(page,'Hello Alice')
6285

6386
# Try Turbolinks javascript API.
6487
page.execute_script('Turbolinks.visit("/pages/2");')
6588
wait_for_turbolinks_to_be_available
66-
assertpage.has_content?('Hello Alice')
89+
assert_greeting(page,'Hello Alice')
6790

6891

6992
page.execute_script('Turbolinks.visit("/pages/1");')
7093
wait_for_turbolinks_to_be_available
71-
assertpage.has_content?('Hello Bob')
94+
assert_greeting(page,'Hello Bob')
7295

7396
# Component state is not persistent after clicking current page link.
7497
page.click_button 'Goodbye'
75-
assertpage.has_content?('Goodbye Bob')
98+
assert_greeting(page,'Goodbye Bob')
7699

77100
page.click_link('Bob')
78101
wait_for_turbolinks_to_be_available
79-
assertpage.has_content?('Hello Bob')
102+
assert_greeting(page,'Hello Bob')
80103
end
81104

82105
test 'react_ujs can unmount/mount using a selector reference for a component parent' do
83106
visit '/pages/1'
84-
assertpage.has_content?('Hello Bob'),page.body
107+
assert_greeting(page,'Hello Bob')
85108

86109
page.click_button "Unmount by parent selector"
87-
assertpage.has_no_content?('Hello Bob'),page.body
110+
refute_greeting(page,'Hello Bob')
88111

89112
page.click_button "Mount by parent selector"
90-
assertpage.has_content?('Hello Bob'),page.body
113+
assert_greeting(page,'Hello Bob')
91114
end
92115

93116
test 'react_ujs can unmount/mount using a selector reference for the component' do
94117
visit '/pages/1'
95-
assertpage.has_content?('Hello Bob'),page.body
118+
assert_greeting(page,'Hello Bob')
96119

97120
page.click_button "Unmount by own selector"
98-
assertpage.has_no_content?('Hello Bob'),page.body
121+
refute_greeting(page,'Hello Bob')
99122

100123
page.click_button "Mount by own selector"
101-
assertpage.has_content?('Hello Bob'),page.body
124+
assert_greeting(page,'Hello Bob')
102125
end
103126

104127
test 'react_ujs can unmount/mount using a dom node context' do
105128
visit '/pages/1'
106-
assertpage.has_content?('Hello Bob'),page.body
129+
assert_greeting(page,'Hello Bob')
107130

108131
page.click_button "Unmount by parent node"
109-
assertpage.has_no_content?('Hello Bob'),page.body
132+
refute_greeting(page,'Hello Bob')
110133

111134
page.click_button "Mount by parent node"
112-
assertpage.has_content?('Hello Bob'),page.body
135+
assert_greeting(page,'Hello Bob')
113136
end
114137

115138
test 'react server rendering also gets mounted on client' do

‎test/react/rails/webpacker_test.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ReactRailsWebpackerTest < ActionDispatch::IntegrationTest
77
setup do
88
Capybara.current_driver = Capybara.javascript_driver
99
WebpackerHelpers.compile
10+
React::ServerRendering.reset_pool
1011
end
1112

1213
teardown do

‎test/test_helper.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
WebpackerHelpers.clear_webpacker_packs
2626

27-
Capybara.javascript_driver = :poltergeist
2827
Capybara.app = Rails.application
2928

3029
Capybara.register_driver :poltergeist_debug do |app|
@@ -37,6 +36,7 @@
3736
Capybara::Poltergeist::Driver.new(app, poltergeist_options)
3837
end
3938
Capybara.javascript_driver = :poltergeist_debug
39+
Capybara.current_driver = Capybara.javascript_driver
4040

4141

4242
CACHE_PATH = Pathname.new File.expand_path("../dummy/tmp/cache", __FILE__)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /