Skip to content

Commit 5f1c45c

Browse files
committed
test: useController
1 parent 8dde49f commit 5f1c45c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/hooks/useController.test.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { PropsWithChildren } from 'react';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import { ParallaxController } from 'parallax-controller';
4+
import { MockProvider } from '../testUtils/MockProvider';
5+
import { useController } from './useController';
6+
7+
const controller = ParallaxController.init({ scrollAxis: 'vertical' });
8+
9+
const Wrapper = (props: PropsWithChildren<{}>) => (
10+
<MockProvider controllerMock={controller}>{props.children}</MockProvider>
11+
);
12+
13+
describe('given useController hook', () => {
14+
const { window } = global;
15+
afterEach(() => {
16+
global.window = window;
17+
});
18+
describe.skip('when the window is undefined', () => {
19+
test('then it should return null', () => {
20+
try {
21+
const { result } = renderHook(() => {
22+
// @ts-expect-error
23+
delete global.window;
24+
return useController();
25+
});
26+
expect(result.current).toBe(null);
27+
} catch (e) {}
28+
});
29+
});
30+
describe('when not wrapped by the ParallaxProvider', () => {
31+
test('then it should throw an error', () => {
32+
try {
33+
const { result } = renderHook(() => useController());
34+
expect(result.error).toEqual(
35+
Error(
36+
'Could not find `react-scroll-parallax` context value. Please ensure the component is wrapped in a <ParallaxProvider>'
37+
)
38+
);
39+
} catch (e) {}
40+
});
41+
});
42+
describe('when wrapped by the ParallaxProvider', () => {
43+
test('then it should return the controller from context', () => {
44+
try {
45+
const { result } = renderHook(() => useController(), {
46+
wrapper: Wrapper,
47+
});
48+
expect(result.current).toEqual(controller);
49+
} catch (e) {}
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)