import React, { PropsWithChildren } from 'react'; import { render } from '@testing-library/react'; import { ParallaxBanner } from '.'; import { ParallaxProvider } from '../ParallaxProvider'; import { ALL_PARALLAX_PROPS } from '../../testUtils/tests.constants'; import { ParallaxController, ScrollAxis } from 'parallax-controller'; import { MockProvider } from '../../testUtils/MockProvider'; describe('given a component', () => { describe('with all props', () => { it('then it will render banners correctly', () => { const { asFragment } = render(

Foo Bar

); expect(asFragment()).toMatchSnapshot(); }); }); describe.each(ALL_PARALLAX_PROPS)('when the prop %s is given', (props) => { it('then it renders without issue and calls create element with props', () => { const controller = ParallaxController.init({ scrollAxis: ScrollAxis.vertical, }); controller.createElement = vi.fn(controller.createElement); function Wrapper(props: PropsWithChildren<{}>) { return ( {props.children} ); } const { asFragment } = render( , { wrapper: Wrapper, } ); expect(asFragment()).toMatchSnapshot(); expect(controller.createElement).toHaveBeenCalledWith({ el: expect.any(HTMLElement), props: { ...props, shouldDisableScalingTranslations: true, targetElement: expect.any(HTMLElement), }, }); }); }); describe('when creating layers in the controller', () => { it('then it defaults shouldDisableScalingTranslations to true', () => { const controller = ParallaxController.init({ scrollAxis: ScrollAxis.vertical, }); controller.createElement = vi.fn(controller.createElement); function Wrapper(props: PropsWithChildren<{}>) { return ( {props.children} ); } render( }]} />, { wrapper: Wrapper, }); expect(controller.createElement).toHaveBeenCalledWith({ el: expect.any(HTMLElement), props: { shouldDisableScalingTranslations: true, targetElement: expect.any(HTMLElement), }, }); }); }); describe('when children are defined', () => { it('then it will render the children', () => { const { getByTestId } = render(
); expect(getByTestId('children')).toBeInTheDocument(); }); }); describe('when custom defined layer children are defined', () => { it('then it will render each layer child', () => { const { getByTestId } = render( foo
}, { children:
bar
}, ]} />
); expect(getByTestId('foo')).toBeInTheDocument(); expect(getByTestId('bar')).toBeInTheDocument(); }); }); describe('when the layer expanded option is false', () => { it('then it will render without expanded styles', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('0px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('0px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); describe('when the layer is expanded and', () => { describe('when the speed prop is set to a positive number', () => { it('then it will render with expanded styles based on speed', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('-20px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('-20px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); describe('when the speed prop is set to a negative number', () => { it('then it will render with expanded styles based on speed', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('-40px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('-40px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); describe('when the translateY prop is set [0px, 10px]', () => { it('then it will render with expanded styles based on the translate start end values', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('-10px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('0px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); describe('when the translateY prop is set [-40px, 30px]', () => { it('then it will render with expanded styles based on the translate start end values', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('-30px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('-40px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); describe('when the translateY prop is set [0px, 100px]', () => { it('then it will render with expanded styles based on the translate start end values', () => { const { getByTestId } = render( ); expect(getByTestId('layer-0').style.top).toBe('-100px'); expect(getByTestId('layer-0').style.right).toBe('0px'); expect(getByTestId('layer-0').style.left).toBe('0px'); expect(getByTestId('layer-0').style.bottom).toBe('0px'); expect(getByTestId('layer-0').style.position).toBe('absolute'); }); }); }); describe('with custom props', () => { it('then it will render children', () => { const { container, getByTestId } = render( ); expect(container.querySelector('.my-custom-class')).toBeInTheDocument(); expect(getByTestId('layer-0').style.background).toBe('red'); }); }); describe('when custom html props are given', () => { it('then it adds them to the returned div', () => { const { container, getByTestId } = render( ); expect(getByTestId('data-test-id')).toBeInTheDocument(); expect(container.querySelector('.my-class')).toBeInTheDocument(); expect(container.querySelector('#test-id')).toBeInTheDocument(); expect(getByTestId('data-test-id')).toHaveAttribute('aria-label', 'Cool'); expect(getByTestId('data-test-id')).toHaveAttribute('data-foo', 'bar'); expect(getByTestId('data-test-id').style.background).toBe('red'); }); }); describe('with custom props are defined in the layer', () => { it('then it adds them to the layer div', () => { const { container, getByTestId } = render( ); expect(getByTestId('data-test-id')).toBeInTheDocument(); expect(container.querySelector('.my-class')).toBeInTheDocument(); expect(container.querySelector('#test-id')).toBeInTheDocument(); expect(getByTestId('data-test-id')).toHaveAttribute('aria-label', 'Cool'); expect(getByTestId('data-test-id')).toHaveAttribute('data-foo', 'bar'); expect(getByTestId('data-test-id').style.background).toBe('red'); }); }); });