Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 1f7a377

Browse files
authored
Merge pull request #84 from wcandillon/tests
Add extensive integration tests
2 parents bf94ba0 + b31d424 commit 1f7a377

File tree

9 files changed

+466
-23
lines changed

9 files changed

+466
-23
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"devDependencies": {
5050
"@commitlint/config-conventional": "^11.0.0",
5151
"@release-it/conventional-changelog": "^2.0.0",
52+
"@testing-library/react-native": "^8.0.0-rc.1",
5253
"@types/jest": "^26.0.0",
5354
"@types/lodash": "^4.14.168",
5455
"@types/react": "^16.9.19",
@@ -61,6 +62,7 @@
6162
"pod-install": "^0.1.0",
6263
"prettier": "^2.0.5",
6364
"react": "16.13.1",
65+
"react-test-renderer": "16.13.1",
6466
"react-native": "0.63.4",
6567
"react-native-builder-bob": "^0.18.0",
6668
"release-it": "^14.2.2",
@@ -75,9 +77,11 @@
7577
},
7678
"jest": {
7779
"preset": "react-native",
80+
"setupFilesAfterEnv": ["./src/__tests__/setup.ts"],
7881
"modulePathIgnorePatterns": [
7982
"<rootDir>/example/node_modules",
80-
"<rootDir>/lib/"
83+
"<rootDir>/lib/",
84+
"<rootDir>/src/__tests__/setup.ts"
8185
]
8286
},
8387
"commitlint": {

src/MediaQuery.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PixelRatio, Platform } from "react-native";
1+
import type * as React from "react";
2+
import { PixelRatio, Platform, PlatformOSType } from "react-native";
23

34
import useDimensions from "./useDimensions";
45

@@ -15,7 +16,7 @@ export interface MediaQuery {
1516
maxPixelRatio?: number;
1617
orientation?: Orientation;
1718
condition?: boolean;
18-
platform?: string;
19+
platform?: PlatformOSType;
1920
}
2021

2122
export const isInInterval = (
@@ -26,22 +27,23 @@ export const isInInterval = (
2627
(min === undefined || value >= min) && (max === undefined || value <= max);
2728

2829
export const mediaQuery = (
29-
{
30+
query: MediaQuery,
31+
width: number,
32+
height: number
33+
): boolean => {
34+
const {
3035
minWidth,
3136
maxWidth,
3237
minHeight,
3338
maxHeight,
3439
minAspectRatio,
3540
maxAspectRatio,
36-
orientation,
37-
platform,
3841
minPixelRatio,
3942
maxPixelRatio,
43+
orientation,
44+
platform,
4045
condition,
41-
}: MediaQuery,
42-
width: number,
43-
height: number
44-
): boolean => {
46+
} = query;
4547
const currentOrientation: Orientation =
4648
width > height ? "landscape" : "portrait";
4749
return (
@@ -55,18 +57,11 @@ export const mediaQuery = (
5557
);
5658
};
5759

58-
interface MediaQueryProps extends MediaQuery {
59-
children: React.ReactNode;
60-
}
61-
62-
const MediaQuery = ({
63-
children,
64-
...props
65-
}: MediaQueryProps): React.ReactNode => {
60+
const MediaQuery: React.FC<MediaQuery> = ({ children, ...props }) => {
6661
const { width, height } = useDimensions();
6762
const val = mediaQuery(props, width, height);
6863
if (val) {
69-
return children;
64+
return children as JSX.Element;
7065
}
7166
return null;
7267
};

src/__tests__/MediaQuery.test.tsx

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import React from "react";
2+
import { Dimensions, Platform, Text } from "react-native";
3+
import { render } from "@testing-library/react-native";
4+
5+
import MediaQuery, { isInInterval, mediaQuery } from "../MediaQuery";
6+
7+
const MediaQueryTestComp = ({ minHeight }: { minHeight: number }) => {
8+
return (
9+
<MediaQuery minHeight={minHeight}>
10+
<Text>I am visible</Text>
11+
</MediaQuery>
12+
);
13+
};
14+
15+
test("isInInterval", () => {
16+
expect(isInInterval(5, 1, 10)).toBeTruthy();
17+
expect(isInInterval(11, 1, 10)).toBeFalsy();
18+
expect(isInInterval(0, 1, 10)).toBeFalsy();
19+
});
20+
21+
describe("mediaQuery", () => {
22+
test("minHeight should be false if too low", () => {
23+
const width = 100;
24+
const height = 100;
25+
const res = mediaQuery({ minHeight: 450 }, width, height);
26+
27+
expect(res).toBeFalsy();
28+
});
29+
30+
test("minHeight should be true if above", () => {
31+
const width = 100;
32+
const height = 500;
33+
const res = mediaQuery({ minHeight: 450 }, width, height);
34+
35+
expect(res).toBeTruthy();
36+
});
37+
38+
test("maxHeight should be true if lower", () => {
39+
const width = 100;
40+
const height = 100;
41+
const res = mediaQuery({ maxHeight: 450 }, width, height);
42+
43+
expect(res).toBeTruthy();
44+
});
45+
46+
test("maxHeight should be false if above", () => {
47+
const width = 100;
48+
const height = 500;
49+
const res = mediaQuery({ maxHeight: 450 }, width, height);
50+
51+
expect(res).toBeFalsy();
52+
});
53+
54+
test("minWidth should be false if too low", () => {
55+
const width = 100;
56+
const height = 100;
57+
const res = mediaQuery({ minWidth: 450 }, width, height);
58+
59+
expect(res).toBeFalsy();
60+
});
61+
62+
test("minWidth should be true if above", () => {
63+
const width = 500;
64+
const height = 100;
65+
const res = mediaQuery({ minWidth: 450 }, width, height);
66+
67+
expect(res).toBeTruthy();
68+
});
69+
70+
test("maxWidth should be true if lower", () => {
71+
const width = 100;
72+
const height = 100;
73+
const res = mediaQuery({ maxWidth: 450 }, width, height);
74+
75+
expect(res).toBeTruthy();
76+
});
77+
78+
test("maxWidth should be false if above", () => {
79+
const width = 500;
80+
const height = 100;
81+
const res = mediaQuery({ maxWidth: 450 }, width, height);
82+
83+
expect(res).toBeFalsy();
84+
});
85+
86+
test("minAspectRatio should be false if too low", () => {
87+
const width = 100;
88+
const height = 200;
89+
const res = mediaQuery({ minAspectRatio: 1 }, width, height);
90+
91+
expect(res).toBeFalsy();
92+
});
93+
94+
test("minAspectRatio should be true if above", () => {
95+
const width = 200;
96+
const height = 100;
97+
const res = mediaQuery({ minAspectRatio: 1 }, width, height);
98+
99+
expect(res).toBeTruthy();
100+
});
101+
102+
test("maxAspectRatio should be true if lower", () => {
103+
const width = 100;
104+
const height = 200;
105+
const res = mediaQuery({ maxAspectRatio: 1 }, width, height);
106+
107+
expect(res).toBeTruthy();
108+
});
109+
110+
test("maxAspectRatio should be false if above", () => {
111+
const width = 200;
112+
const height = 100;
113+
const res = mediaQuery({ maxAspectRatio: 1 }, width, height);
114+
115+
expect(res).toBeFalsy();
116+
});
117+
118+
test("minPixelRatio should be false if too low", () => {
119+
Dimensions.set({ pixelRatio: 0 });
120+
const res = mediaQuery({ minPixelRatio: 1 }, 0, 0);
121+
122+
expect(res).toBeFalsy();
123+
});
124+
125+
test("minPixelRatio should be true if above", () => {
126+
Dimensions.set({ pixelRatio: 2 });
127+
const res = mediaQuery({ minPixelRatio: 1 }, 0, 0);
128+
129+
expect(res).toBeTruthy();
130+
});
131+
132+
test("maxPixelRatio should be true if lower", () => {
133+
Dimensions.set({ pixelRatio: 0 });
134+
const res = mediaQuery({ maxPixelRatio: 1 }, 0, 0);
135+
136+
expect(res).toBeTruthy();
137+
});
138+
139+
test("maxPixelRatio should be false if above", () => {
140+
Dimensions.set({ pixelRatio: 2 });
141+
const res = mediaQuery({ maxPixelRatio: 1 }, 0, 0);
142+
143+
expect(res).toBeFalsy();
144+
});
145+
146+
test("orientation landscape should fail when expected", () => {
147+
const width = 100;
148+
const height = 200;
149+
const res = mediaQuery({ orientation: "landscape" }, width, height);
150+
151+
expect(res).toBeFalsy();
152+
});
153+
154+
test("orientation landscape should pass when expected", () => {
155+
const width = 200;
156+
const height = 100;
157+
const res = mediaQuery({ orientation: "landscape" }, width, height);
158+
159+
expect(res).toBeTruthy();
160+
});
161+
162+
test("orientation portrait should fail when expected", () => {
163+
const width = 200;
164+
const height = 100;
165+
const res = mediaQuery({ orientation: "portrait" }, width, height);
166+
167+
expect(res).toBeFalsy();
168+
});
169+
170+
test("orientation portrait should pass when expected", () => {
171+
const width = 100;
172+
const height = 200;
173+
const res = mediaQuery({ orientation: "portrait" }, width, height);
174+
175+
expect(res).toBeTruthy();
176+
});
177+
178+
test("platform should match", () => {
179+
Platform.OS = "web";
180+
const res = mediaQuery({ platform: "web" }, 0, 0);
181+
182+
expect(res).toBeTruthy();
183+
});
184+
185+
test("platform should fail", () => {
186+
Platform.OS = "ios";
187+
const res = mediaQuery({ platform: "web" }, 0, 0);
188+
189+
expect(res).toBeFalsy();
190+
});
191+
192+
test("condition should match", () => {
193+
const res = mediaQuery({ condition: true }, 0, 0);
194+
195+
expect(res).toBeTruthy();
196+
});
197+
198+
test("condition should fail", () => {
199+
const res = mediaQuery({ condition: false }, 0, 0);
200+
201+
expect(res).toBeFalsy();
202+
});
203+
});
204+
205+
describe("MediaQuery Comp", () => {
206+
test("should render when constraints set", async () => {
207+
const { queryByText } = render(<MediaQueryTestComp minHeight={50} />);
208+
209+
Dimensions.set({ height: 100, width: 100 });
210+
211+
expect(queryByText("I am visible")).toBeTruthy();
212+
});
213+
214+
test("should not render when constraints not set", async () => {
215+
const { queryByText } = render(<MediaQueryTestComp minHeight={150} />);
216+
217+
Dimensions.set({ height: 100, width: 100 });
218+
219+
expect(queryByText("I am visible")).toBeFalsy();
220+
});
221+
222+
test("should re-render when constraints updated", async () => {
223+
const { queryByText } = render(<MediaQueryTestComp minHeight={150} />);
224+
225+
Dimensions.set({ height: 100, width: 100 });
226+
227+
expect(queryByText("I am visible")).toBeFalsy();
228+
229+
Dimensions.set({ height: 200, width: 200 });
230+
231+
expect(queryByText("I am visible")).toBeTruthy();
232+
});
233+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from "react";
2+
import { Dimensions, Text, View } from "react-native";
3+
import { render } from "@testing-library/react-native";
4+
5+
import ResponsiveComponent from "../ResponsiveComponent";
6+
7+
class RespTestComp extends ResponsiveComponent {
8+
render() {
9+
const { width, height } = this.state.window;
10+
return (
11+
<View>
12+
<Text>Width: {width}</Text>
13+
<Text>Height: {height}</Text>
14+
</View>
15+
);
16+
}
17+
}
18+
19+
describe("ResponsiveComponent", () => {
20+
test("should display proper sizing", async () => {
21+
Dimensions.set({ height: 100, width: 100 });
22+
23+
const { queryByText } = render(<RespTestComp />);
24+
25+
expect(queryByText("Width: 100")).toBeTruthy();
26+
expect(queryByText("Height: 100")).toBeTruthy();
27+
28+
Dimensions.set({ height: 200, width: 200 });
29+
30+
expect(queryByText("Width: 200")).toBeTruthy();
31+
expect(queryByText("Height: 200")).toBeTruthy();
32+
});
33+
});

src/__tests__/index.test.tsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)