|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +/* eslint-disable jest/no-conditional-expect */ |
| 8 | +import React from 'react' |
| 9 | +import {Helmet} from 'react-helmet' |
| 10 | +import DynamicImage from '@salesforce/retail-react-app/app/components/dynamic-image/index' |
| 11 | +import {Img} from '@salesforce/retail-react-app/app/components/shared/ui' |
| 12 | +import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils' |
| 13 | +import {isServer} from '@salesforce/retail-react-app/app/components/image/utils' |
| 14 | + |
| 15 | +jest.mock('@salesforce/retail-react-app/app/components/image/utils', () => ({ |
| 16 | + ...jest.requireActual('@salesforce/retail-react-app/app/components/image/utils'), |
| 17 | + isServer: jest.fn().mockReturnValue(true) |
| 18 | +})) |
| 19 | + |
| 20 | +const src = |
| 21 | + 'https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg' |
| 22 | +const imageProps = { |
| 23 | + alt: 'Ruffle Front V-Neck Cardigan, large', |
| 24 | + title: 'Ruffle Front V-Neck Cardigan' |
| 25 | +} |
| 26 | + |
| 27 | +describe('Dynamic Image Component', () => { |
| 28 | + test('renders an image without decoding strategy and fetch priority', () => { |
| 29 | + const {getAllByTitle} = renderWithProviders( |
| 30 | + <DynamicImage src={src} imageProps={imageProps} /> |
| 31 | + ) |
| 32 | + const elements = getAllByTitle(imageProps.title) |
| 33 | + expect(elements).toHaveLength(1) |
| 34 | + expect(elements[0]).not.toHaveAttribute('decoding') |
| 35 | + expect(elements[0]).not.toHaveAttribute('fetchpriority') |
| 36 | + }) |
| 37 | + |
| 38 | + describe('loading="lazy"', () => { |
| 39 | + test('renders an image using the default "async" decoding strategy', () => { |
| 40 | + const {getAllByTitle} = renderWithProviders( |
| 41 | + <DynamicImage |
| 42 | + src={src} |
| 43 | + imageProps={{ |
| 44 | + ...imageProps, |
| 45 | + loading: 'lazy' |
| 46 | + }} |
| 47 | + /> |
| 48 | + ) |
| 49 | + const elements = getAllByTitle(imageProps.title) |
| 50 | + expect(elements).toHaveLength(1) |
| 51 | + expect(elements[0]).toHaveAttribute('decoding', 'async') |
| 52 | + }) |
| 53 | + |
| 54 | + test.each(['sync', 'async', 'auto'])( |
| 55 | + 'renders an image using an explicit "%s" decoding strategy', |
| 56 | + (decoding) => { |
| 57 | + const {getAllByTitle} = renderWithProviders( |
| 58 | + <DynamicImage |
| 59 | + src={src} |
| 60 | + imageProps={{ |
| 61 | + ...imageProps, |
| 62 | + loading: 'lazy', |
| 63 | + decoding |
| 64 | + }} |
| 65 | + /> |
| 66 | + ) |
| 67 | + const elements = getAllByTitle(imageProps.title) |
| 68 | + expect(elements).toHaveLength(1) |
| 69 | + expect(elements[0]).toHaveAttribute('decoding', decoding) |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | + test('renders an image replacing an invalid decoding strategy with the default "async" value', () => { |
| 74 | + const {getAllByTitle} = renderWithProviders( |
| 75 | + <DynamicImage |
| 76 | + src={src} |
| 77 | + imageProps={{ |
| 78 | + ...imageProps, |
| 79 | + loading: 'lazy', |
| 80 | + decoding: 'invalid' |
| 81 | + }} |
| 82 | + /> |
| 83 | + ) |
| 84 | + const elements = getAllByTitle(imageProps.title) |
| 85 | + expect(elements).toHaveLength(1) |
| 86 | + expect(elements[0]).toHaveAttribute('decoding', 'async') |
| 87 | + }) |
| 88 | + |
| 89 | + test('renders an explicitly given image component without attribute modifications', () => { |
| 90 | + const {getAllByTitle} = renderWithProviders( |
| 91 | + <DynamicImage |
| 92 | + src={src} |
| 93 | + as={Img} |
| 94 | + imageProps={{ |
| 95 | + ...imageProps, |
| 96 | + loading: 'lazy' |
| 97 | + }} |
| 98 | + /> |
| 99 | + ) |
| 100 | + const elements = getAllByTitle(imageProps.title) |
| 101 | + expect(elements).toHaveLength(1) |
| 102 | + expect(elements[0]).not.toHaveAttribute('decoding') |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe('loading="eager"', () => { |
| 107 | + test('renders an image using the default "high" fetch priority', () => { |
| 108 | + const {getAllByTitle} = renderWithProviders( |
| 109 | + <DynamicImage |
| 110 | + src={src} |
| 111 | + imageProps={{ |
| 112 | + ...imageProps, |
| 113 | + loading: 'eager' |
| 114 | + }} |
| 115 | + widths={['50vw', '50vw', '20vw', '20vw', '25vw']} |
| 116 | + /> |
| 117 | + ) |
| 118 | + const elements = getAllByTitle(imageProps.title) |
| 119 | + expect(elements).toHaveLength(1) |
| 120 | + expect(elements[0]).toHaveAttribute('fetchpriority', 'high') |
| 121 | + |
| 122 | + const helmet = Helmet.peek() |
| 123 | + expect(helmet.linkTags).toHaveLength(1) |
| 124 | + expect(helmet.linkTags[0]).toStrictEqual({ |
| 125 | + as: 'image', |
| 126 | + href: src, |
| 127 | + rel: 'preload', |
| 128 | + imageSizes: |
| 129 | + '(min-width: 80em) 25vw, (min-width: 62em) 20vw, (min-width: 48em) 20vw, (min-width: 30em) 50vw, 50vw', |
| 130 | + imageSrcSet: |
| 131 | + 'https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 198w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 396w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 240w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 480w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 256w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 512w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 384w, https://edge.disstg.commercecloud.salesforce.com/dw/image/v2/ZZRF_001/on/demandware.static/-/Sites-apparel-m-catalog/default/dw4cd0a798/images/large/PG.10216885.JJ169XX.PZ.jpg 768w' |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + test.each(['high', 'low', 'auto'])( |
| 136 | + 'renders an image using an explicit "%s" fetch priority', |
| 137 | + (fetchPriority) => { |
| 138 | + const {getAllByTitle} = renderWithProviders( |
| 139 | + <DynamicImage |
| 140 | + src={src} |
| 141 | + imageProps={{ |
| 142 | + ...imageProps, |
| 143 | + loading: 'eager', |
| 144 | + fetchPriority |
| 145 | + }} |
| 146 | + /> |
| 147 | + ) |
| 148 | + const elements = getAllByTitle(imageProps.title) |
| 149 | + expect(elements).toHaveLength(1) |
| 150 | + expect(elements[0]).toHaveAttribute('fetchpriority', fetchPriority) |
| 151 | + |
| 152 | + const helmet = Helmet.peek() |
| 153 | + if (fetchPriority === 'high') { |
| 154 | + expect(helmet.linkTags).toHaveLength(1) |
| 155 | + expect(helmet.linkTags[0]).toStrictEqual({ |
| 156 | + as: 'image', |
| 157 | + href: src, |
| 158 | + rel: 'preload' |
| 159 | + }) |
| 160 | + } else { |
| 161 | + expect(helmet.linkTags).toStrictEqual([]) |
| 162 | + } |
| 163 | + } |
| 164 | + ) |
| 165 | + |
| 166 | + test('renders an image replacing an invalid fetch priority with the default "auto" value', () => { |
| 167 | + const {getAllByTitle} = renderWithProviders( |
| 168 | + <DynamicImage |
| 169 | + src={src} |
| 170 | + imageProps={{ |
| 171 | + ...imageProps, |
| 172 | + loading: 'eager', |
| 173 | + fetchPriority: 'invalid' |
| 174 | + }} |
| 175 | + /> |
| 176 | + ) |
| 177 | + const elements = getAllByTitle(imageProps.title) |
| 178 | + expect(elements).toHaveLength(1) |
| 179 | + expect(elements[0]).toHaveAttribute('fetchpriority', 'auto') |
| 180 | + expect(Helmet.peek().linkTags).toStrictEqual([]) |
| 181 | + }) |
| 182 | + |
| 183 | + test('renders an explicitly given image component without modifications', () => { |
| 184 | + const {getAllByTitle} = renderWithProviders( |
| 185 | + <DynamicImage |
| 186 | + src={src} |
| 187 | + as={Img} |
| 188 | + imageProps={{ |
| 189 | + ...imageProps, |
| 190 | + loading: 'eager' |
| 191 | + }} |
| 192 | + /> |
| 193 | + ) |
| 194 | + const elements = getAllByTitle(imageProps.title) |
| 195 | + expect(elements).toHaveLength(1) |
| 196 | + expect(elements[0]).not.toHaveAttribute('fetchpriority') |
| 197 | + expect(Helmet.peek().linkTags).toStrictEqual([]) |
| 198 | + }) |
| 199 | + |
| 200 | + test('renders an image on the client', () => { |
| 201 | + isServer.mockReturnValue(false) |
| 202 | + const {getAllByTitle} = renderWithProviders( |
| 203 | + <DynamicImage |
| 204 | + src={src} |
| 205 | + imageProps={{ |
| 206 | + ...imageProps, |
| 207 | + loading: 'eager' |
| 208 | + }} |
| 209 | + /> |
| 210 | + ) |
| 211 | + const elements = getAllByTitle(imageProps.title) |
| 212 | + expect(elements).toHaveLength(1) |
| 213 | + expect(elements[0]).toHaveAttribute('fetchpriority', 'high') |
| 214 | + expect(Helmet.peek().linkTags).toStrictEqual([]) |
| 215 | + }) |
| 216 | + }) |
| 217 | +}) |
0 commit comments