-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useWindowSize): A bit changed lyfecycle and added types;
feat(useWindowSize): Rewritten tests - covers more cases and will work on next @testing-library/react-hooks release (been broken before);
- Loading branch information
Showing
4 changed files
with
58 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,88 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { replaceRaf } from 'raf-stub'; | ||
import useWindowSize from '../useWindowSize'; | ||
import { isClient } from '../util'; | ||
|
||
interface RequestAnimationFrame { | ||
reset(): void; | ||
step(): void; | ||
} | ||
declare var requestAnimationFrame: { | ||
reset: () => void; | ||
step: (steps?: number, duration?: number) => void; | ||
}; | ||
|
||
declare var requestAnimationFrame: RequestAnimationFrame; | ||
describe('useWindowSize', () => { | ||
beforeAll(() => { | ||
replaceRaf(); | ||
}); | ||
|
||
replaceRaf(); | ||
afterEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
|
||
beforeEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
it('should be defined', () => { | ||
expect(useWindowSize).toBeDefined(); | ||
}); | ||
|
||
afterEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
function getHook(...args) { | ||
return renderHook(() => useWindowSize(...args)); | ||
} | ||
|
||
function triggerResize(dimension: 'width' | 'height', value: number) { | ||
if (dimension === 'width') { | ||
(window.innerWidth as number) = value; | ||
} else if (dimension === 'height') { | ||
(window.innerHeight as number) = value; | ||
} | ||
|
||
// simulate window resize | ||
function fireResize(type, value) { | ||
switch (type) { | ||
case 'width': | ||
(window.innerWidth as number) = value; // assert type of window.innerWidth as it is typed as readonly. | ||
break; | ||
case 'height': | ||
(window.innerHeight as number) = value; // assert type of window.innerHeight as it is typed as readonly. | ||
break; | ||
default: | ||
break; | ||
window.dispatchEvent(new Event('resize')); | ||
} | ||
|
||
window.dispatchEvent(new Event('resize')); | ||
} | ||
it('should return current window dimensions', () => { | ||
const hook = getHook(); | ||
|
||
describe('useWindowSize', () => { | ||
it('should be defined', () => { | ||
expect(useWindowSize).toBeDefined(); | ||
expect(typeof hook.result.current).toBe('object'); | ||
expect(typeof hook.result.current.height).toBe('number'); | ||
expect(typeof hook.result.current.width).toBe('number'); | ||
}); | ||
|
||
it('should use passed parameters as initial values in case of non-browser use', () => { | ||
const hook = getHook(1, 1); | ||
|
||
expect(hook.result.current.height).toBe(isClient ? window.innerHeight : 1); | ||
expect(hook.result.current.width).toBe(isClient ? window.innerWidth : 1); | ||
}); | ||
|
||
const hook = renderHook(() => useWindowSize()); | ||
it('should re-render after height change on closest RAF', () => { | ||
const hook = getHook(); | ||
|
||
it('should update width', () => { | ||
act(() => { | ||
fireResize('width', 320); | ||
triggerResize('height', 360); | ||
requestAnimationFrame.step(); | ||
}); | ||
|
||
expect(hook.result.current.width).toBe(320); | ||
expect(hook.result.current.height).toBe(360); | ||
|
||
act(() => { | ||
fireResize('width', 640); | ||
triggerResize('height', 2048); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(hook.result.current.width).toBe(640); | ||
|
||
expect(hook.result.current.height).toBe(2048); | ||
}); | ||
|
||
it('should update height', () => { | ||
it('should re-render after width change on closest RAF', () => { | ||
const hook = getHook(); | ||
|
||
act(() => { | ||
fireResize('height', 500); | ||
triggerResize('width', 360); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(hook.result.current.height).toBe(500); | ||
|
||
expect(hook.result.current.width).toBe(360); | ||
|
||
act(() => { | ||
fireResize('height', 1000); | ||
triggerResize('width', 2048); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(hook.result.current.height).toBe(1000); | ||
|
||
expect(hook.result.current.width).toBe(2048); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters