|
| 1 | +import React from 'react'; |
| 2 | +import '@testing-library/jest-dom/extend-expect' |
| 3 | + |
| 4 | +import {LogoList} from 'contentElements/logoList/LogoList'; |
| 5 | + |
| 6 | +import {renderInContentElement} from 'pageflow-scrolled/testHelpers'; |
| 7 | + |
| 8 | +describe('LogoList images', () => { |
| 9 | + const imageFileUrlTemplates = { |
| 10 | + medium: ':id_partition/medium/image.webp', |
| 11 | + original: ':id_partition/original/:basename.:extension' |
| 12 | + }; |
| 13 | + |
| 14 | + function renderLogoList({configuration, imageFiles}) { |
| 15 | + const result = renderInContentElement( |
| 16 | + <LogoList configuration={configuration} contentElementId={1} />, |
| 17 | + {seed: {imageFileUrlTemplates, imageFiles}} |
| 18 | + ); |
| 19 | + |
| 20 | + result.simulateScrollPosition('near viewport'); |
| 21 | + |
| 22 | + return result; |
| 23 | + } |
| 24 | + |
| 25 | + it('renders medium variant of item images', () => { |
| 26 | + const {getAllByRole} = renderLogoList({ |
| 27 | + configuration: {items: [{id: 1, image: 100}, {id: 2, image: 101}]}, |
| 28 | + imageFiles: [{id: 1, permaId: 100}, {id: 2, permaId: 101}] |
| 29 | + }); |
| 30 | + |
| 31 | + expect(getAllByRole('img').map(img => img.getAttribute('src'))).toEqual([ |
| 32 | + '000/000/001/medium/image.webp', |
| 33 | + '000/000/002/medium/image.webp' |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses original url of svg images', () => { |
| 38 | + const {getByRole} = renderLogoList({ |
| 39 | + configuration: {items: [{id: 1, image: 100}]}, |
| 40 | + imageFiles: [{id: 1, permaId: 100, basename: 'logo', extension: 'svg'}] |
| 41 | + }); |
| 42 | + |
| 43 | + expect(getByRole('img')).toHaveAttribute('src', '000/000/001/original/logo.svg'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders alt text of image file', () => { |
| 47 | + const {getByRole} = renderLogoList({ |
| 48 | + configuration: {items: [{id: 1, image: 100}]}, |
| 49 | + imageFiles: [{id: 1, permaId: 100, configuration: {alt: 'ACME Inc.'}}] |
| 50 | + }); |
| 51 | + |
| 52 | + expect(getByRole('img', {name: 'ACME Inc.'})).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('prefers alt text of item', () => { |
| 56 | + const {getByRole} = renderLogoList({ |
| 57 | + configuration: {items: [{id: 1, image: 100, alt: 'ACME website'}]}, |
| 58 | + imageFiles: [{id: 1, permaId: 100, configuration: {alt: 'ACME Inc.'}}] |
| 59 | + }); |
| 60 | + |
| 61 | + expect(getByRole('img', {name: 'ACME website'})).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('passes image dimensions to preserve aspect ratio', () => { |
| 65 | + const {getByRole} = renderLogoList({ |
| 66 | + configuration: {items: [{id: 1, image: 100}]}, |
| 67 | + imageFiles: [{id: 1, permaId: 100, width: 400, height: 100}] |
| 68 | + }); |
| 69 | + |
| 70 | + expect(getByRole('img')).toHaveAttribute('width', '400'); |
| 71 | + expect(getByRole('img')).toHaveAttribute('height', '100'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('skips items with missing image files', () => { |
| 75 | + const {getAllByRole} = renderLogoList({ |
| 76 | + configuration: {items: [{id: 1, image: 100}, {id: 2, image: 999}]}, |
| 77 | + imageFiles: [{id: 1, permaId: 100}] |
| 78 | + }); |
| 79 | + |
| 80 | + expect(getAllByRole('listitem')).toHaveLength(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not load images before content element is near viewport', () => { |
| 84 | + const {queryByRole} = renderInContentElement( |
| 85 | + <LogoList configuration={{items: [{id: 1, image: 100}]}} contentElementId={1} />, |
| 86 | + {seed: {imageFileUrlTemplates, imageFiles: [{id: 1, permaId: 100}]}} |
| 87 | + ); |
| 88 | + |
| 89 | + expect(queryByRole('img')).toBeNull(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments