|
1 | 1 | import {render, screen} from '@testing-library/react'; |
2 | 2 | import {faker} from '@faker-js/faker'; |
3 | 3 | import {Image} from './Image.js'; |
| 4 | +import {Mock} from 'vitest'; |
4 | 5 |
|
5 | 6 | const defaultProps = { |
| 7 | + sizes: '100vw', |
6 | 8 | src: 'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg', |
7 | 9 | }; |
8 | 10 |
|
9 | 11 | describe('<Image />', () => { |
10 | | - beforeAll(() => { |
11 | | - // eslint-disable-next-line @typescript-eslint/no-empty-function |
12 | | - vi.spyOn(console, 'warn').mockImplementation(() => {}); |
13 | | - }); |
14 | | - |
15 | | - it.todo('warns user if no sizes are provided'); |
16 | | - |
17 | 12 | // This test fails because the received src has ?width=100 appended to it |
18 | 13 | it.skip('renders an `img` element', () => { |
19 | 14 | const src = faker.image.imageUrl(); |
@@ -61,4 +56,68 @@ describe('<Image />', () => { |
61 | 56 | expect(image).toBeInTheDocument(); |
62 | 57 | expect(image).toHaveAttribute('sizes', '100vw'); |
63 | 58 | }); |
| 59 | + |
| 60 | + describe('warnings', () => { |
| 61 | + const consoleMock = { |
| 62 | + ...console, |
| 63 | + warn: vi.fn(), |
| 64 | + }; |
| 65 | + |
| 66 | + vi.stubGlobal('console', consoleMock); |
| 67 | + |
| 68 | + afterAll(() => { |
| 69 | + vi.unstubAllGlobals(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('warns user if no sizes are provided', () => { |
| 73 | + render(<Image {...defaultProps} sizes={undefined} />); |
| 74 | + |
| 75 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 76 | + expect(getWarnings()).toMatchInlineSnapshot( |
| 77 | + ` |
| 78 | + [ |
| 79 | + "No sizes prop provided to Image component, you may be loading unnecessarily large images. Image used is https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg", |
| 80 | + ] |
| 81 | + `, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not warn user if no sizes are provided but width is fixed', () => { |
| 86 | + render(<Image {...defaultProps} sizes={undefined} width={100} />); |
| 87 | + |
| 88 | + expect(console.warn).toHaveBeenCalledTimes(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it('warns user if widths is provided', () => { |
| 92 | + render(<Image {...defaultProps} widths={[]} />); |
| 93 | + |
| 94 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 95 | + expect(getWarnings()).toMatchInlineSnapshot( |
| 96 | + ` |
| 97 | + [ |
| 98 | + "Deprecated property from original Image component in use: \`widths\` are now calculated automatically based on the config and width props. Image used is https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg", |
| 99 | + ] |
| 100 | + `, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('warns user if loaderOptions are provided', () => { |
| 105 | + render(<Image {...defaultProps} loaderOptions={{}} />); |
| 106 | + |
| 107 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 108 | + expect(getWarnings()).toMatchInlineSnapshot( |
| 109 | + ` |
| 110 | + [ |
| 111 | + "Deprecated property from original Image component in use: Use the \`crop\`, \`width\`, \`height\`, and src props, or the \`data\` prop to achieve the same result. Image used is https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg", |
| 112 | + ] |
| 113 | + `, |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
64 | 117 | }); |
| 118 | + |
| 119 | +function getWarnings() { |
| 120 | + return (console.warn as Mock<[string]>).mock.calls.map( |
| 121 | + ([message]) => message, |
| 122 | + ); |
| 123 | +} |
0 commit comments