Skip to content

Commit 5af1833

Browse files
committed
correct test cases for responsive images
1 parent 2e76262 commit 5af1833

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../test_helper"
4+
5+
class ResponsiveImageAttributesTest < Minitest::Test
6+
def setup
7+
@client = Imagekit::Client.new(
8+
private_key: "private_key_test",
9+
password: "test_password"
10+
)
11+
end
12+
13+
def test_bare_minimum_input
14+
result = @client.helper.get_responsive_image_attributes(
15+
src: "sample.jpg",
16+
url_endpoint: "https://ik.imagekit.io/demo"
17+
)
18+
19+
# Expected object based on default device_breakpoints and image_breakpoints
20+
expected = {
21+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
22+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/sample.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max 3840w",
23+
sizes: "100vw"
24+
}
25+
26+
assert_equal(expected, result)
27+
end
28+
29+
def test_sizes_provided_100vw
30+
result = @client.helper.get_responsive_image_attributes(
31+
src: "sample.jpg",
32+
url_endpoint: "https://ik.imagekit.io/demo",
33+
sizes: "100vw"
34+
)
35+
36+
# With a sizes value of "100vw", the function should use the same breakpoints as in the bare minimum case
37+
expected = {
38+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
39+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/sample.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max 3840w",
40+
sizes: "100vw"
41+
}
42+
43+
assert_equal(expected, result)
44+
end
45+
46+
def test_width_only_dpr_strategy
47+
result = @client.helper.get_responsive_image_attributes(
48+
src: "sample.jpg",
49+
url_endpoint: "https://ik.imagekit.io/demo",
50+
width: 400
51+
)
52+
53+
# When width is provided without sizes attribute, the DPR strategy should be used
54+
expected = {
55+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max",
56+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 2x",
57+
width: 400
58+
}
59+
60+
assert_equal(expected, result)
61+
end
62+
63+
def test_custom_breakpoints
64+
result = @client.helper.get_responsive_image_attributes(
65+
src: "sample.jpg",
66+
url_endpoint: "https://ik.imagekit.io/demo",
67+
device_breakpoints: [200, 400, 800],
68+
image_breakpoints: [100]
69+
)
70+
71+
# For custom breakpoints, the breakpoints will be derived from the provided arrays
72+
expected = {
73+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-800,c-at_max",
74+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-200,c-at_max 200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-400,c-at_max 400w, https://ik.imagekit.io/demo/sample.jpg?tr=w-800,c-at_max 800w",
75+
sizes: "100vw"
76+
}
77+
78+
assert_equal(expected, result)
79+
end
80+
81+
def test_preserves_caller_transformations
82+
result = @client.helper.get_responsive_image_attributes(
83+
src: "sample.jpg",
84+
url_endpoint: "https://ik.imagekit.io/demo",
85+
width: 500,
86+
transformation: [Imagekit::Models::Transformation.new(height: 300.0)]
87+
)
88+
89+
# The provided transformation should be preserved in the output
90+
expected = {
91+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:w-1080,c-at_max",
92+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=h-300:w-1080,c-at_max 2x",
93+
width: 500
94+
}
95+
96+
assert_equal(expected, result)
97+
end
98+
99+
def test_both_sizes_and_width_passed
100+
result = @client.helper.get_responsive_image_attributes(
101+
src: "sample.jpg",
102+
url_endpoint: "https://ik.imagekit.io/demo",
103+
sizes: "50vw",
104+
width: 600
105+
)
106+
107+
# Both sizes and width are provided, so the function should apply the sizes attribute while using width for DPR strategy
108+
expected = {
109+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
110+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/sample.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max 3840w",
111+
sizes: "50vw",
112+
width: 600
113+
}
114+
115+
assert_equal(expected, result)
116+
end
117+
118+
def test_multiple_transformations
119+
result = @client.helper.get_responsive_image_attributes(
120+
src: "sample.jpg",
121+
url_endpoint: "https://ik.imagekit.io/demo",
122+
width: 450,
123+
transformation: [
124+
Imagekit::Models::Transformation.new(height: 300.0),
125+
Imagekit::Models::Transformation.new(ai_remove_background: true)
126+
]
127+
)
128+
129+
# Multiple caller transformations should be combined appropriately
130+
expected = {
131+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max",
132+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max 2x",
133+
width: 450
134+
}
135+
136+
assert_equal(expected, result)
137+
end
138+
139+
def test_sizes_causes_breakpoint_pruning_33vw_path
140+
result = @client.helper.get_responsive_image_attributes(
141+
src: "sample.jpg",
142+
url_endpoint: "https://ik.imagekit.io/demo",
143+
sizes: "(min-width: 800px) 33vw, 100vw"
144+
)
145+
146+
# When specified with a sizes attribute that prunes breakpoints, the output should reflect the pruned values
147+
expected = {
148+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
149+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-256,c-at_max 256w, https://ik.imagekit.io/demo/sample.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/sample.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max 3840w",
150+
sizes: "(min-width: 800px) 33vw, 100vw"
151+
}
152+
153+
assert_equal(expected, result)
154+
end
155+
156+
def test_using_query_parameters_and_transformation_position
157+
result = @client.helper.get_responsive_image_attributes(
158+
src: "sample.jpg",
159+
url_endpoint: "https://ik.imagekit.io/demo",
160+
width: 450,
161+
transformation: [
162+
Imagekit::Models::Transformation.new(height: 300),
163+
Imagekit::Models::Transformation.new(ai_remove_background: true)
164+
],
165+
query_parameters: {key: "value"},
166+
transformation_position: :path
167+
)
168+
169+
# The function should respect the transformation position and query parameters
170+
expected = {
171+
src: "https://ik.imagekit.io/demo/tr:h-300:e-bgremove:w-1080,c-at_max/sample.jpg?key=value",
172+
src_set: "https://ik.imagekit.io/demo/tr:h-300:e-bgremove:w-640,c-at_max/sample.jpg?key=value 1x, https://ik.imagekit.io/demo/tr:h-300:e-bgremove:w-1080,c-at_max/sample.jpg?key=value 2x",
173+
width: 450
174+
}
175+
176+
assert_equal(expected, result)
177+
end
178+
179+
def test_fallback_when_no_usable_vw_tokens
180+
result = @client.helper.get_responsive_image_attributes(
181+
src: "sample.jpg",
182+
url_endpoint: "https://ik.imagekit.io/demo",
183+
sizes: "100%"
184+
)
185+
186+
# When sizes has no vw tokens, all breakpoints should be used
187+
expected = {
188+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
189+
src_set: "https://ik.imagekit.io/demo/sample.jpg?tr=w-16,c-at_max 16w, https://ik.imagekit.io/demo/sample.jpg?tr=w-32,c-at_max 32w, https://ik.imagekit.io/demo/sample.jpg?tr=w-48,c-at_max 48w, https://ik.imagekit.io/demo/sample.jpg?tr=w-64,c-at_max 64w, https://ik.imagekit.io/demo/sample.jpg?tr=w-96,c-at_max 96w, https://ik.imagekit.io/demo/sample.jpg?tr=w-128,c-at_max 128w, https://ik.imagekit.io/demo/sample.jpg?tr=w-256,c-at_max 256w, https://ik.imagekit.io/demo/sample.jpg?tr=w-384,c-at_max 384w, https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 640w, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 750w, https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max 828w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1080,c-at_max 1080w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max 1200w, https://ik.imagekit.io/demo/sample.jpg?tr=w-1920,c-at_max 1920w, https://ik.imagekit.io/demo/sample.jpg?tr=w-2048,c-at_max 2048w, https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max 3840w",
190+
sizes: "100%"
191+
}
192+
193+
assert_equal(expected, result)
194+
end
195+
end

0 commit comments

Comments
 (0)