Skip to content

Commit

Permalink
fix(useOrientation): update tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Joseph authored and kodai3 committed Mar 9, 2021
1 parent f45cb70 commit f9c743f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion tests/useOrientation.test.ts
Expand Up @@ -11,11 +11,15 @@ declare var requestAnimationFrame: {
describe('useOrientation', () => {
beforeAll(() => {
replaceRaf();
});

beforeEach(() => {
(window.screen.orientation as object) = {
type: 'landscape-primary',
angle: 0,
};
});
(window.orientation as number) = 0;
})

afterEach(() => {
requestAnimationFrame.reset();
Expand Down Expand Up @@ -44,6 +48,13 @@ describe('useOrientation', () => {
expect(typeof hook.result.current.angle).toBe('number');
});

it('should use initial values in case of no parameters', () => {
const hook = getHook();

expect(hook.result.current.type).toBe('landscape-primary');
expect(hook.result.current.angle).toBe(0);
});

it('should use passed parameters as initial values in case of non-browser use', () => {
const hook = getHook({
angle: 90,
Expand All @@ -65,4 +76,23 @@ describe('useOrientation', () => {
expect(hook.result.current.type).toBe('portrait-secondary');
expect(hook.result.current.angle).toBe(180);
});

it('should return window.orientation number if window.screen.orientation is missing', () => {
(window.screen.orientation as unknown) = undefined;

const hook = getHook();

expect(hook.result.current.type).toBe('');
expect(hook.result.current.angle).toBe(0);
});

it('should return 0 if window.orientation is not a number and if window.screen.orientation is missing', () => {
(window.screen.orientation as unknown) = undefined;
(window.orientation as unknown) = null;

const hook = getHook();

expect(hook.result.current.type).toBe('');
expect(hook.result.current.angle).toBe(0);
})
});

0 comments on commit f9c743f

Please sign in to comment.