-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathkonva-testing.helpers.ts
More file actions
175 lines (153 loc) · 4.79 KB
/
konva-testing.helpers.ts
File metadata and controls
175 lines (153 loc) · 4.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
import { Page } from '@playwright/test';
import { Layer } from 'konva/lib/Layer';
import { Shape } from 'konva/lib/Shape';
import { Group } from 'konva/lib/Group';
import { E2E_CanvasItemKeyAttrs } from './types/e2e-types';
import { getCanvasBoundingBox } from './position.helpers';
// MAIN CANVAS HELPERS
const getLayer = async (page: Page): Promise<Layer> =>
await page.evaluate(() => {
return window.__TESTING_KONVA_LAYER__;
});
const getChildren = async (page: Page): Promise<(Group | Shape)[]> => {
const layer = await getLayer(page);
return layer?.children.flatMap(child =>
Boolean((child as any)?.children) ? (child as any).children : child
);
};
export const getAllByShapeType = async (
page: Page,
shape: string
): Promise<(Group | Shape)[]> => {
const children = await getChildren(page);
const shapes = children?.filter(child => child.attrs.shapeType === shape);
if (shapes.length === 0) {
throw new Error(`No shapes found with shapeType ${shape}`);
} else {
return shapes;
}
};
export const getByShapeType = async (
page: Page,
shape: string
): Promise<Group | Shape | undefined> => {
const children = await getChildren(page);
const count = children?.filter(
child => child.attrs.shapeType === shape
)?.length;
if (count === 1) {
return children.find(child => child.attrs.shapeType === shape);
} else if (count > 1) {
throw new Error(
`Found ${count} shapes with shapeType ${shape} you should use getAllByShapeType`
);
} else {
return undefined;
}
};
// THUMB HELPERS
const getThumbLayer = async (page: Page, pageIndex: number): Promise<Layer> =>
await page.evaluate(index => {
return window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
}, pageIndex);
const getThumbChildren = async (page: Page, pageIndex: number) => {
const layer = await getThumbLayer(page, pageIndex);
return layer?.children || [];
};
// Waits for a thumb to finish rendering (until it has at least one child)
export const waitForThumbToRender = async (
page: Page,
pageIndex: number,
timeout = 5000
) => {
await page.waitForFunction(
async index => {
const layer = window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
if (!layer) return false;
const children = layer.children || [];
return children && children.length > 0;
},
pageIndex,
{ timeout }
);
};
export const getByShapeTypeInThumb = async (
page: Page,
pageIndex: number,
shape: string
): Promise<Group | Shape | undefined> => {
await waitForThumbToRender(page, pageIndex);
// Search for the shape
const children = await getThumbChildren(page, pageIndex);
return children?.find(child => child.attrs.shapeType === shape);
};
export const getTransformer = async (page: Page): Promise<any> => {
const layer = await getLayer(page);
const transformer = layer?.children.find((child: any) => {
// Ensure that canvas has an element dropped, selected or not
return (
child._nodes?.length > 0 ||
(child._nodes?.length === 0 && child.index > 0)
);
});
return transformer;
};
export const getIndexFromCanvasShape = async (
page: Page,
shapeId: number
): Promise<number> => {
const children = await getChildren(page);
const index = children.findIndex(child => child._id === shapeId);
return index;
};
export const getWithinCanvasItemList = async (
page: Page
): Promise<Group['attrs'][]> => {
const items = await page.evaluate(() => {
return window.__TESTING_KONVA_LAYER__.find(
(c: any) => c.getType('Group') && (c.attrs['data-id'] as Group)
);
});
return items.map(it => it.attrs);
};
export const clickOnCanvasItem = async (
page: Page,
item: E2E_CanvasItemKeyAttrs
) => {
const { x, y } = item;
const stageCanvas = await page.locator('#konva-stage canvas').nth(1);
const canvasWindowPos = await stageCanvas.boundingBox();
if (!canvasWindowPos) throw new Error('Canvas is not loaded on ui');
await page.mouse.move(
canvasWindowPos?.x + x + 20,
canvasWindowPos?.y + y + 20
);
await page.mouse.down();
await page.mouse.up();
return item;
};
export const dbClickOnCanvasItem = async (
page: Page,
item: E2E_CanvasItemKeyAttrs
) => {
const { x, y } = item;
const canvasWindowPos = await getCanvasBoundingBox(page);
await page.mouse.dblclick(
canvasWindowPos?.x + x + 20,
canvasWindowPos?.y + y + 20
);
return item;
};
export const ctrlClickOverCanvasItems = async (
page: Page,
itemList: E2E_CanvasItemKeyAttrs[]
) => {
if (!itemList.length)
throw new Error('Please, add an array with at least one canvas Item');
// NOTE: The keyboard entry 'ControlOrMeta' is the way to simulate both 'Ctrl' or 'Command' key
await page.keyboard.down('ControlOrMeta');
for (const item of itemList) {
await clickOnCanvasItem(page, item);
}
await page.keyboard.up('ControlOrMeta');
};