-
Notifications
You must be signed in to change notification settings - Fork 241
/
Image.test.tsx
196 lines (152 loc) · 5.35 KB
/
Image.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {Mock, vi, describe, expect, it} from 'vitest';
import {render, screen} from '@testing-library/react';
import {faker} from '@faker-js/faker';
import {Image} from './Image.js';
const defaultProps = {
sizes: '100vw',
src: 'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg',
};
describe('<Image />', () => {
// This test fails because the received src has ?width=100 appended to it
it.skip('renders an `img` element', () => {
const src = faker.image.imageUrl();
render(<Image {...defaultProps} src={src} />);
expect(screen.getByRole('img')).toHaveAttribute('src', src);
});
it('accepts passthrough props such as `id`', () => {
const id = faker.random.alpha();
render(<Image {...defaultProps} id={id} />);
expect(screen.getByRole('img')).toHaveAttribute('id', id);
});
it('sets the `alt` prop on the img tag', () => {
const alt = faker.random.alpha();
render(<Image {...defaultProps} alt={alt} />);
expect(screen.getByRole('img')).toHaveAttribute('alt', alt);
});
it('has a `loading` prop of `lazy` by default', () => {
render(<Image {...defaultProps} />);
expect(screen.getByRole('img')).toHaveAttribute('loading', 'lazy');
});
it('accepts a `loading` prop', () => {
render(<Image {...defaultProps} loading="eager" />);
expect(screen.getByRole('img')).toHaveAttribute('loading', 'eager');
});
it('accepts a `sizes` prop', () => {
render(<Image {...defaultProps} sizes="100vw" />);
expect(screen.getByRole('img')).toHaveAttribute('sizes', '100vw');
});
describe('loader', () => {
it('calls the loader with the src, width, height and crop props', () => {
const loader = vi.fn();
const src = faker.image.imageUrl();
const width = 600;
const height = 400;
const crop = 'center';
render(
<Image
{...defaultProps}
src={src}
width={width}
crop={crop}
height={height}
loader={loader}
/>,
);
expect(loader).toHaveBeenCalledWith({
src,
width,
height,
crop,
});
});
});
describe('aspect-ratio', () => {
// Assertion support is limited for aspectRatio
// https://github.com/testing-library/jest-dom/issues/452
// expect(image).toHaveStyle('aspect-ratio: 1 / 1');
it('sets the aspect-ratio on the style prop when set explicitly', () => {
const aspectRatio = '4/3';
render(
<Image {...defaultProps} sizes="100vw" aspectRatio={aspectRatio} />,
);
expect(screen.getByRole('img').style.aspectRatio).toBe(aspectRatio);
});
it('infers the aspect-ratio from the storefront data', () => {
const data = {height: 300, width: 400};
render(<Image {...defaultProps} sizes="100vw" data={data} />);
expect(screen.getByRole('img').style.aspectRatio).toBe('400/300');
});
it('infers the aspect-ratio from the storefront data for fixed-width images when no height prop is provided', () => {
const data = {height: 300, width: 400};
render(<Image {...defaultProps} sizes="100vw" data={data} width={600} />);
expect(screen.getByRole('img').style.aspectRatio).toBe('400/300');
});
it('infers the aspect-ratio from the storefront data for fixed-width images the height and width are different units', () => {
const data = {height: 300, width: 400};
render(
<Image
{...defaultProps}
sizes="100vw"
data={data}
height={400}
width="100%"
/>,
);
expect(screen.getByRole('img').style.aspectRatio).toBe('400/300');
});
it('infers the aspect-ratio from the height and width props for fixed-width images', () => {
const data = {height: 300, width: 400};
render(
<Image
{...defaultProps}
sizes="100vw"
data={data}
width={600}
height={400}
/>,
);
expect(screen.getByRole('img').style.aspectRatio).toBe('600/400');
});
});
describe('warnings', () => {
const consoleMock = {
...console,
warn: vi.fn(),
};
vi.stubGlobal('console', consoleMock);
afterAll(() => {
vi.unstubAllGlobals();
});
it('warns user if no src is provided', () => {
render(<Image {...defaultProps} src={undefined} />);
expect(console.warn).toHaveBeenCalledTimes(1);
expect(getWarnings()).toMatchInlineSnapshot(
`
[
"No src or data.url provided to Image component.",
]
`,
);
});
it('warns user if no sizes are provided', () => {
render(<Image {...defaultProps} width="100%" sizes={undefined} />);
expect(console.warn).toHaveBeenCalledTimes(1);
expect(getWarnings()).toMatchInlineSnapshot(
`
[
"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",
]
`,
);
});
it('does not warn user if no sizes are provided but width is fixed', () => {
render(<Image {...defaultProps} sizes={undefined} width={100} />);
expect(console.warn).toHaveBeenCalledTimes(0);
});
});
});
function getWarnings(): string[] {
return (console.warn as Mock<[string]>).mock.calls.map(
([message]) => message,
);
}