|
| 1 | +import React from 'react'; |
1 | 2 | import 'contentElements/inlineImage/frontend'; |
2 | 3 | import {renderContentElement, usePageObjects} from 'support/pageObjects'; |
| 4 | +import {renderInContentElement} from 'pageflow-scrolled/testHelpers'; |
3 | 5 | import '@testing-library/jest-dom/extend-expect' |
4 | 6 |
|
| 7 | +import {InlineImage} from 'contentElements/inlineImage/InlineImage'; |
| 8 | +import {features} from 'pageflow/frontend'; |
5 | 9 | import {usePortraitOrientation} from 'frontend/usePortraitOrientation'; |
6 | 10 | jest.mock('frontend/usePortraitOrientation'); |
7 | 11 |
|
@@ -190,6 +194,118 @@ describe('InlineImage', () => { |
190 | 194 | }); |
191 | 195 | }); |
192 | 196 |
|
| 197 | + describe('srcset', () => { |
| 198 | + beforeEach(() => features.enable('frontend', ['image_srcset'])); |
| 199 | + afterEach(() => features.enabledFeatureNames = []); |
| 200 | + |
| 201 | + function renderInlineImage({contentElementWidth = 0, ...seedOptions} = {}) { |
| 202 | + const result = renderInContentElement( |
| 203 | + <InlineImage contentElementId={42} |
| 204 | + contentElementWidth={contentElementWidth} |
| 205 | + configuration={{id: 100}} />, |
| 206 | + {seed: seedOptions} |
| 207 | + ); |
| 208 | + result.simulateScrollPosition('near viewport'); |
| 209 | + return result; |
| 210 | + } |
| 211 | + |
| 212 | + it('uses medium and large srcset for default width', () => { |
| 213 | + const {getByRole} = renderInlineImage({ |
| 214 | + imageFileUrlTemplates: { |
| 215 | + medium: ':id_partition/medium/image.jpg', |
| 216 | + large: ':id_partition/large/image.jpg' |
| 217 | + }, |
| 218 | + imageFiles: [{permaId: 100, id: 1, width: 4000, height: 3000}] |
| 219 | + }); |
| 220 | + |
| 221 | + expect(getByRole('img')).toHaveAttribute('srcset', |
| 222 | + '000/000/001/medium/image.jpg 1024w, 000/000/001/large/image.jpg 1920w'); |
| 223 | + expect(getByRole('img')).toHaveAttribute('sizes', |
| 224 | + '(min-width: 950px) 950px, 100vw'); |
| 225 | + }); |
| 226 | + |
| 227 | + it('uses medium, large and ultra srcset for full width', () => { |
| 228 | + const {getByRole} = renderInlineImage({ |
| 229 | + contentElementWidth: 3, |
| 230 | + imageFileUrlTemplates: { |
| 231 | + medium: ':id_partition/medium/image.jpg', |
| 232 | + large: ':id_partition/large/image.jpg', |
| 233 | + ultra: ':id_partition/ultra/image.jpg' |
| 234 | + }, |
| 235 | + imageFiles: [{permaId: 100, id: 1, width: 4000, height: 3000}] |
| 236 | + }); |
| 237 | + |
| 238 | + expect(getByRole('img')).toHaveAttribute('srcset', |
| 239 | + '000/000/001/medium/image.jpg 1024w, ' + |
| 240 | + '000/000/001/large/image.jpg 1920w, ' + |
| 241 | + '000/000/001/ultra/image.jpg 3840w'); |
| 242 | + expect(getByRole('img')).toHaveAttribute('sizes', '100vw'); |
| 243 | + }); |
| 244 | + |
| 245 | + it('uses medium, large and ultra srcset with 1200px sizes for xl width', () => { |
| 246 | + const {getByRole} = renderInlineImage({ |
| 247 | + contentElementWidth: 2, |
| 248 | + imageFileUrlTemplates: { |
| 249 | + medium: ':id_partition/medium/image.jpg', |
| 250 | + large: ':id_partition/large/image.jpg', |
| 251 | + ultra: ':id_partition/ultra/image.jpg' |
| 252 | + }, |
| 253 | + imageFiles: [{permaId: 100, id: 1, width: 4000, height: 3000}] |
| 254 | + }); |
| 255 | + |
| 256 | + expect(getByRole('img')).toHaveAttribute('srcset', |
| 257 | + '000/000/001/medium/image.jpg 1024w, ' + |
| 258 | + '000/000/001/large/image.jpg 1920w, ' + |
| 259 | + '000/000/001/ultra/image.jpg 3840w'); |
| 260 | + expect(getByRole('img')).toHaveAttribute('sizes', |
| 261 | + '(min-width: 950px) 1200px, 100vw'); |
| 262 | + }); |
| 263 | + |
| 264 | + it('uses computed width descriptors for portrait images', () => { |
| 265 | + const {getByRole} = renderInlineImage({ |
| 266 | + imageFileUrlTemplates: { |
| 267 | + medium: ':id_partition/medium/image.jpg', |
| 268 | + large: ':id_partition/large/image.jpg' |
| 269 | + }, |
| 270 | + imageFiles: [{permaId: 100, id: 1, width: 2160, height: 3840}] |
| 271 | + }); |
| 272 | + |
| 273 | + expect(getByRole('img')).toHaveAttribute('srcset', |
| 274 | + '000/000/001/medium/image.jpg 576w, 000/000/001/large/image.jpg 1080w'); |
| 275 | + }); |
| 276 | + |
| 277 | + it('uses plain medium variant for small widths', () => { |
| 278 | + const {getByRole} = renderInlineImage({ |
| 279 | + contentElementWidth: -1, |
| 280 | + imageFileUrlTemplates: { |
| 281 | + medium: ':id_partition/medium/image.jpg', |
| 282 | + large: ':id_partition/large/image.jpg' |
| 283 | + }, |
| 284 | + imageFiles: [{permaId: 100, id: 1, width: 200, height: 100}] |
| 285 | + }); |
| 286 | + |
| 287 | + expect(getByRole('img')).not.toHaveAttribute('srcset'); |
| 288 | + expect(getByRole('img')).toHaveAttribute('src', |
| 289 | + '000/000/001/medium/image.jpg'); |
| 290 | + }); |
| 291 | + |
| 292 | + it('falls back to original behavior when feature is disabled', () => { |
| 293 | + features.enabledFeatureNames = []; |
| 294 | + |
| 295 | + const {getByRole} = renderInlineImage({ |
| 296 | + imageFileUrlTemplates: { |
| 297 | + medium: ':id_partition/medium/image.jpg', |
| 298 | + large: ':id_partition/large/image.jpg' |
| 299 | + }, |
| 300 | + imageFiles: [{permaId: 100, id: 1, width: 200, height: 100}] |
| 301 | + }); |
| 302 | + |
| 303 | + expect(getByRole('img')).not.toHaveAttribute('srcset'); |
| 304 | + expect(getByRole('img')).toHaveAttribute('src', |
| 305 | + '000/000/001/medium/image.jpg'); |
| 306 | + }); |
| 307 | + }); |
| 308 | + |
193 | 309 | describe('basic functionality', () => { |
194 | 310 | it('renders with FitViewport and ContentElementBox', () => { |
195 | 311 | const {getContentElement} = renderContentElement({ |
|
0 commit comments