Skip to content

Commit

Permalink
test: add createBreakpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 8, 2019
2 parents 5c53d71 + 29453ef commit dbee45c
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/createBreakpoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { act, renderHook } from '@testing-library/react-hooks';
import createBreakpoint from '../src/createBreakpoint';

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

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

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

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

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

act(() => {
changeInnerWidth(1500);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('laptopL');

act(() => {
revert();
});
});

test('should use custom', () => {
const { result } = renderHook(() => useBreakpointB());
act(() => {
changeInnerWidth(100);
window.dispatchEvent(new Event('resize'));
});
expect(result.current).toBe('mobileM');

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

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

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

act(() => {
revert();
});
});
});

0 comments on commit dbee45c

Please sign in to comment.