|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { getResponsiveImageAttributes } = require('../src/getResponsiveImageAttributes'); |
| 3 | + |
| 4 | +describe.only('getResponsiveImageAttributes – smoke run‑through', () => { |
| 5 | + it('bare minimum input', () => { |
| 6 | + const out = getResponsiveImageAttributes({ |
| 7 | + src: 'sample.jpg', |
| 8 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 9 | + }); |
| 10 | + // Expected object based on default deviceSizes and imageSizes: |
| 11 | + expect(out).to.deep.equal({ |
| 12 | + src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max", |
| 13 | + 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 |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('sizes provided (100vw)', () => { |
| 20 | + const out = getResponsiveImageAttributes({ |
| 21 | + src: 'sample.jpg', |
| 22 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 23 | + sizes: '100vw', |
| 24 | + }); |
| 25 | + // With a sizes value of "100vw", the function should use the same breakpoints as in the bare minimum case. |
| 26 | + expect(out).to.deep.equal({ |
| 27 | + src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-3840,c-at_max", |
| 28 | + 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 |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('width only – DPR strategy', () => { |
| 35 | + const out = getResponsiveImageAttributes({ |
| 36 | + src: 'sample.jpg', |
| 37 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 38 | + width: 400, |
| 39 | + }); |
| 40 | + // When width is provided without sizes attribute, the DPR strategy should be used. |
| 41 | + 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, |
| 45 | + width: 400 |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('custom breakpoints', () => { |
| 50 | + const out = getResponsiveImageAttributes({ |
| 51 | + src: 'sample.jpg', |
| 52 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 53 | + deviceSizes: [200, 400, 800], |
| 54 | + imageSizes: [100], |
| 55 | + }); |
| 56 | + // For custom breakpoints, the breakpoints will be derived from the provided arrays. |
| 57 | + expect(out).to.deep.equal({ |
| 58 | + src: "https://ik.imagekit.io/demo/sample.jpg?tr=w-800,c-at_max", |
| 59 | + 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 |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('preserves caller transformations', () => { |
| 66 | + const out = getResponsiveImageAttributes({ |
| 67 | + src: 'sample.jpg', |
| 68 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 69 | + width: 500, |
| 70 | + transformation: [{ height: 300 }], |
| 71 | + }); |
| 72 | + // The provided transformation should be preserved in the output. |
| 73 | + 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, |
| 77 | + width: 500 |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('both sizes and width passed', () => { |
| 82 | + const out = getResponsiveImageAttributes({ |
| 83 | + src: 'sample.jpg', |
| 84 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 85 | + sizes: '50vw', |
| 86 | + width: 600, |
| 87 | + }); |
| 88 | + // Both sizes and width are provided, so the function should apply the sizes attribute while using width for DPR strategy. |
| 89 | + 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", |
| 92 | + sizes: "50vw", |
| 93 | + width: 600 |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('multiple transformations', () => { |
| 98 | + const out = getResponsiveImageAttributes({ |
| 99 | + src: 'sample.jpg', |
| 100 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 101 | + width: 450, |
| 102 | + transformation: [ |
| 103 | + { height: 300 }, |
| 104 | + { aiRemoveBackground: true } |
| 105 | + ] |
| 106 | + }); |
| 107 | + // Multiple caller transformations should be combined appropriately. |
| 108 | + 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, |
| 112 | + width: 450 |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('sizes causes breakpoint pruning (33vw path)', () => { |
| 117 | + const out = getResponsiveImageAttributes({ |
| 118 | + src: 'sample.jpg', |
| 119 | + urlEndpoint: 'https://ik.imagekit.io/demo', |
| 120 | + sizes: '(min-width: 800px) 33vw, 100vw', |
| 121 | + }); |
| 122 | + // When specified with a sizes attribute that prunes breakpoints, the output should reflect the pruned values. |
| 123 | + expect(out).to.deep.equal({ |
| 124 | + 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 |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments