Skip to content

Commit b3aa7f0

Browse files
committed
Initial positioned tests
1 parent bd02b3a commit b3aa7f0

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

src/components/tests/Common.tsx

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { render } from "@testing-library/react";
33
import "@testing-library/jest-dom/extend-expect";
44
import { ViewPort } from "../ViewPort";
55
import { drag } from "./TestUtils";
6+
import { ResizeType } from "../../core-types";
67

7-
const mutateComponent = (component: React.ReactNode, newProps: Object) => {
8+
export const mutateComponent = (component: React.ReactNode, newProps: Object) => {
89
return React.cloneElement(component as React.DetailedReactHTMLElement<any, HTMLElement>, newProps);
910
};
1011

@@ -287,3 +288,73 @@ export const commonResizableTests = (
287288
expect(size(style)).toBe("150px");
288289
});
289290
};
291+
292+
export const commonPositionedTests = (
293+
name: string,
294+
component: React.ReactNode,
295+
size: (style: CSSStyleDeclaration) => string | null,
296+
edge: (style: CSSStyleDeclaration) => string | null,
297+
oppositeEdge: (style: CSSStyleDeclaration) => string | null,
298+
handle: string,
299+
horizontal: boolean,
300+
negate: boolean,
301+
) => {
302+
test(`${name} after resize has correct styles`, async () => {
303+
// arrange
304+
const { container } = render(
305+
<ViewPort>
306+
{mutateComponent(component, { id: "test", resizable: [ResizeType.Left, ResizeType.Top, ResizeType.Bottom, ResizeType.Right] })}
307+
</ViewPort>,
308+
);
309+
310+
const resizeHandle = container.querySelector(`#test-${handle}`)!;
311+
const sut = container.querySelector("#test")!;
312+
313+
// act
314+
drag(
315+
resizeHandle,
316+
sut,
317+
{ width: horizontal ? 50 : 0, height: horizontal ? 0 : 50 },
318+
{ width: horizontal ? 150 : 0, height: horizontal ? 0 : 150 },
319+
horizontal ? (negate ? -100 : 100) : 0,
320+
horizontal ? 0 : negate ? -100 : 100,
321+
);
322+
323+
// assert
324+
const style = window.getComputedStyle(sut);
325+
expect(size(style)).toBe("calc(100px + -100px)");
326+
});
327+
328+
test(`${name} subsequent resize has correct styles`, async () => {
329+
// arrange
330+
const { container } = render(
331+
<ViewPort>
332+
{mutateComponent(component, { id: "test", resizable: [ResizeType.Left, ResizeType.Top, ResizeType.Bottom, ResizeType.Right] })}
333+
</ViewPort>,
334+
);
335+
const resizeHandle = container.querySelector(`#test-${handle}`)!;
336+
const sut = container.querySelector("#test")!;
337+
338+
// act
339+
drag(
340+
resizeHandle,
341+
sut,
342+
{ width: horizontal ? 50 : 0, height: horizontal ? 0 : 50 },
343+
{ width: horizontal ? 150 : 0, height: horizontal ? 0 : 150 },
344+
horizontal ? (negate ? -100 : 100) : 0,
345+
horizontal ? 0 : negate ? -100 : 100,
346+
);
347+
drag(
348+
resizeHandle,
349+
sut,
350+
{ width: horizontal ? 150 : 0, height: horizontal ? 0 : 150 },
351+
{ width: horizontal ? 50 : 0, height: horizontal ? 0 : 50 },
352+
horizontal ? (negate ? 100 : -100) : 0,
353+
horizontal ? 0 : negate ? 100 : -100,
354+
);
355+
356+
// assert
357+
const style = window.getComputedStyle(sut);
358+
expect(size(style)).toBe("100px");
359+
});
360+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as React from "react";
2+
import { cleanup } from "@testing-library/react";
3+
import { Positioned } from "../../Positioned";
4+
import "@testing-library/jest-dom/extend-expect";
5+
import { commonPositionedTests, commonPropsTests } from "../Common";
6+
7+
afterEach(cleanup);
8+
9+
const positionedLeftTopRightBottom = <Positioned left={100} top={100} right={100} bottom={100} />;
10+
const positionedLeftTopWidthHeight = <Positioned left={100} top={100} width={100} height={100} />;
11+
12+
commonPropsTests("Positioned (left/top/right/bottom)", positionedLeftTopRightBottom, {
13+
position: "absolute",
14+
left: "100px",
15+
top: "100px",
16+
right: "100px",
17+
bottom: "100px",
18+
width: "",
19+
height: "",
20+
});
21+
22+
commonPropsTests("Positioned (left/top/width/height)", positionedLeftTopWidthHeight, {
23+
position: "absolute",
24+
left: "100px",
25+
top: "100px",
26+
right: "",
27+
bottom: "",
28+
width: "100px",
29+
height: "100px",
30+
});
31+
32+
commonPositionedTests(
33+
"Positioned left resize (left/top/width/height)",
34+
positionedLeftTopWidthHeight,
35+
(style) => style.width,
36+
(style) => style.left,
37+
(style) => style.right,
38+
"ml",
39+
true,
40+
false,
41+
);
42+
43+
commonPositionedTests(
44+
"Positioned right resize (left/top/width/height)",
45+
positionedLeftTopWidthHeight,
46+
(style) => style.width,
47+
(style) => style.right,
48+
(style) => style.left,
49+
"mr",
50+
true,
51+
true,
52+
);
53+
54+
commonPositionedTests(
55+
"Positioned top resize (left/top/width/height)",
56+
positionedLeftTopWidthHeight,
57+
(style) => style.height,
58+
(style) => style.top,
59+
(style) => style.bottom,
60+
"mt",
61+
false,
62+
false,
63+
);
64+
65+
commonPositionedTests(
66+
"Positioned bottom resize (left/top/width/height)",
67+
positionedLeftTopWidthHeight,
68+
(style) => style.height,
69+
(style) => style.bottom,
70+
(style) => style.top,
71+
"mb",
72+
false,
73+
true,
74+
);

0 commit comments

Comments
 (0)