Skip to content

Commit 2a3294f

Browse files
test: add tests for useBreakpoint and useWindowSize hooks
1 parent 5bfead1 commit 2a3294f

File tree

5 files changed

+178
-3
lines changed

5 files changed

+178
-3
lines changed

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@semantic-release/release-notes-generator": "7.1.4",
3434
"@types/jest": "24.0.13",
3535
"@types/react": "16.8.17",
36+
"@types/react-dom": "16.8.4",
3637
"@typescript-eslint/eslint-plugin": "1.9.0",
3738
"@typescript-eslint/parser": "1.9.0",
3839
"commitlint": "7.6.1",
@@ -45,6 +46,7 @@
4546
"jest": "24.8.0",
4647
"prettier": "1.17.1",
4748
"react": "16.8.6",
49+
"react-dom": "16.8.6",
4850
"semantic-release": "15.13.12",
4951
"ts-jest": "24.0.2",
5052
"typescript": "3.4.5"

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { useBreakpoint, useBreakpoints } from './useBreakpoint';
2-
export { useComponentSize } from './useComponentSize';
3-
export { useWindowSize } from './useWindowSize';
1+
export * from './useBreakpoint';
2+
export * from './useComponentSize';
3+
export * from './useWindowSize';

tests/useBreakpoint.spec.tsx

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import { act } from 'react-dom/test-utils';
4+
5+
import { useBreakpoint, useBreakpoints } from '../src';
6+
7+
describe('useBreakpoint', () => {
8+
let container: Element;
9+
10+
beforeEach(() => {
11+
container = document.createElement('div');
12+
document.body.appendChild(container);
13+
});
14+
15+
afterEach(() => {
16+
document.body.removeChild(container);
17+
});
18+
19+
it('should switch component based on a single breakpoint', () => {
20+
const Component = () => {
21+
const isSmall = useBreakpoint(500);
22+
23+
if (isSmall) {
24+
return <p id="small">Small</p>
25+
}
26+
27+
return <p id="big">Big</p>
28+
}
29+
30+
act(() => {
31+
ReactDOM.render(<Component />, container);
32+
});
33+
34+
let big = container.querySelector('#big');
35+
let small = container.querySelector('#small');
36+
expect(big && big.textContent).toEqual('Big');
37+
expect(small).toBeNull();
38+
39+
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 400 })
40+
41+
act(() => {
42+
window.dispatchEvent(new Event('resize'))
43+
})
44+
45+
big = container.querySelector('#big');
46+
small = container.querySelector('#small');
47+
expect(small && small.textContent).toEqual('Small');
48+
expect(big).toBeNull();
49+
})
50+
51+
it('should switch component based on multiple breakpoints', () => {
52+
const Component = () => {
53+
const [isSmall, isMedium] = useBreakpoints([500, 700]);
54+
55+
if (isSmall) {
56+
return <p id="small">Small</p>
57+
}
58+
59+
if (isMedium) {
60+
return <p id="medium">Medium</p>
61+
}
62+
63+
return <p id="big">Big</p>
64+
}
65+
66+
act(() => {
67+
ReactDOM.render(<Component />, container);
68+
});
69+
70+
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 400 })
71+
act(() => {
72+
window.dispatchEvent(new Event('resize'))
73+
})
74+
75+
let big = container.querySelector('#big');
76+
let medium = container.querySelector('#medium');
77+
let small = container.querySelector('#small');
78+
expect(medium).toBeNull();
79+
expect(big).toBeNull();
80+
expect(small && small.textContent).toEqual('Small');
81+
82+
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 })
83+
act(() => {
84+
window.dispatchEvent(new Event('resize'))
85+
})
86+
87+
big = container.querySelector('#big');
88+
medium = container.querySelector('#medium');
89+
small = container.querySelector('#small');
90+
expect(small).toBeNull();
91+
expect(big).toBeNull();
92+
expect(medium && medium.textContent).toEqual('Medium');
93+
94+
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 800 })
95+
act(() => {
96+
window.dispatchEvent(new Event('resize'))
97+
})
98+
99+
big = container.querySelector('#big');
100+
medium = container.querySelector('#medium');
101+
small = container.querySelector('#small');
102+
expect(small).toBeNull();
103+
expect(medium).toBeNull();
104+
expect(big && big.textContent).toEqual('Big');
105+
})
106+
});

tests/useWindowSize.spec.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import { act } from 'react-dom/test-utils';
4+
5+
import { useWindowSize } from '../src';
6+
7+
describe('useWindowSize', () => {
8+
let container: Element;
9+
10+
beforeEach(() => {
11+
container = document.createElement('div');
12+
document.body.appendChild(container);
13+
});
14+
15+
afterEach(() => {
16+
document.body.removeChild(container);
17+
});
18+
19+
it('should return the window size', () => {
20+
const Component = () => {
21+
const { width, height } = useWindowSize();
22+
23+
return <><p id="height">{height}</p><p id="width">{width}</p></>
24+
}
25+
26+
act(() => {
27+
ReactDOM.render(<Component />, container);
28+
});
29+
30+
const height = container.querySelector('#height');
31+
const width = container.querySelector('#width');
32+
33+
expect(height && height.textContent).toEqual('768');
34+
expect(width && width.textContent).toEqual('1024');
35+
36+
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 500 })
37+
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 200 })
38+
39+
act(() => {
40+
window.dispatchEvent(new Event('resize'))
41+
})
42+
43+
expect(height && height.textContent).toEqual('200');
44+
expect(width && width.textContent).toEqual('500');
45+
})
46+
});

0 commit comments

Comments
 (0)