Skip to content

Commit

Permalink
Merge pull request #240 from DanilaEgorenko/#237
Browse files Browse the repository at this point in the history
[test]: add test for useDeviceOrientation
  • Loading branch information
debabin authored Oct 2, 2024
2 parents 4e38ada + 7bb75d7 commit 7077e1a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/hooks/useDeviceOrientation/useDeviceOrientation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { act, renderHook } from '@testing-library/react';

import { useDeviceOrientation } from './useDeviceOrientation';

it('Should use onDeviceOrientation', () => {
const { result } = renderHook(useDeviceOrientation);

expect(result.current.supported).toBe(true);
expect(typeof result.current.onDeviceOrientation).toBe('function');
});

it('Should use initial values', () => {
const { result } = renderHook(useDeviceOrientation);

expect(result.current.value.alpha).toBeNull();
expect(result.current.value.beta).toBeNull();
expect(result.current.value.gamma).toBeNull();
expect(result.current.value.absolute).toBeFalsy();
});

it('Should set new values', () => {
const { result } = renderHook(useDeviceOrientation);

const mockEvent = {
alpha: 30,
beta: 60,
gamma: 90,
absolute: true
};

act(() => {
const event = new Event('deviceorientation');
Object.assign(event, mockEvent);
window.dispatchEvent(event);
});

expect(result.current.value.alpha).toContain(mockEvent.alpha);
expect(result.current.value.beta).toContain(mockEvent.beta);
expect(result.current.value.gamma).toContain(mockEvent.gamma);
expect(result.current.value.absolute).toContain(mockEvent.absolute);
});

0 comments on commit 7077e1a

Please sign in to comment.