Skip to content

Commit

Permalink
Update createBreakpoin.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeeou authored Nov 26, 2019
1 parent 8028561 commit db87762
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions tests/createBreakpoin.test.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,37 @@
import { act, renderHook } from '@testing-library/react-hooks';
import createBreakpoint from '../src/createBreakpoint';

const TestScreenSize = {
'100': 100,
'200': 200,
'300': 300,
'400': 400,
'500': 500,
'600': 600,
'700': 700,
'800': 800,
'900': 900,
'1000': 1000,
'1100': 1100,
'1200': 1200,
'1300': 1300,
'1400': 1400,
'1500': 1500,
'1600': 1600,
};

const useBreakpointA = createBreakpoint();
const useBreakpointB = createBreakpoint({ mobileM: 350, laptop: 1024, tablet: 768 });

const orginInnerWidth = window.innerWidth;
const originalInnerWidth = window.innerWidth;
const changeInnerWidth = value =>
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value });
const revert = () => changeInnerWidth(orginInnerWidth);
const revert = () => changeInnerWidth(originalInnerWidth);

describe('createBreakpoint', () => {
test('should use default', () => {
const { result } = renderHook(() => useBreakpointA());
act(() => {
changeInnerWidth(TestScreenSize['100']);
changeInnerWidth(100);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('tablet');

act(() => {
changeInnerWidth(TestScreenSize['200']);
changeInnerWidth(200);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('tablet');

act(() => {
changeInnerWidth(TestScreenSize['1100']);
changeInnerWidth(1100);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('laptop');

act(() => {
changeInnerWidth(TestScreenSize['1500']);
changeInnerWidth(1500);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('laptopL');
Expand All @@ -63,25 +44,25 @@ describe('createBreakpoint', () => {
test('should use custom', () => {
const { result } = renderHook(() => useBreakpointB());
act(() => {
changeInnerWidth(TestScreenSize['100']);
changeInnerWidth(100);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('mobileM');

act(() => {
changeInnerWidth(TestScreenSize['200']);
changeInnerWidth(200);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('mobileM');

act(() => {
changeInnerWidth(TestScreenSize['800']);
changeInnerWidth(800);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('tablet');

act(() => {
changeInnerWidth(TestScreenSize['1100']);
changeInnerWidth(1100);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('laptop');
Expand Down

0 comments on commit db87762

Please sign in to comment.