Skip to content

Commit b7a5758

Browse files
committed
fix test cases
1 parent 83d38b1 commit b7a5758

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/getResponsiveImageAttributes.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { buildSrc } from './url'
33

44
/* Default break‑point pools */
55
const DEFAULT_DEVICE_BREAKPOINTS = [640, 750, 828, 1080, 1200, 1920, 2048, 3840] as const
6-
const DEFAULT_IMAGE_BREAKPOINTS = [16, 32, 48, 64, 96, 128, 256, 320, 420] as const
6+
const DEFAULT_IMAGE_BREAKPOINTS = [16, 32, 48, 64, 96, 128, 256, 384] as const
77

88
export interface GetImageAttributesOptions extends SrcOptions {
99
width?: number // explicit rendered width
@@ -62,11 +62,13 @@ export function getResponsiveImageAttributes(
6262
.map((w, i) => `${buildURL(w)} ${descriptorKind === 'w' ? w : i + 1}${descriptorKind}`)
6363
.join(', ') || undefined
6464

65+
const finalSizes = sizes ?? (descriptorKind === 'w' ? '100vw' : undefined)
66+
6567
return {
6668
src: buildURL(candidates[candidates.length - 1]), // largest candidate
6769
srcSet,
68-
sizes: sizes ?? (descriptorKind === 'w' ? '100vw' : undefined),
69-
width,
70+
...(finalSizes ? { sizes: finalSizes } : {}), // include only when defined
71+
...(width !== undefined ? { width } : {}), // include only when defined
7072
}
7173
}
7274

test/getResponsiveImageAttributes.test.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
const { expect } = require('chai');
22
const { getResponsiveImageAttributes } = require('../src/getResponsiveImageAttributes');
33

4-
describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
4+
describe.only('getResponsiveImageAttributes', () => {
55
it('bare minimum input', () => {
66
const out = getResponsiveImageAttributes({
77
src: 'sample.jpg',
88
urlEndpoint: 'https://ik.imagekit.io/demo',
99
});
10-
// Expected object based on default deviceSizes and imageSizes:
10+
// Expected object based on default deviceBreakpoints and imageBreakpoints:
1111
expect(out).to.deep.equal({
1212
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
1313
srcSet: "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",
14-
sizes: "100vw",
15-
width: undefined
14+
sizes: "100vw"
1615
});
1716
});
1817

@@ -26,8 +25,7 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
2625
expect(out).to.deep.equal({
2726
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
2827
srcSet: "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",
29-
sizes: "100vw",
30-
width: undefined
28+
sizes: "100vw"
3129
});
3230
});
3331

@@ -39,9 +37,8 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
3937
});
4038
// When width is provided without sizes attribute, the DPR strategy should be used.
4139
expect(out).to.deep.equal({
42-
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max",
43-
srcSet: "https://ik.imagekit.io/demo/sample.jpg?tr=w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=w-750,c-at_max 2x",
44-
sizes: undefined,
40+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-828,c-at_max",
41+
srcSet: "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",
4542
width: 400
4643
});
4744
});
@@ -50,15 +47,14 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
5047
const out = getResponsiveImageAttributes({
5148
src: 'sample.jpg',
5249
urlEndpoint: 'https://ik.imagekit.io/demo',
53-
deviceSizes: [200, 400, 800],
54-
imageSizes: [100],
50+
deviceBreakpoints: [200, 400, 800],
51+
imageBreakpoints: [100],
5552
});
5653
// For custom breakpoints, the breakpoints will be derived from the provided arrays.
5754
expect(out).to.deep.equal({
5855
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-800,c-at_max",
5956
srcSet: "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",
60-
sizes: "100vw",
61-
width: undefined
57+
sizes: "100vw"
6258
});
6359
});
6460

@@ -71,9 +67,8 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
7167
});
7268
// The provided transformation should be preserved in the output.
7369
expect(out).to.deep.equal({
74-
src: "https://ik.imagekit.io/demo/sample.jpg?tr=height-300,w-1200,c-at_max",
75-
srcSet: "https://ik.imagekit.io/demo/sample.jpg?tr=height-300,w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=height-300,w-1200,c-at_max 2x",
76-
sizes: undefined,
70+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:w-1080,c-at_max",
71+
srcSet: "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",
7772
width: 500
7873
});
7974
});
@@ -87,8 +82,8 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
8782
});
8883
// Both sizes and width are provided, so the function should apply the sizes attribute while using width for DPR strategy.
8984
expect(out).to.deep.equal({
90-
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-1200,c-at_max",
91-
srcSet: "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",
85+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
86+
srcSet: "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",
9287
sizes: "50vw",
9388
width: 600
9489
});
@@ -106,9 +101,8 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
106101
});
107102
// Multiple caller transformations should be combined appropriately.
108103
expect(out).to.deep.equal({
109-
src: "https://ik.imagekit.io/demo/sample.jpg?tr=height-300,aiRemoveBackground-true,w-828,c-at_max",
110-
srcSet: "https://ik.imagekit.io/demo/sample.jpg?tr=height-300,aiRemoveBackground-true,w-640,c-at_max 1x, https://ik.imagekit.io/demo/sample.jpg?tr=height-300,aiRemoveBackground-true,w-828,c-at_max 2x",
111-
sizes: undefined,
104+
src: "https://ik.imagekit.io/demo/sample.jpg?tr=h-300:e-bgremove:w-1080,c-at_max",
105+
srcSet: "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",
112106
width: 450
113107
});
114108
});
@@ -122,9 +116,8 @@ describe.only('getResponsiveImageAttributes – smoke run‑through', () => {
122116
// When specified with a sizes attribute that prunes breakpoints, the output should reflect the pruned values.
123117
expect(out).to.deep.equal({
124118
src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max",
125-
srcSet: "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",
126-
sizes: "(min-width: 800px) 33vw, 100vw",
127-
width: undefined
119+
srcSet: "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",
120+
sizes: "(min-width: 800px) 33vw, 100vw"
128121
});
129122
});
130123
});

0 commit comments

Comments
 (0)