Skip to content

Commit e37df85

Browse files
committed
test: use generated models. update test suite
1 parent 08560d4 commit e37df85

File tree

2 files changed

+56
-61
lines changed

2 files changed

+56
-61
lines changed

lib/imagekit/helpers/helper.rb

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -246,42 +246,28 @@ def get_authentication_parameters(token: nil, expire: nil)
246246
# - DPR-based (x descriptors): When width is provided without sizes
247247
# - Fallback (w descriptors): Uses device breakpoints when neither is provided
248248
#
249-
# @param options [Hash] Options for generating responsive image attributes
250-
# @option options [String] :src Required. The relative or absolute path of the image
251-
# @option options [String] :url_endpoint Required. Your ImageKit URL endpoint
252-
# @option options [Integer] :width The intended display width in pixels, used only when sizes is not provided.
253-
# Triggers a DPR-based strategy (1x and 2x variants) and generates x descriptors in srcSet. Ignored if sizes is present.
254-
# @option options [String] :sizes The value for the HTML sizes attribute (e.g., "100vw" or "(min-width:768px) 50vw, 100vw").
255-
# If it includes one or more vw units, breakpoints smaller than the corresponding percentage of the smallest device width are excluded.
256-
# If it contains no vw units, the full breakpoint list is used. Enables a width-based strategy and generates w descriptors in srcSet.
257-
# @option options [Array<Integer>] :device_breakpoints Custom list of device-width breakpoints in pixels.
258-
# These define common screen widths for responsive image generation. Defaults to [640, 750, 828, 1080, 1200, 1920, 2048, 3840]. Sorted automatically.
259-
# @option options [Array<Integer>] :image_breakpoints Custom list of image-specific breakpoints in pixels.
260-
# Useful for generating small variants (e.g., placeholders or thumbnails). Merged with device_breakpoints before calculating srcSet.
261-
# Defaults to [16, 32, 48, 64, 96, 128, 256, 384]. Sorted automatically.
262-
# @option options [Array<Hash>] :transformation Array of transformation objects to apply
263-
# @option options [Symbol] :transformation_position Where to add transformations (:path or :query)
264-
# @option options [Hash] :query_parameters Additional query parameters to add to URLs
265-
# @return [Hash] Hash containing responsive image attributes suitable for an HTML <img> element:
266-
# - :src - URL for the largest candidate (assigned to plain src)
267-
# - :src_set - Candidate set with w or x descriptors (if generated)
268-
# - :sizes - sizes attribute value (returned or synthesized as "100vw")
269-
# - :width - Width as a number (if width was provided)
270-
def get_responsive_image_attributes(options = {})
249+
# @param options [Imagekit::Models::GetImageAttributesOptions] Options for generating responsive image attributes
250+
# @return [Imagekit::Models::ResponsiveImageAttributes] Responsive image attributes suitable for an HTML <img> element
251+
def get_responsive_image_attributes(options)
252+
# Convert model to hash for easier access
253+
opts = options.is_a?(Imagekit::Internal::Type::BaseModel) ? options.to_h : options
254+
271255
# Default breakpoint pools
272256
default_device_breakpoints = [640, 750, 828, 1080, 1200, 1920, 2048, 3840]
273257
default_image_breakpoints = [16, 32, 48, 64, 96, 128, 256, 384]
274258

275259
# Extract options
276-
src = options[:src]
277-
url_endpoint = options[:url_endpoint]
278-
width = options[:width]
279-
sizes = options[:sizes]
280-
device_breakpoints = options[:device_breakpoints] || default_device_breakpoints
281-
image_breakpoints = options[:image_breakpoints] || default_image_breakpoints
282-
transformation = options[:transformation] || []
283-
transformation_position = options[:transformation_position]
284-
query_parameters = options[:query_parameters]
260+
src = opts[:src]
261+
url_endpoint = opts[:url_endpoint]
262+
width = opts[:width]
263+
sizes = opts[:sizes]
264+
device_breakpoints = opts[:device_breakpoints] || default_device_breakpoints
265+
image_breakpoints = opts[:image_breakpoints] || default_image_breakpoints
266+
transformation = opts[:transformation] || []
267+
transformation_position = opts[:transformation_position]
268+
query_parameters = opts[:query_parameters]
269+
expires_in = opts[:expires_in]
270+
signed = opts[:signed]
285271

286272
# Sort and merge breakpoints
287273
sorted_device_breakpoints = device_breakpoints.sort
@@ -306,6 +292,8 @@ def get_responsive_image_attributes(options = {})
306292
url_endpoint: url_endpoint,
307293
query_parameters: query_parameters,
308294
transformation_position: transformation_position,
295+
expires_in: expires_in,
296+
signed: signed,
309297
transformation: transformation + [
310298
Imagekit::Models::Transformation.new(width: w, crop: "at_max") # never upscale beyond original
311299
]
@@ -322,15 +310,13 @@ def get_responsive_image_attributes(options = {})
322310

323311
final_sizes = sizes || (descriptor_kind == :w ? "100vw" : nil)
324312

325-
# Build result - include only when defined
326-
result = {
327-
src: build_url_fn.call(candidates.last) # largest candidate
328-
}
329-
result[:src_set] = src_set if src_set
330-
result[:sizes] = final_sizes if final_sizes
331-
result[:width] = width if width
332-
333-
result
313+
# Build and return ResponsiveImageAttributes model
314+
Imagekit::Models::ResponsiveImageAttributes.new(
315+
src: build_url_fn.call(candidates.last), # largest candidate
316+
src_set: src_set,
317+
sizes: final_sizes,
318+
width: width
319+
)
334320
end
335321

336322
# @api private

test/imagekit/custom-tests/url-generation/responsive_image_attributes_test.rb renamed to test/imagekit/custom-tests/responsive_image_attributes_test.rb

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative "../../test_helper"
3+
require_relative "../test_helper"
44

55
class ResponsiveImageAttributesTest < Minitest::Test
66
def setup
@@ -20,10 +20,11 @@ def test_bare_minimum_input
2020
expected = {
2121
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
2222
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"
23+
sizes: "100vw",
24+
width: nil
2425
}
2526

26-
assert_equal(expected, result)
27+
assert_equal(expected, result.to_h)
2728
end
2829

2930
def test_sizes_provided_100vw
@@ -37,10 +38,11 @@ def test_sizes_provided_100vw
3738
expected = {
3839
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
3940
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+
sizes: "100vw",
42+
width: nil
4143
}
4244

43-
assert_equal(expected, result)
45+
assert_equal(expected, result.to_h)
4446
end
4547

4648
def test_width_only_dpr_strategy
@@ -54,10 +56,11 @@ def test_width_only_dpr_strategy
5456
expected = {
5557
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max",
5658
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
59+
sizes: nil,
60+
width: 400.0
5861
}
5962

60-
assert_equal(expected, result)
63+
assert_equal(expected, result.to_h)
6164
end
6265

6366
def test_custom_breakpoints
@@ -72,10 +75,11 @@ def test_custom_breakpoints
7275
expected = {
7376
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-800,c-at_max",
7477
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"
78+
sizes: "100vw",
79+
width: nil
7680
}
7781

78-
assert_equal(expected, result)
82+
assert_equal(expected, result.to_h)
7983
end
8084

8185
def test_preserves_caller_transformations
@@ -90,10 +94,11 @@ def test_preserves_caller_transformations
9094
expected = {
9195
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:w-1080,c-at_max",
9296
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
97+
sizes: nil,
98+
width: 500.0
9499
}
95100

96-
assert_equal(expected, result)
101+
assert_equal(expected, result.to_h)
97102
end
98103

99104
def test_both_sizes_and_width_passed
@@ -109,10 +114,10 @@ def test_both_sizes_and_width_passed
109114
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
110115
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",
111116
sizes: "50vw",
112-
width: 600
117+
width: 600.0
113118
}
114119

115-
assert_equal(expected, result)
120+
assert_equal(expected, result.to_h)
116121
end
117122

118123
def test_multiple_transformations
@@ -130,10 +135,11 @@ def test_multiple_transformations
130135
expected = {
131136
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max",
132137
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
138+
sizes: nil,
139+
width: 450.0
134140
}
135141

136-
assert_equal(expected, result)
142+
assert_equal(expected, result.to_h)
137143
end
138144

139145
def test_sizes_causes_breakpoint_pruning_33vw_path
@@ -147,10 +153,11 @@ def test_sizes_causes_breakpoint_pruning_33vw_path
147153
expected = {
148154
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
149155
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"
156+
sizes: "(min-width: 800px) 33vw, 100vw",
157+
width: nil
151158
}
152159

153-
assert_equal(expected, result)
160+
assert_equal(expected, result.to_h)
154161
end
155162

156163
def test_using_query_parameters_and_transformation_position
@@ -170,10 +177,11 @@ def test_using_query_parameters_and_transformation_position
170177
expected = {
171178
src: "https://ik.imagekit.io/demo/tr:h-300:e-bgremove:w-1080,c-at_max/sample.jpg?key=value",
172179
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
180+
sizes: nil,
181+
width: 450.0
174182
}
175183

176-
assert_equal(expected, result)
184+
assert_equal(expected, result.to_h)
177185
end
178186

179187
def test_fallback_when_no_usable_vw_tokens
@@ -187,9 +195,10 @@ def test_fallback_when_no_usable_vw_tokens
187195
expected = {
188196
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
189197
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%"
198+
sizes: "100%",
199+
width: nil
191200
}
192201

193-
assert_equal(expected, result)
202+
assert_equal(expected, result.to_h)
194203
end
195204
end

0 commit comments

Comments
 (0)