-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathtest.tsx
277 lines (239 loc) · 6.91 KB
/
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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { expect } from '@storybook/jest';
import {
within,
userEvent,
waitFor,
fireEvent,
} from '@storybook/testing-library';
import type { queries } from '@storybook/testing-library';
export type Canvas = ReturnType<typeof within<typeof queries>>;
type HorArrows = { left: boolean; right: boolean };
type VerArrows = { up: boolean; down: boolean };
export class TestObj {
canvas: Canvas;
leftArrow: string;
rightArrow: string;
constructor(
canvas: Canvas,
{ leftArrow, rightArrow }: { leftArrow: string; rightArrow: string },
) {
this.canvas = canvas;
this.leftArrow = leftArrow;
this.rightArrow = rightArrow;
}
async isReady() {
await waitFor(() =>
expect(this.canvas.getAllByText('visible: false')[0]).toBeInTheDocument(),
);
}
async getCards(text = '') {
const elems = await this.canvas.queryAllByText(
(_content, element) =>
!!(element as HTMLElement)?.innerText?.includes(text),
{ selector: '.card' },
);
return [...(elems || [])];
}
async getVisibleCards() {
return (await this.getCards('visible: true')) || [];
}
async getVisibleCardsKeys(length = 3) {
const nodes = await this.getVisibleCards();
const keys = nodes.map((el) => el.innerText.split('\n')[0]);
expect(keys).toHaveLength(length);
return keys;
}
async getSelectedCards() {
return (await this.getCards('selected: true')) || [];
}
async getSelectedCardsKeys() {
const nodes = await this.getSelectedCards();
return nodes.map((el) => el.innerText.split('\n')[0]);
}
async cardHidden(card: string) {
const hiddenCards = await this.getCards(`${card}\nvisible: false`);
await waitFor(() => expect(hiddenCards[0]).toBeInTheDocument());
}
async wait(timeout = 800) {
await new Promise((res) => setTimeout(() => res(true), timeout));
}
async clickPrev() {
await userEvent.click(this.canvas.getByTestId(this.leftArrow));
await this.wait();
}
async clickNext() {
await userEvent.click(this.canvas.getByTestId(this.rightArrow));
await this.wait();
}
async arrowsVisible(arrows: HorArrows | VerArrows) {
await this.wait();
let firstArrow: HorArrows['left'] | VerArrows['up'];
let secondArrow: HorArrows['right'] | VerArrows['down'];
if ('up' in arrows) {
firstArrow = arrows.up;
secondArrow = arrows.down;
} else {
firstArrow = arrows.left;
secondArrow = arrows.right;
}
if (firstArrow) {
expect(await this.canvas.getByTestId(this.leftArrow)).toBeVisible();
} else {
expect(await this.canvas.getByTestId(this.leftArrow)).not.toBeVisible();
}
if (secondArrow) {
expect(await this.canvas.getByTestId(this.rightArrow)).toBeVisible();
} else {
expect(await this.canvas.getByTestId(this.rightArrow)).not.toBeVisible();
}
}
}
export const leftArrowSelector = 'left-arrow';
export const rightArrowSelector = 'right-arrow';
export const upArrowSelector = 'up-arrow';
export const downArrowSelector = 'down-arrow';
export const scrollSmokeTest = async (testObj: TestObj) => {
await testObj.wait();
await testObj.arrowsVisible({ up: false, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test0',
'test1',
'test2',
]);
await testObj.clickNext();
await testObj.cardHidden('test0');
await testObj.arrowsVisible({ up: true, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test3',
'test4',
'test5',
]);
await testObj.clickNext();
await testObj.cardHidden('test5');
await testObj.arrowsVisible({ up: true, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test6',
'test7',
'test8',
]);
await testObj.clickNext();
await testObj.cardHidden('test6');
await testObj.arrowsVisible({ up: true, down: false });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test7',
'test8',
'test9',
]);
await testObj.clickPrev();
await testObj.cardHidden('test7');
await testObj.arrowsVisible({ up: true, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test4',
'test5',
'test6',
]);
await testObj.clickPrev();
await testObj.cardHidden('test4');
await testObj.arrowsVisible({ up: true, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test1',
'test2',
'test3',
]);
await testObj.clickPrev();
await testObj.cardHidden('test3');
await testObj.arrowsVisible({ up: false, down: true });
expect(await testObj.getVisibleCardsKeys()).toEqual([
'test0',
'test1',
'test2',
]);
};
export const ScrollTest = ({
leftArrow = leftArrowSelector,
rightArrow = rightArrowSelector,
} = {}) => ({
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const testObj = new TestObj(canvas, { leftArrow, rightArrow });
await testObj.isReady();
await testObj.wait();
await scrollSmokeTest(testObj);
},
});
export async function drag(
element: HTMLElement,
{
to: inTo = undefined,
delta = undefined,
steps = 20,
duration = 300,
}: {
to?: undefined | { x: number; y: number };
delta?: undefined | { x: number; y: number };
steps?: number;
duration?: number;
},
) {
const from = getElementClientCenter(element);
const to = delta
? {
x: from.x + delta.x,
y: from.y + delta.y,
}
: getCoords(inTo as unknown as HTMLElement);
const step = {
x: (to.x - from.x) / steps,
y: (to.y - from.y) / steps,
};
const current = {
clientX: from.x,
clientY: from.y,
};
fireEvent.mouseEnter(element, current);
fireEvent.mouseOver(element, current);
fireEvent.mouseMove(element, current);
fireEvent.mouseDown(element, current);
for (let i = 0; i < steps; i++) {
current.clientX += step.x;
current.clientY += step.y;
await sleep(duration / steps);
fireEvent.mouseMove(element, current);
}
fireEvent.mouseUp(element, current);
}
function getElementClientCenter(element: HTMLElement) {
const { left, top, width, height } = element.getBoundingClientRect();
return {
x: left + width / 2,
y: top + height / 2,
};
}
const getCoords = (charlie: HTMLElement) =>
isElement(charlie) ? getElementClientCenter(charlie) : { x: 0, y: 0 };
function isElement(obj: unknown) {
if (typeof obj !== 'object') {
return false;
}
let prototypeStr, prototype;
do {
prototype = Object.getPrototypeOf(obj);
// to work in iframe
prototypeStr = Object.prototype.toString.call(prototype);
// '[object Document]' is used to detect document
if (
prototypeStr === '[object Element]' ||
prototypeStr === '[object Document]'
) {
return true;
}
obj = prototype;
// null is the terminal of object
} while (prototype !== null);
return false;
}
const sleep = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});