Skip to content

Commit 83437cf

Browse files
committed
Add unit test for Propshaft::Helper
1 parent 4af2f12 commit 83437cf

File tree

1 file changed

+261
-0
lines changed

1 file changed

+261
-0
lines changed

test/propshaft/helper_test.rb

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
require "test_helper"
2+
3+
class Propshaft::HelperTest < ActionView::TestCase
4+
test "asset_integrity returns SHA256 hash for existing asset" do
5+
integrity = asset_integrity("hello_world.js")
6+
assert_equal "sha384-BIr0kyMRq2sfytK/T0XlGjfav9ZZrWkSBC2yHVunCchnkpP83H28/UtHw+m9iNHO", integrity
7+
end
8+
9+
test "asset_integrity with asset type option" do
10+
integrity = asset_integrity("hello_world", type: :stylesheet)
11+
assert_equal "sha384-ZSAt6UaTZ1OYvSB1fr2WXE8izMW4qnd17BZ1zaZ3TpAdIw3VEUmyupHd/k/cMCqM", integrity
12+
end
13+
14+
test "compute_asset_path returns resolved path for existing asset" do
15+
path = compute_asset_path("hello_world.js")
16+
assert_equal "/assets/hello_world-888761f8.js", path
17+
end
18+
19+
test "compute_asset_path raises MissingAssetError for nonexistent asset" do
20+
error = assert_raises(Propshaft::MissingAssetError) do
21+
compute_asset_path("nonexistent.txt")
22+
end
23+
assert_equal "The asset 'nonexistent.txt' was not found in the load path.", error.message
24+
end
25+
26+
test "stylesheet_link_tag with integrity in secure context" do
27+
request.headers["HTTPS"] = "on"
28+
29+
result = stylesheet_link_tag("hello_world", integrity: true)
30+
31+
assert_dom_equal(<<~HTML, result)
32+
<link
33+
rel="stylesheet"
34+
href="/assets/hello_world-4137140a.css"
35+
integrity="sha384-ZSAt6UaTZ1OYvSB1fr2WXE8izMW4qnd17BZ1zaZ3TpAdIw3VEUmyupHd/k/cMCqM"
36+
/>
37+
HTML
38+
end
39+
40+
test "stylesheet_link_tag with integrity in local context" do
41+
request.remote_addr = "127.0.0.1"
42+
43+
result = stylesheet_link_tag("hello_world", integrity: true)
44+
45+
assert_dom_equal(<<~HTML, result)
46+
<link
47+
rel="stylesheet"
48+
href="/assets/hello_world-4137140a.css"
49+
integrity="sha384-ZSAt6UaTZ1OYvSB1fr2WXE8izMW4qnd17BZ1zaZ3TpAdIw3VEUmyupHd/k/cMCqM"
50+
/>
51+
HTML
52+
end
53+
54+
test "stylesheet_link_tag without integrity in insecure context" do
55+
result = stylesheet_link_tag("hello_world", integrity: true)
56+
57+
assert_dom_equal(<<~HTML, result)
58+
<link
59+
rel="stylesheet"
60+
href="/assets/hello_world-4137140a.css"
61+
/>
62+
HTML
63+
end
64+
65+
test "stylesheet_link_tag without request context" do
66+
request.remote_addr = "127.0.0.1"
67+
@request = nil
68+
69+
result = stylesheet_link_tag("hello_world", integrity: true)
70+
71+
assert_dom_equal(<<~HTML, result)
72+
<link
73+
rel="stylesheet"
74+
href="/assets/hello_world-4137140a.css"
75+
/>
76+
HTML
77+
end
78+
79+
test "stylesheet_link_tag with multiple sources and integrity" do
80+
request.headers["HTTPS"] = "on"
81+
82+
result = stylesheet_link_tag("hello_world", "goodbye", integrity: true)
83+
84+
assert_dom_equal(<<~HTML, result)
85+
<link
86+
rel="stylesheet"
87+
href="/assets/hello_world-4137140a.css"
88+
integrity="sha384-ZSAt6UaTZ1OYvSB1fr2WXE8izMW4qnd17BZ1zaZ3TpAdIw3VEUmyupHd/k/cMCqM"
89+
/>
90+
<link
91+
rel="stylesheet"
92+
href="/assets/goodbye-b1dc9940.css"
93+
integrity="sha384-fdjPDIC6emuy5FFidLaq2BgRhq3H1f7Cukj0jMOA8yltqt7kFKylYD+MjrkdZ7Ji"
94+
/>
95+
HTML
96+
end
97+
98+
test "stylesheet_link_tag with :all option" do
99+
result = stylesheet_link_tag(:all)
100+
101+
assert_dom_equal(<<~HTML, result)
102+
<link rel="stylesheet" href="/assets/goodbye-b1dc9940.css" />
103+
<link rel="stylesheet" href="/assets/hello_world-4137140a.css" />
104+
<link rel="stylesheet" href="/assets/library-86a3b7a9.css" />
105+
HTML
106+
end
107+
108+
test "stylesheet_link_tag with :app option" do
109+
result = stylesheet_link_tag(:app)
110+
111+
assert_dom_equal(<<~HTML, result)
112+
<link rel="stylesheet" href="/assets/goodbye-b1dc9940.css" />
113+
<link rel="stylesheet" href="/assets/hello_world-4137140a.css" />
114+
HTML
115+
end
116+
117+
test "stylesheet_link_tag with additional options" do
118+
result = stylesheet_link_tag(
119+
"hello_world",
120+
media: "print",
121+
data: { turbo_track: "reload" }
122+
)
123+
124+
assert_dom_equal(<<~HTML, result)
125+
<link
126+
rel="stylesheet"
127+
href="/assets/hello_world-4137140a.css"
128+
media="print"
129+
data-turbo-track="reload"
130+
/>
131+
HTML
132+
end
133+
134+
test "javascript_include_tag with integrity in secure context" do
135+
request.headers["HTTPS"] = "on"
136+
137+
result = javascript_include_tag("hello_world", integrity: true)
138+
139+
assert_dom_equal(<<~HTML, result)
140+
<script
141+
src="/assets/hello_world-888761f8.js"
142+
integrity="sha384-BIr0kyMRq2sfytK/T0XlGjfav9ZZrWkSBC2yHVunCchnkpP83H28/UtHw+m9iNHO"
143+
></script>
144+
HTML
145+
end
146+
147+
test "javascript_include_tag with integrity in local context" do
148+
request.remote_addr = "127.0.0.1"
149+
150+
result = javascript_include_tag("hello_world", integrity: true)
151+
152+
assert_dom_equal(<<~HTML, result)
153+
<script
154+
src="/assets/hello_world-888761f8.js"
155+
integrity="sha384-BIr0kyMRq2sfytK/T0XlGjfav9ZZrWkSBC2yHVunCchnkpP83H28/UtHw+m9iNHO"
156+
></script>
157+
HTML
158+
end
159+
160+
test "javascript_include_tag without integrity in insecure context" do
161+
result = javascript_include_tag("hello_world", integrity: true)
162+
163+
assert_dom_equal(<<~HTML, result)
164+
<script
165+
src="/assets/hello_world-888761f8.js"
166+
></script>
167+
HTML
168+
end
169+
170+
test "javascript_include_tag with multiple sources and integrity" do
171+
request.headers["HTTPS"] = "on"
172+
173+
result = javascript_include_tag("hello_world", "hello_world", integrity: true)
174+
175+
assert_dom_equal(<<~HTML, result)
176+
<script
177+
src="/assets/hello_world-888761f8.js"
178+
integrity="sha384-BIr0kyMRq2sfytK/T0XlGjfav9ZZrWkSBC2yHVunCchnkpP83H28/UtHw+m9iNHO"
179+
></script>
180+
<script
181+
src="/assets/hello_world-888761f8.js"
182+
integrity="sha384-BIr0kyMRq2sfytK/T0XlGjfav9ZZrWkSBC2yHVunCchnkpP83H28/UtHw+m9iNHO"
183+
></script>
184+
HTML
185+
end
186+
187+
test "javascript_include_tag with additional options" do
188+
result = javascript_include_tag(
189+
"hello_world",
190+
defer: true,
191+
data: { turbo_track: "reload" }
192+
)
193+
194+
assert_dom_equal(<<~HTML, result)
195+
<script
196+
src="/assets/hello_world-888761f8.js"
197+
defer="defer"
198+
data-turbo-track="reload"
199+
></script>
200+
HTML
201+
end
202+
203+
test "all_stylesheets_paths returns array of CSS asset paths" do
204+
paths = all_stylesheets_paths
205+
206+
assert_equal(
207+
[
208+
"goodbye.css",
209+
"hello_world.css",
210+
"library.css"
211+
],
212+
paths
213+
)
214+
end
215+
216+
test "app_stylesheets_paths returns array of app CSS asset paths" do
217+
paths = app_stylesheets_paths
218+
219+
assert_equal(
220+
[
221+
"goodbye.css",
222+
"hello_world.css"
223+
],
224+
paths
225+
)
226+
end
227+
228+
test "asset_integrity handles file extensions correctly" do
229+
integrity1 = asset_integrity("hello_world.css")
230+
231+
integrity2 = asset_integrity("hello_world", type: :stylesheet)
232+
233+
assert_equal integrity1, integrity2
234+
end
235+
236+
test "integrity option false explicitly disables integrity" do
237+
request.headers["HTTPS"] = "on"
238+
239+
result = stylesheet_link_tag("hello_world", integrity: false)
240+
241+
assert_dom_equal(<<~HTML, result)
242+
<link
243+
rel="stylesheet"
244+
href="/assets/hello_world-4137140a.css"
245+
/>
246+
HTML
247+
end
248+
249+
test "integrity option nil does not enable integrity" do
250+
request.headers["HTTPS"] = "on"
251+
252+
result = stylesheet_link_tag("hello_world", integrity: nil)
253+
254+
assert_dom_equal(<<~HTML, result)
255+
<link
256+
rel="stylesheet"
257+
href="/assets/hello_world-4137140a.css"
258+
/>
259+
HTML
260+
end
261+
end

0 commit comments

Comments
 (0)