|
| 1 | +import React, { PropsWithChildren, useRef } from 'react'; |
| 2 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 3 | +import { ParallaxController, ScrollAxis, Element } from 'parallax-controller'; |
| 4 | +import { MockProvider } from '../testUtils/MockProvider'; |
| 5 | +import { useParallax } from './useParallax'; |
| 6 | + |
| 7 | +jest.mock('react', () => { |
| 8 | + return { |
| 9 | + ...jest.requireActual<typeof React>('react'), |
| 10 | + useRef: jest.fn(() => ({ |
| 11 | + current: document.createElement('div'), |
| 12 | + })), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +describe('given useParallax hook', () => { |
| 17 | + beforeAll(() => { |
| 18 | + // NOTE: stop console warning of expected error |
| 19 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + // @ts-expect-error |
| 25 | + useRef.mockImplementation( |
| 26 | + jest.fn(() => ({ |
| 27 | + current: document.createElement('div'), |
| 28 | + })) |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll(() => { |
| 33 | + jest.resetAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('when not wrapped by the ParallaxProvider', () => { |
| 37 | + test('then it should throw an error', () => { |
| 38 | + try { |
| 39 | + const { result } = renderHook(() => |
| 40 | + useParallax({ translateX: [0, 100] }) |
| 41 | + ); |
| 42 | + |
| 43 | + act(() => { |
| 44 | + result.current.controller?.update(); |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result.error).toEqual( |
| 48 | + Error( |
| 49 | + 'Could not find `react-scroll-parallax` context value. Please ensure the component is wrapped in a <ParallaxProvider>' |
| 50 | + ) |
| 51 | + ); |
| 52 | + } catch (e) {} |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('when wrapped by the ParallaxProvider', () => { |
| 57 | + describe('when ref is not assigned', () => { |
| 58 | + test('then it should throw an error', () => { |
| 59 | + // NOTE: must override the useRef mock that returns an element |
| 60 | + // @ts-expect-error |
| 61 | + useRef.mockImplementation( |
| 62 | + jest.fn(() => ({ |
| 63 | + current: null, |
| 64 | + })) |
| 65 | + ); |
| 66 | + |
| 67 | + try { |
| 68 | + const controller = ParallaxController.init({ |
| 69 | + scrollAxis: ScrollAxis.vertical, |
| 70 | + }); |
| 71 | + controller.createElement = jest.fn(controller.createElement); |
| 72 | + const Wrapper = (props: PropsWithChildren<{}>) => ( |
| 73 | + <MockProvider controllerMock={controller}> |
| 74 | + {props.children} |
| 75 | + </MockProvider> |
| 76 | + ); |
| 77 | + |
| 78 | + const { result } = renderHook( |
| 79 | + () => useParallax({ translateX: [0, 100] }), |
| 80 | + { wrapper: Wrapper } |
| 81 | + ); |
| 82 | + |
| 83 | + expect(result.error).toEqual( |
| 84 | + Error( |
| 85 | + 'You must assign the ref returned by the useParallax() hook to an HTML Element.' |
| 86 | + ) |
| 87 | + ); |
| 88 | + } catch (e) {} |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('when wrapped by the ParallaxProvider', () => { |
| 94 | + test('then it should return an instance of the controller', () => { |
| 95 | + const controller = ParallaxController.init({ |
| 96 | + scrollAxis: ScrollAxis.vertical, |
| 97 | + }); |
| 98 | + controller.createElement = jest.fn(controller.createElement); |
| 99 | + const Wrapper = (props: PropsWithChildren<{}>) => ( |
| 100 | + <MockProvider controllerMock={controller}> |
| 101 | + {props.children} |
| 102 | + </MockProvider> |
| 103 | + ); |
| 104 | + |
| 105 | + const { result } = renderHook( |
| 106 | + () => |
| 107 | + useParallax<HTMLDivElement>({ |
| 108 | + translateX: [0, 100], |
| 109 | + }), |
| 110 | + { wrapper: Wrapper } |
| 111 | + ); |
| 112 | + |
| 113 | + expect(result.current.controller).toBeInstanceOf(ParallaxController); |
| 114 | + }); |
| 115 | + |
| 116 | + test('then it should return the created element', () => { |
| 117 | + const controller = ParallaxController.init({ |
| 118 | + scrollAxis: ScrollAxis.vertical, |
| 119 | + }); |
| 120 | + controller.createElement = jest.fn(controller.createElement); |
| 121 | + const Wrapper = (props: PropsWithChildren<{}>) => ( |
| 122 | + <MockProvider controllerMock={controller}> |
| 123 | + {props.children} |
| 124 | + </MockProvider> |
| 125 | + ); |
| 126 | + |
| 127 | + const { result } = renderHook( |
| 128 | + () => |
| 129 | + useParallax<HTMLDivElement>({ |
| 130 | + translateX: [0, 100], |
| 131 | + }), |
| 132 | + { wrapper: Wrapper } |
| 133 | + ); |
| 134 | + |
| 135 | + expect(result.current.element).toBeInstanceOf(Element); |
| 136 | + expect(result.current.element?.props.translateX).toEqual([0, 100]); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments