Skip to content

Commit f04e1f6

Browse files
feat: add thumb testing helpers with flat structure support and async rendering wait
1 parent 349e06c commit f04e1f6

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

e2e/helpers/konva-testing.helpers.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Group } from 'konva/lib/Group';
55
import { E2E_CanvasItemKeyAttrs } from './types/e2e-types';
66
import { getCanvasBoundingBox } from './position.helpers';
77

8+
// MAIN CANVAS HELPERS
9+
810
const getLayer = async (page: Page): Promise<Layer> =>
911
await page.evaluate(() => {
1012
return window.__TESTING_KONVA_LAYER__;
@@ -50,6 +52,50 @@ export const getByShapeType = async (
5052
}
5153
};
5254

55+
// THUMB HELPERS
56+
57+
const getThumbLayer = async (page: Page, pageIndex: number): Promise<Layer> =>
58+
await page.evaluate(index => {
59+
return window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
60+
}, pageIndex);
61+
62+
const getThumbChildren = async (page: Page, pageIndex: number) => {
63+
const layer = await getThumbLayer(page, pageIndex);
64+
return layer?.children || [];
65+
};
66+
67+
// Waits for a thumb to finish rendering (until it has at least one child)
68+
69+
export const waitForThumbToRender = async (
70+
page: Page,
71+
pageIndex: number,
72+
timeout = 5000
73+
) => {
74+
await page.waitForFunction(
75+
async index => {
76+
const layer = window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
77+
if (!layer) return false;
78+
79+
const children = layer.children || [];
80+
return children && children.length > 0;
81+
},
82+
pageIndex,
83+
{ timeout }
84+
);
85+
};
86+
87+
export const getByShapeTypeInThumb = async (
88+
page: Page,
89+
pageIndex: number,
90+
shape: string
91+
): Promise<Group | Shape | undefined> => {
92+
await waitForThumbToRender(page, pageIndex);
93+
94+
// Search for the shape
95+
const children = await getThumbChildren(page, pageIndex);
96+
return children?.find(child => child.attrs.shapeType === shape);
97+
};
98+
5399
export const getTransformer = async (page: Page): Promise<any> => {
54100
const layer = await getLayer(page);
55101
const transformer = layer?.children.find((child: any) => {

0 commit comments

Comments
 (0)