Skip to content

Commit fab9156

Browse files
committed
feat(tests): add tests to ensure integer descriptors in srcset for responsive image attributes
1 parent f7a8f2d commit fab9156

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lib/imagekit/helpers/helper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,18 @@ def get_responsive_image_attributes(options)
303303

304304
# Build srcset
305305
src_set_entries = candidates.map.with_index do |w, i|
306-
descriptor = descriptor_kind == :w ? "#{w}w" : "#{i + 1}x"
307-
"#{build_url_fn.call(w)} #{descriptor}"
306+
# Ensure width is an integer for proper descriptor format (e.g., "640w" not "640.0w")
307+
width_int = w.to_i
308+
descriptor = descriptor_kind == :w ? "#{width_int}w" : "#{i + 1}x"
309+
"#{build_url_fn.call(width_int)} #{descriptor}"
308310
end
309311
src_set = src_set_entries.empty? ? nil : src_set_entries.join(", ")
310312

311313
final_sizes = sizes || (descriptor_kind == :w ? "100vw" : nil)
312314

313315
# Build and return ResponsiveImageAttributes model
314316
Imagekit::Models::ResponsiveImageAttributes.new(
315-
src: build_url_fn.call(candidates.last), # largest candidate
317+
src: build_url_fn.call(candidates.last.to_i), # largest candidate as integer
316318
src_set: src_set,
317319
sizes: final_sizes,
318320
width: width

test/imagekit/custom-tests/responsive_image_attributes_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,56 @@ def test_should_work_with_plain_hashes_for_responsive_image_attributes
214214
assert_equal(expected, result.to_h)
215215
end
216216

217+
# Test to verify that srcset uses integer descriptors (640w) not float (640.0w)
218+
# This tests both model object and hash approaches
219+
def test_should_use_integer_descriptors_in_srcset_with_model_object
220+
# Using GetImageAttributesOptions model with Float array (as defined in schema)
221+
result = @client.helper.get_responsive_image_attributes(
222+
Imagekit::Models::GetImageAttributesOptions.new(
223+
src: "sample.jpg",
224+
url_endpoint: "https://ik.imagekit.io/demo",
225+
device_breakpoints: [320.0, 640.0, 1280.0], # SDK converts to floats
226+
image_breakpoints: []
227+
)
228+
)
229+
230+
src_set = result.src_set
231+
232+
# Should have integer descriptors like "320w", NOT "320.0w"
233+
assert_includes(src_set, "320w")
234+
assert_includes(src_set, "640w")
235+
assert_includes(src_set, "1280w")
236+
237+
# Should NOT have float descriptors
238+
refute_includes(src_set, "320.0w")
239+
refute_includes(src_set, "640.0w")
240+
refute_includes(src_set, "1280.0w")
241+
end
242+
243+
def test_should_use_integer_descriptors_in_srcset_with_hash
244+
# Using plain hash with integer breakpoints
245+
result = @client.helper.get_responsive_image_attributes(
246+
{
247+
src: "sample.jpg",
248+
url_endpoint: "https://ik.imagekit.io/demo",
249+
device_breakpoints: [320, 640, 1280],
250+
image_breakpoints: []
251+
}
252+
)
253+
254+
src_set = result.src_set
255+
256+
# Should have integer descriptors like "320w", NOT "320.0w"
257+
assert_includes(src_set, "320w")
258+
assert_includes(src_set, "640w")
259+
assert_includes(src_set, "1280w")
260+
261+
# Should NOT have float descriptors
262+
refute_includes(src_set, "320.0w")
263+
refute_includes(src_set, "640.0w")
264+
refute_includes(src_set, "1280.0w")
265+
end
266+
217267
def test_fallback_when_no_usable_vw_tokens
218268
result = @client.helper.get_responsive_image_attributes(
219269
src: "sample.jpg",

0 commit comments

Comments
 (0)