Skip to content

Commit e33681f

Browse files
committed
Add tests for warnings and fix bug when getting normalizedWidth
1 parent b1110fa commit e33681f

2 files changed

Lines changed: 68 additions & 9 deletions

File tree

packages/hydrogen-react/src/Image.test.tsx

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import {render, screen} from '@testing-library/react';
22
import {faker} from '@faker-js/faker';
33
import {Image} from './Image.js';
4+
import {Mock} from 'vitest';
45

56
const defaultProps = {
7+
sizes: '100vw',
68
src: 'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg',
79
};
810

911
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-
1712
// This test fails because the received src has ?width=100 appended to it
1813
it.skip('renders an `img` element', () => {
1914
const src = faker.image.imageUrl();
@@ -61,4 +56,68 @@ describe('<Image />', () => {
6156
expect(image).toBeInTheDocument();
6257
expect(image).toHaveAttribute('sizes', '100vw');
6358
});
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+
});
64117
});
118+
119+
function getWarnings() {
120+
return (console.warn as Mock<[string]>).mock.calls.map(
121+
([message]) => message,
122+
);
123+
}

packages/hydrogen-react/src/Image.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function Image({
120120
if (loaderOptions) {
121121
console.warn(
122122
`Deprecated property from original Image component in use: ` +
123-
`Use the \`crop\`, \`width\`, \`height\`, and src props, or` +
123+
`Use the \`crop\`, \`width\`, \`height\`, and src props, or ` +
124124
`the \`data\` prop to achieve the same result. Image used is ${
125125
src || data?.url
126126
}`,
@@ -154,7 +154,7 @@ export function Image({
154154

155155
const normalizedWidth = `${
156156
getUnitValueParts(normalizedWidthProp.toString()).number
157-
} ${getUnitValueParts(normalizedWidthProp.toString()).unit}`;
157+
}${getUnitValueParts(normalizedWidthProp.toString()).unit}`;
158158

159159
const normalizedHeight: string =
160160
height === undefined

0 commit comments

Comments
 (0)