Skip to content

Commit bc56e10

Browse files
committed
Added drag tests for positioned space
1 parent ca78b67 commit bc56e10

File tree

2 files changed

+119
-7
lines changed

2 files changed

+119
-7
lines changed

src/components/tests/Common.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export const commonResizableTests = (
290290
});
291291
};
292292

293-
export const commonPositionedTests = (
293+
export const commonPositionedResizeTests = (
294294
name: string,
295295
size: (style: CSSStyleDeclaration) => string | null,
296296
edge: (style: CSSStyleDeclaration) => string | null,

src/components/tests/__tests__/Positioned.tsx

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as React from "react";
2-
import { cleanup } from "@testing-library/react";
2+
import { cleanup, render } from "@testing-library/react";
33
import { Positioned } from "../../Positioned";
44
import "@testing-library/jest-dom/extend-expect";
5-
import { commonPositionedTests, commonPropsTests } from "../Common";
5+
import { commonPositionedResizeTests, commonPropsTests, mutateComponent } from "../Common";
6+
import { useCurrentSpace } from "../../../core-react";
7+
import { ViewPort } from "../../ViewPort";
8+
import { drag } from "../TestUtils";
69

710
afterEach(cleanup);
811

@@ -29,7 +32,7 @@ commonPropsTests("Positioned (left/top/width/height)", positionedLeftTopWidthHei
2932
height: "100px",
3033
});
3134

32-
commonPositionedTests(
35+
commonPositionedResizeTests(
3336
"Positioned left resize",
3437
/* size */ (style) => style.width,
3538
/* edge */ (style) => style.left,
@@ -39,7 +42,7 @@ commonPositionedTests(
3942
/* negate */ false,
4043
);
4144

42-
commonPositionedTests(
45+
commonPositionedResizeTests(
4346
"Positioned right resize",
4447
/* size */ (style) => style.width,
4548
/* edge */ (style) => style.right,
@@ -49,7 +52,7 @@ commonPositionedTests(
4952
/* negate */ true,
5053
);
5154

52-
commonPositionedTests(
55+
commonPositionedResizeTests(
5356
"Positioned top resize",
5457
/* size */ (style) => style.height,
5558
/* edge */ (style) => style.top,
@@ -59,7 +62,7 @@ commonPositionedTests(
5962
/* negate */ false,
6063
);
6164

62-
commonPositionedTests(
65+
commonPositionedResizeTests(
6366
"Positioned bottom resize",
6467
/* size */ (style) => style.height,
6568
/* edge */ (style) => style.bottom,
@@ -68,3 +71,112 @@ commonPositionedTests(
6871
/* horizontal */ false,
6972
/* negate */ true,
7073
);
74+
75+
const testProps = { id: "test" };
76+
77+
[
78+
{ name: "left/top/width/height", props: { left: 100, top: 100, width: 100, height: 100 }, widthHeightSpecified: true },
79+
{ name: "left/top/right/bottom", props: { left: 100, top: 100, right: 100, bottom: 100 }, widthHeightSpecified: false },
80+
].forEach((testCase) => {
81+
test(`Positioned (${testCase.name}) after drag has correct styles`, async () => {
82+
const Inner = () => {
83+
const space = useCurrentSpace();
84+
return (
85+
<button id="test-drag-handle" onMouseDown={space.startMouseDrag} onTouchStart={space.startTouchDrag}>
86+
Drag handle
87+
</button>
88+
);
89+
};
90+
91+
const { container } = render(
92+
<ViewPort>
93+
{mutateComponent(
94+
<Positioned>
95+
<Inner />
96+
</Positioned>,
97+
{ ...testProps, ...testCase.props },
98+
)}
99+
</ViewPort>,
100+
);
101+
const sut = container.querySelector("#test")!;
102+
const dragHandle = container.querySelector(`#test-drag-handle`)!;
103+
104+
// act
105+
drag(
106+
dragHandle,
107+
sut,
108+
/* start rect */ { width: 50, height: 50 },
109+
/* end rect */ { width: 150, height: 150 },
110+
/* end X */ 100,
111+
/* end Y */ 100,
112+
);
113+
114+
// assert
115+
const style = window.getComputedStyle(sut);
116+
117+
if (testCase.widthHeightSpecified) {
118+
expect(style.width).toBe("100px");
119+
expect(style.height).toBe("100px");
120+
} else {
121+
expect(style.left).toBe("calc(100px + 100px)");
122+
expect(style.right).toBe("calc(100px + -100px)");
123+
expect(style.top).toBe("calc(100px + 100px)");
124+
expect(style.bottom).toBe("calc(100px + -100px)");
125+
}
126+
});
127+
128+
test(`Positioned (${testCase.name}) after subsequent drag has correct styles`, async () => {
129+
const Inner = () => {
130+
const space = useCurrentSpace();
131+
return (
132+
<button id="test-drag-handle" onMouseDown={space.startMouseDrag} onTouchStart={space.startTouchDrag}>
133+
Drag handle
134+
</button>
135+
);
136+
};
137+
138+
const { container } = render(
139+
<ViewPort>
140+
{mutateComponent(
141+
<Positioned>
142+
<Inner />
143+
</Positioned>,
144+
{ ...testProps, ...testCase.props },
145+
)}
146+
</ViewPort>,
147+
);
148+
const sut = container.querySelector("#test")!;
149+
const dragHandle = container.querySelector(`#test-drag-handle`)!;
150+
151+
// act
152+
drag(
153+
dragHandle,
154+
sut,
155+
/* start rect */ { width: 50, height: 50 },
156+
/* end rect */ { width: 150, height: 150 },
157+
/* end X */ 100,
158+
/* end Y */ 100,
159+
);
160+
drag(
161+
dragHandle,
162+
sut,
163+
/* start rect */ { width: 150, height: 150 },
164+
/* end rect */ { width: 50, height: 50 },
165+
/* end X */ -100,
166+
/* end Y */ -100,
167+
);
168+
169+
// assert
170+
const style = window.getComputedStyle(sut);
171+
172+
if (testCase.widthHeightSpecified) {
173+
expect(style.width).toBe("100px");
174+
expect(style.height).toBe("100px");
175+
} else {
176+
expect(style.left).toBe("calc(100px + 0px)");
177+
expect(style.right).toBe("calc(100px + 0px)");
178+
expect(style.top).toBe("calc(100px + 0px)");
179+
expect(style.bottom).toBe("calc(100px + 0px)");
180+
}
181+
});
182+
});

0 commit comments

Comments
 (0)