-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathbasic.test.tsx
More file actions
173 lines (157 loc) · 5.19 KB
/
basic.test.tsx
File metadata and controls
173 lines (157 loc) · 5.19 KB
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
import { act, fireEvent, render } from '@testing-library/react';
import React from 'react';
import Image, { CoverConfig } from '../src';
describe('Basic', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('snapshot', () => {
const { asFragment } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
width={200}
/>,
);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('With click', () => {
const onClickMock = jest.fn();
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
onClick={onClickMock}
/>,
);
fireEvent.click(container.querySelector('.rc-image'));
expect(onClickMock).toHaveBeenCalledTimes(1);
});
it('With click when disable preview', () => {
const onClickMock = jest.fn();
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
onClick={onClickMock}
preview={false}
/>,
);
fireEvent.click(container.querySelector('.rc-image'));
expect(onClickMock).toHaveBeenCalledTimes(1);
});
it('fetchPriority should be passed to img element', () => {
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
fetchPriority="high"
/>,
);
const img = container.querySelector('img');
const wrapper = container.querySelector('.rc-image');
expect(img).toHaveAttribute('fetchpriority', 'high');
expect(wrapper).not.toHaveAttribute('fetchpriority');
});
it('className and style props should work on img element', () => {
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
className="img"
style={{
objectFit: 'cover',
}}
/>,
);
const img = container.querySelector('img');
expect(img).toHaveClass('img');
expect(img).toHaveStyle({ objectFit: 'cover' });
});
it('classNames.root and styles.root should work on image wrapper element', () => {
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
classNames={{
root: 'bamboo',
}}
styles={{
root: {
objectFit: 'cover',
},
}}
/>,
);
const wrapperElement = container.firstChild;
expect(wrapperElement).toHaveClass('bamboo');
expect(wrapperElement).toHaveStyle({ objectFit: 'cover' });
});
// https://github.com/ant-design/ant-design/issues/36680
it('preview mask should be hidden when image has style { display: "none" }', () => {
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
style={{
display: 'none',
}}
preview={{ cover: 'Click to Preview' }}
/>,
);
const maskElement = container.querySelector('.rc-image-cover');
expect(maskElement).toHaveStyle({ display: 'none' });
});
it('preview zIndex should pass', () => {
const { baseElement } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{ zIndex: 9999, open: true }}
/>,
);
const operationsElement = baseElement.querySelector('.rc-image-preview');
expect(operationsElement).toHaveStyle({ zIndex: 9999 });
});
it('cover placement should work', () => {
const App = () => {
const [placement, setPlacement] = React.useState<'top' | 'bottom' | 'center'>('center');
return (
<>
<select
id="placement"
onChange={e => setPlacement(e.target.value as CoverConfig['placement'])}
value={placement}
>
<option value="top">top</option>
<option value="bottom">bottom</option>
<option value="center">center</option>
</select>
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{
cover: {
coverNode: 'Click to Preview',
placement: placement as CoverConfig['placement'],
},
}}
/>
</>
);
};
const { container } = render(<App />);
const coverElement = container.querySelector('.rc-image-cover');
expect(coverElement).toHaveClass('rc-image-cover-center');
fireEvent.change(container.querySelector('#placement'), {
target: { value: 'top' },
});
// Wait for the state update to take effect
act(() => {
jest.runAllTimers();
});
expect(coverElement).toHaveClass('rc-image-cover-top');
fireEvent.change(container.querySelector('#placement'), {
target: { value: 'bottom' },
});
// Wait for the state update to take effect
act(() => {
jest.runAllTimers();
});
expect(coverElement).toHaveClass('rc-image-cover-bottom');
});
});