|
| 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