Skip to content

Commit 55e767b

Browse files
committed
Add viewport example, using custom useMedia hook
1 parent 98c8cb4 commit 55e767b

File tree

12 files changed

+7070
-154
lines changed

12 files changed

+7070
-154
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"presets": [
3-
"@babel/preset-env"
3+
"@babel/preset-env",
4+
"@babel/preset-react"
45
]
56
}

.eslintrc.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:react/recommended",
10+
"plugin:jest/recommended"
11+
],
12+
"globals": {
13+
"Atomics": "readonly",
14+
"SharedArrayBuffer": "readonly"
15+
},
16+
"parserOptions": {
17+
"ecmaFeatures": {
18+
"jsx": true
19+
},
20+
"ecmaVersion": 2018,
21+
"sourceType": "module"
22+
},
23+
"plugins": [
24+
"react"
25+
],
26+
"rules": {
27+
}
28+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
3+
import useMedia from './useMedia'
4+
5+
const Component = () => {
6+
const isDesktop = useMedia('(min-width: 1280px)')
7+
8+
if (isDesktop === null) {
9+
return (
10+
<div>server</div>
11+
)
12+
}
13+
14+
return (
15+
<div>{isDesktop ? 'desktop' : 'not desktop'}</div>
16+
)
17+
}
18+
19+
export default Component
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react'
2+
import { render, act } from '@testing-library/react'
3+
import '@testing-library/jest-dom/extend-expect'
4+
5+
import { mockViewport } from '../../../src/mocks/viewport'
6+
7+
import Component from './Component'
8+
9+
const VIEWPORT_DESKTOP = { width: "1440px", height: "900px" }
10+
const VIEWPORT_MOBILE = { width: "320px", height: "568px" }
11+
12+
describe('It renders correctly on server, desktop and mobile', () => {
13+
it('works on the server', () => {
14+
const { getByText } = render(<Component />)
15+
16+
expect(getByText('server')).toBeInTheDocument();
17+
})
18+
19+
it('works on desktop', () => {
20+
const viewport = mockViewport(VIEWPORT_DESKTOP)
21+
22+
const { getByText } = render(<Component />);
23+
24+
expect(getByText('desktop')).toBeInTheDocument();
25+
26+
viewport.cleanup()
27+
})
28+
29+
it('works on mobile', () => {
30+
const viewport = mockViewport(VIEWPORT_MOBILE)
31+
32+
const { getByText } = render(<Component />);
33+
34+
expect(getByText('not desktop')).toBeInTheDocument();
35+
36+
viewport.cleanup()
37+
});
38+
39+
it('works on desktop and mobile, even if we change the viewport description', () => {
40+
const viewport = mockViewport(VIEWPORT_DESKTOP)
41+
42+
const { getByText, queryByText } = render(<Component />)
43+
44+
expect(getByText('desktop')).toBeInTheDocument()
45+
expect(queryByText('not desktop')).not.toBeInTheDocument()
46+
47+
act(() => {
48+
viewport.set(VIEWPORT_MOBILE)
49+
})
50+
51+
expect(getByText('not desktop')).toBeInTheDocument()
52+
expect(queryByText('desktop')).not.toBeInTheDocument()
53+
54+
viewport.cleanup()
55+
})
56+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
"@babel/preset-env",
5+
{
6+
targets: {
7+
node: "current"
8+
}
9+
}
10+
],
11+
"@babel/preset-react"
12+
]
13+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
transform: {
3+
"^.+\\.[t|j]sx?$": "babel-jest"
4+
},
5+
testPathIgnorePatterns: ["<rootDir>/node_modules/"]
6+
};

0 commit comments

Comments
 (0)