Skip to content

Commit 7661f2e

Browse files
committed
Added prop types tests
1 parent ba4aaa8 commit 7661f2e

File tree

19 files changed

+149
-40
lines changed

19 files changed

+149
-40
lines changed

src/components/Anchored.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface IResizableProps extends IReactSpaceCommonProps {
88
size: SizeUnit;
99
order?: number;
1010
handleSize?: number;
11+
touchHandleSize?: number;
1112
handlePlacement?: ResizeHandlePlacement;
1213
handleRender?: (handleProps: IResizeHandleProps) => React.ReactNode;
1314
minimumSize?: number;
@@ -22,6 +23,7 @@ export const resizableProps = {
2223
size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
2324
order: PropTypes.number,
2425
handleSize: PropTypes.number,
26+
touchHandleSize: PropTypes.number,
2527
handlePlacement: PropTypes.oneOf([ResizeHandlePlacement.Inside, ResizeHandlePlacement.OverlayBoundary, ResizeHandlePlacement.OverlayInside]),
2628
handleRender: PropTypes.func,
2729
minimumSize: PropTypes.number,

src/components/Custom.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { IReactSpaceCommonProps } from "../core-react";
66
import { anchoredProps, IAnchorProps } from "./Anchored";
77

88
type ICustomProps = Omit<IReactSpaceCommonProps & IAnchorProps, "size"> & {
9+
type?: Type;
10+
911
// Anchored
1012
anchor?: AnchorType;
1113
anchorSize?: SizeUnit;
@@ -17,13 +19,14 @@ type ICustomProps = Omit<IReactSpaceCommonProps & IAnchorProps, "size"> & {
1719
bottom?: SizeUnit | undefined;
1820
width?: SizeUnit | undefined;
1921
height?: SizeUnit | undefined;
20-
isPositioned?: boolean;
2122
resizeTypes?: ResizeType[];
2223
};
2324

2425
const customProps = {
2526
...anchoredProps,
2627
...{
28+
type: PropTypes.oneOf([Type.Positioned, Type.Fill, Type.Anchored]),
29+
2730
anchor: PropTypes.oneOf([AnchorType.Left, AnchorType.Top, AnchorType.Right, AnchorType.Bottom]),
2831
anchorSize: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
2932

@@ -33,13 +36,13 @@ const customProps = {
3336
bottom: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
3437
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
3538
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
36-
isPositioned: PropTypes.bool,
37-
resizeTypes: PropTypes.any,
39+
resizeTypes: PropTypes.array,
3840
},
3941
};
4042

4143
export const Custom: React.FC<ICustomProps> = ({
4244
children,
45+
type,
4346
left,
4447
top,
4548
right,
@@ -48,15 +51,27 @@ export const Custom: React.FC<ICustomProps> = ({
4851
height,
4952
anchorSize,
5053
anchor,
51-
isPositioned,
5254
resizable,
5355
resizeTypes,
5456
...props
5557
}) => {
5658
let position: IPositionalProps;
57-
let type = Type.Positioned;
59+
type = type || Type.Fill;
5860

59-
if (!isPositioned) {
61+
if (type === Type.Positioned) {
62+
position = {
63+
left: left,
64+
top: top,
65+
right: right,
66+
bottom: bottom,
67+
width: width,
68+
height: height,
69+
leftResizable: resizeTypes && resizeTypes.includes(ResizeType.Left),
70+
topResizable: resizeTypes && resizeTypes.includes(ResizeType.Top),
71+
rightResizable: resizeTypes && resizeTypes.includes(ResizeType.Right),
72+
bottomResizable: resizeTypes && resizeTypes.includes(ResizeType.Bottom),
73+
};
74+
} else {
6075
if (anchor === AnchorType.Left) {
6176
position = { left: 0, top: 0, bottom: 0, width: anchorSize, rightResizable: resizable };
6277
type = Type.Anchored;
@@ -78,19 +93,6 @@ export const Custom: React.FC<ICustomProps> = ({
7893
};
7994
type = Type.Fill;
8095
}
81-
} else {
82-
position = {
83-
left: left,
84-
top: top,
85-
right: right,
86-
bottom: bottom,
87-
width: width,
88-
height: height,
89-
leftResizable: resizeTypes && resizeTypes.includes(ResizeType.Left),
90-
topResizable: resizeTypes && resizeTypes.includes(ResizeType.Top),
91-
rightResizable: resizeTypes && resizeTypes.includes(ResizeType.Right),
92-
bottomResizable: resizeTypes && resizeTypes.includes(ResizeType.Bottom),
93-
};
9496
}
9597

9698
return (

src/components/stories/02-components/Fixed.stories.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Meta, Story, Canvas, Props } from "@storybook/addon-docs";
22
import { withKnobs } from "@storybook/addon-knobs";
33
import { Fixed, Centered, LeftResizable, TopResizable, RightResizable, BottomResizable, Fill } from "../../";
44
import { red, green, blue, description } from "../Utils";
5-
import { CommonHeader, PropsTable, PropsHeader, Prop } from "../Utils";
5+
import { CommonHeader, PropsTable, PropsHeader, Prop, StandardProps } from "../Utils";
66

77
<CommonHeader />
88

@@ -39,8 +39,6 @@ Top-level space with a fixed height and optional width.
3939
<PropsHeader>Fixed properties</PropsHeader>
4040
<Prop name="width" type="number | string" description="Optional width of space. When not specified the space will fill 100% of it's container." />
4141
<Prop name="height" type="number | string" description="Height of space." />
42-
<PropsHeader>Standard properties</PropsHeader>
43-
<Prop name="className" type="string" description="A class name to apply to the space element." />
44-
<Prop name="style" type="CSSProperties" description="CSS properties" />
42+
<StandardProps />
4543
</PropsTable>
4644
</Story>

src/components/stories/02-components/ViewPort.stories.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Meta, Story, Canvas, Props } from "@storybook/addon-docs";
22
import { withKnobs } from "@storybook/addon-knobs";
33
import { ViewPort, LeftResizable, TopResizable, RightResizable, BottomResizable, Fill } from "../../";
44
import { red, green, blue, description } from "../Utils";
5-
import { CommonHeader, PropsTable, PropsHeader, Prop } from "../Utils";
5+
import { CommonHeader, PropsTable, PropsHeader, Prop, StandardProps } from "../Utils";
66

77
<CommonHeader />
88

@@ -74,8 +74,6 @@ window elements that may be on the page.
7474
<Prop name="left" type="number | string" description="Left offset from viewport edge" />
7575
<Prop name="right" type="number | string" description="Right offset from viewport edge" />
7676
<Prop name="top" type="number | string" description="Top offset from viewport edge" />
77-
<PropsHeader>Standard properties</PropsHeader>
78-
<Prop name="className" type="string" description="A class name to apply to the space element." />
79-
<Prop name="style" type="CSSProperties" description="CSS properties" />
77+
<StandardProps />
8078
</PropsTable>
8179
</Story>

src/components/stories/Utils.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,30 @@ export const StandardProps = () => (
125125
export const AnchoredProps = () => (
126126
<>
127127
<PropsHeader>Anchored properties</PropsHeader>
128-
<Prop name="size" type="string | number" description="Initial size of space specified as a percentage or in pixels." />
128+
<Prop
129+
name="resizable"
130+
type="boolean"
131+
default="false"
132+
description={
133+
<>
134+
Determines if space is resizable.{" "}
135+
<p>
136+
<strong>
137+
Note this is only available on Left, Top, Right and Bottom spaces to be able to switch non-resizable anchored spaces to
138+
resizable spaces without reparenting child components. On LeftResizable, TopResizable, RightResizable and BottomResizable
139+
this is inferred by default as true.
140+
</strong>
141+
</p>
142+
</>
143+
}
144+
/>
129145
</>
130146
);
131147

132148
export const ResizableProps = () => (
133149
<>
134150
<PropsHeader>Resizable properties</PropsHeader>
151+
<Prop name="size" type="string | number" description="Initial size of space specified as a percentage or in pixels." />
135152
<Prop name="handleSize" type="number" default="5" description="Size of the resize handle in pixels." />
136153
<Prop
137154
name="touchHandleSize"

src/components/tests/Common.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import { ViewPort } from "../ViewPort";
55
import { drag } from "./TestUtils";
66
import { ResizeType } from "../../core-types";
77
import { Positioned } from "../Positioned";
8+
import { IReactSpaceCommonProps } from "../../core-react";
89

910
export const mutateComponent = (component: React.ReactNode, newProps: Object) => {
1011
return React.cloneElement(component as React.DetailedReactHTMLElement<any, HTMLElement>, newProps);
1112
};
1213

14+
export function hasProps(name: string, component: React.FC<IReactSpaceCommonProps>, props: string[]) {
15+
props.forEach((prop) => {
16+
expect(prop in component.propTypes!, `${prop} missing`).toBe(true);
17+
});
18+
}
19+
1320
export const fixUp = (sut: HTMLElement) => {
1421
// This is annoying. A bug in JSDOM means that getComputedStyle() on test elements does
1522
// not correctly return the applied styles when a dynamically added style tag is added to
@@ -26,6 +33,54 @@ export const fixUp = (sut: HTMLElement) => {
2633
return sut;
2734
};
2835

36+
export function commonPropTypesTest(name: string, component: React.FC<IReactSpaceCommonProps>) {
37+
test(`${name} has correct common prop types`, async () => {
38+
hasProps(name, component, [
39+
"as",
40+
"centerContent",
41+
"className",
42+
"id",
43+
"scrollable",
44+
"trackSize",
45+
"zIndex",
46+
"allowOverflow",
47+
"onClick",
48+
"onDoubleClick",
49+
"onMouseDown",
50+
"onMouseEnter",
51+
"onMouseLeave",
52+
"onMouseMove",
53+
"onTouchStart",
54+
"onTouchMove",
55+
"onTouchEnd",
56+
]);
57+
});
58+
}
59+
60+
export function resizablePropTypesTest(name: string, component: React.FC<IReactSpaceCommonProps>) {
61+
commonPropTypesTest(name, component);
62+
test(`${name} has correct resizable prop types`, async () => {
63+
hasProps(name, component, [
64+
"size",
65+
"handleSize",
66+
"touchHandleSize",
67+
"handlePlacement",
68+
"handleRender",
69+
"minimumSize",
70+
"maximumSize",
71+
"onResizeStart",
72+
"onResizeEnd",
73+
]);
74+
});
75+
}
76+
77+
export function anchorPropTypesTest(name: string, component: React.FC<IReactSpaceCommonProps>) {
78+
resizablePropTypesTest(name, component);
79+
test(`${name} has correct anchor prop types`, async () => {
80+
hasProps(name, component, ["resizable"]);
81+
});
82+
}
83+
2984
export const commonPropsTests = (name: string, component: React.ReactNode, expectedStyle: Partial<CSSStyleDeclaration>) => {
3085
test(`${name} default has correct styles`, async () => {
3186
// arrange, act

src/components/tests/__tests__/Bottom.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import { cleanup } from "@testing-library/react";
33
import { Bottom } from "../../Anchored";
4-
import { commonPropsTests, commonAnchorTests } from "../Common";
4+
import { commonPropsTests, commonAnchorTests, anchorPropTypesTest } from "../Common";
55

66
afterEach(cleanup);
77

@@ -22,3 +22,5 @@ commonAnchorTests(
2222
(style) => style.bottom,
2323
(style) => style.top,
2424
);
25+
26+
anchorPropTypesTest("Bottom", Bottom);

src/components/tests/__tests__/BottomResizable.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22
import { cleanup } from "@testing-library/react";
33
import { BottomResizable } from "../../Anchored";
4-
import { commonPropsTests, commonAnchorTests, commonResizableTests } from "../Common";
4+
import { commonPropsTests, commonAnchorTests, commonResizableTests, resizablePropTypesTest } from "../Common";
55

66
afterEach(cleanup);
77

@@ -32,3 +32,5 @@ commonResizableTests(
3232
false,
3333
true,
3434
);
35+
36+
resizablePropTypesTest("BottomResizable", BottomResizable);

src/components/tests/__tests__/Fill.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Fill } from "../../Fill";
44
import { Left, Top, Right, Bottom, LeftResizable, TopResizable, RightResizable, BottomResizable } from "../../Anchored";
55
import "@testing-library/jest-dom/extend-expect";
66
import { ViewPort } from "../../ViewPort";
7-
import { commonPropsTests, fixUp } from "../Common";
7+
import { commonPropsTests, commonPropTypesTest, fixUp } from "../Common";
88

99
afterEach(cleanup);
1010

@@ -362,3 +362,5 @@ test("Fill with stacked BottomResizable has correct styles", async () => {
362362
expect(style.height).toBe("");
363363
expect(sut.nodeName).toBe("DIV");
364364
});
365+
366+
commonPropTypesTest("Fill", Fill);

src/components/tests/__tests__/Fixed.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import { render, cleanup } from "@testing-library/react";
33
import { Fixed } from "../../Fixed";
44
import "@testing-library/jest-dom/extend-expect";
5-
import { commonPropsTests } from "../Common";
5+
import { commonPropsTests, commonPropTypesTest, hasProps } from "../Common";
66

77
afterEach(cleanup);
88

@@ -28,3 +28,9 @@ test("Fixed with width and height has correct styles", async () => {
2828
expect(style.width).toBe("10px");
2929
expect(style.height).toBe("20px");
3030
});
31+
32+
commonPropTypesTest("Fixed", Fixed);
33+
34+
test(`Fixed has correct prop types`, async () => {
35+
hasProps("Fixed", Fixed, ["width", "height"]);
36+
});

0 commit comments

Comments
 (0)