-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmodal.test.tsx
More file actions
233 lines (208 loc) · 7.79 KB
/
modal.test.tsx
File metadata and controls
233 lines (208 loc) · 7.79 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React from 'react';
import { cleanup, fireEvent, render } from '@testing-library/react';
import { alert, modal } from 'ant-design-testing';
import '@testing-library/jest-dom';
import { IFloatProps } from '../../float';
import Modal from '../';
function dragFromTo(
ele: HTMLElement,
from: NonNullable<IFloatProps['position']>,
to: NonNullable<IFloatProps['position']>
) {
fireEvent.mouseDown(ele, { clientX: from.x, clientY: from.y });
fireEvent.mouseMove(document, { clientX: to.x, clientY: to.y });
return {
mouseUp: () => fireEvent.mouseUp(ele, { clientX: to.x, clientY: to.y }),
};
}
describe('Test Modal Component', () => {
beforeEach(() => {
cleanup();
});
it('Should Match snapshot', () => {
const { asFragment } = render(
<Modal visible title="title" getContainer={false}>
<div>test</div>
</Modal>
);
expect(asFragment()).toMatchSnapshot();
});
it('Should get different size', () => {
// default size
const { container, rerender } = render(
<Modal visible title="title" getContainer={false}>
test
</Modal>
);
expect(modal.query(container)?.style.width).toBe('520px');
// small size
rerender(
<Modal visible title="title" getContainer={false} size="small">
test
</Modal>
);
expect(modal.query(container)?.style.width).toBe('400px');
// middle size
rerender(
<Modal visible title="title" getContainer={false} size="middle">
test
</Modal>
);
expect(modal.query(container)?.style.width).toBe('640px');
// large size
rerender(
<Modal visible title="title" getContainer={false} size="large">
test
</Modal>
);
expect(modal.query(container)?.style.width).toBe('900px');
});
describe('Should support banner', () => {
it('Should support string banner', async () => {
const { getByText } = render(
<Modal visible title="title" getContainer={false} banner="banner">
test
</Modal>
);
expect(getByText('banner')).toBeInTheDocument();
});
it('Should support ReactNode', async () => {
const { getByTestId } = render(
<Modal
visible
title="title"
getContainer={false}
banner={<div data-testid="banner">xxxx</div>}
>
test
</Modal>
);
expect(getByTestId('banner')).toBeInTheDocument();
});
it('Should support AlertProps', async () => {
const { container, getByText } = render(
<Modal
visible
title="title"
getContainer={false}
banner={{ message: 'banner', type: 'error' }}
>
test
</Modal>
);
expect(getByText('banner')).toBeInTheDocument();
expect(alert.query(container)?.classList.contains('ant-alert-error')).toBeTruthy();
});
it('Should match snapshot for draggable modal', () => {
const { asFragment } = render(
<Modal
visible
title="title"
getContainer={false}
draggable
position={{ x: 0, y: 0 }}
>
test
</Modal>
);
expect(asFragment()).toMatchSnapshot();
});
it('Should support draggable modal and update position on drag', () => {
const fn = jest.fn();
const { container } = render(
<Modal
visible
title="title"
getContainer={false}
draggable
position={{ x: 0, y: 0 }}
onPositionChange={fn}
>
test
</Modal>
);
const modalElement = modal
.query(container)!
.querySelector<HTMLDivElement>('.ant-modal-header')!;
dragFromTo(modalElement, { x: 0, y: 0 }, { x: 100, y: 100 }).mouseUp();
expect(fn).toHaveBeenCalledWith({ x: 100, y: 100 });
});
it('Should match snapshot for resizable modal', () => {
const { asFragment } = render(
<Modal
visible
title="title"
getContainer={false}
resizable
rect={{ width: 620, height: 600 }}
>
test
</Modal>
);
expect(asFragment()).toMatchSnapshot();
});
it('Should render Spin correct', () => {
const { container } = render(
<Modal visible title="title" getContainer={false} loading>
test
</Modal>
);
const spin = modal.query(container)!.querySelector<HTMLDivElement>('.ant-spin-blur')!;
expect(spin).toBeTruthy();
});
it('Should render no Spin correct', () => {
const { container } = render(
<Modal visible title="title" getContainer={false} loading={false}>
test
</Modal>
);
const spin = modal.query(container)!.querySelector<HTMLDivElement>('.ant-spin-blur')!;
expect(spin).toBeFalsy();
});
it('Should call onRectChange for resizable modal', () => {
const onRectChange = jest.fn();
const { container } = render(
<Modal
visible
title="title"
getContainer={false}
resizable
rect={{ width: 620, height: 600 }}
onRectChange={onRectChange}
>
test
</Modal>
);
const eHandle = modal.query(container)!.querySelector<HTMLDivElement>('.handle-e')!;
dragFromTo(eHandle, { x: 620, y: 300 }, { x: 700, y: 300 }).mouseUp();
expect(onRectChange).toBeCalledWith({ width: 700, height: 600 });
});
it('Should support resizable and draggable modal', () => {
const onRectChange = jest.fn();
const onPositionChange = jest.fn();
const { container } = render(
<Modal
visible
title="title"
getContainer={false}
resizable
draggable
rect={{ width: 620, height: 600 }}
position={{ x: 50, y: 50 }}
onRectChange={onRectChange}
onPositionChange={onPositionChange}
>
test
</Modal>
);
const eHandle = modal.query(container)!.querySelector<HTMLDivElement>('.handle-e')!;
dragFromTo(eHandle, { x: 620, y: 300 }, { x: 700, y: 300 }).mouseUp();
expect(onRectChange).toBeCalledWith({ width: 700, height: 600 });
expect(onPositionChange).not.toBeCalled();
const wHandle = modal.query(container)!.querySelector<HTMLDivElement>('.handle-w')!;
dragFromTo(wHandle, { x: 620, y: 300 }, { x: 400, y: 300 }).mouseUp();
expect(onRectChange).toBeCalledWith({ width: 700, height: 600 });
expect(onPositionChange).toBeCalledWith({ x: -170, y: 50 });
});
});
});