Skip to content

Commit 3b9a9aa

Browse files
author
babin
committed
main 🧊 add use device orientation test, fix use async
1 parent 7077e1a commit 3b9a9aa

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

‎src/hooks/useAsync/useAsync.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export const useAsync = <Data>(
4545
.catch((error: Error) => {
4646
setError(error);
4747
setIsError(true);
48-
setIsLoading(false);
4948
})
5049
.finally(() => {
5150
setIsLoading(false);

‎src/hooks/useDeviceOrientation/useDeviceOrientation.test.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,41 @@ import { act, renderHook } from '@testing-library/react';
22

33
import { useDeviceOrientation } from './useDeviceOrientation';
44

5-
it('Should use onDeviceOrientation', () => {
6-
const { result } = renderHook(useDeviceOrientation);
7-
8-
expect(result.current.supported).toBe(true);
9-
expect(typeof result.current.onDeviceOrientation).toBe('function');
5+
beforeAll(() => {
6+
Object.defineProperty(window, 'DeviceOrientationEvent', {
7+
configurable: true,
8+
value: {},
9+
writable: true
10+
});
1011
});
1112

12-
it('Should use initial values', () => {
13+
it('Should use on device orientation', () => {
1314
const { result } = renderHook(useDeviceOrientation);
1415

16+
expect(result.current.supported).toBe(true);
1517
expect(result.current.value.alpha).toBeNull();
1618
expect(result.current.value.beta).toBeNull();
1719
expect(result.current.value.gamma).toBeNull();
1820
expect(result.current.value.absolute).toBeFalsy();
1921
});
2022

21-
it('Should set new values', () => {
23+
it('Should set new values when device orientation change', () => {
2224
const { result } = renderHook(useDeviceOrientation);
2325

24-
const mockEvent = {
25-
alpha: 30,
26-
beta: 60,
27-
gamma: 90,
28-
absolute: true
29-
};
30-
3126
act(() => {
3227
const event = new Event('deviceorientation');
33-
Object.assign(event, mockEvent);
28+
Object.assign(event, {
29+
alpha: 30,
30+
beta: 60,
31+
gamma: 90,
32+
absolute: true
33+
});
34+
3435
window.dispatchEvent(event);
3536
});
3637

37-
expect(result.current.value.alpha).toContain(mockEvent.alpha);
38-
expect(result.current.value.beta).toContain(mockEvent.beta);
39-
expect(result.current.value.gamma).toContain(mockEvent.gamma);
40-
expect(result.current.value.absolute).toContain(mockEvent.absolute);
38+
expect(result.current.value.alpha).toBe(30);
39+
expect(result.current.value.beta).toBe(60);
40+
expect(result.current.value.gamma).toBe(90);
41+
expect(result.current.value.absolute).toBe(true);
4142
});

‎src/hooks/useDeviceOrientation/useDeviceOrientation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const useDeviceOrientation = (): UseDeviceOrientationReturn => {
4141
});
4242

4343
useEffect(() => {
44+
console.log('@@@@@@', window, 'DeviceOrientationEvent' in window);
4445
if (!supported) return;
4546

4647
const onDeviceOrientation = (event: DeviceOrientationEvent) =>

‎src/utils/helpers/debounce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export function debounce<Params extends unknown[]>(
22
callback: (...args: Params) => void,
33
delay: number
4-
): ((...args: Params) => void) {
4+
): (...args: Params) => void {
55
let timer: ReturnType<typeof setTimeout>;
66

77
return function (this: any, ...args: Params) {
88
clearTimeout(timer);
99
timer = setTimeout(() => callback.apply(this, args), delay);
1010
};
11-
};
11+
}

0 commit comments

Comments
 (0)