|
| 1 | +import { render, waitFor, screen } from '@testing-library/react'; |
| 2 | +import { renderHook } from '@testing-library/react-hooks'; |
| 3 | +import { framesOnce } from '../../example/src/frames'; |
| 4 | +import usePresentation from '..'; |
| 5 | + |
| 6 | +describe('Tests usePresentation', () => { |
| 7 | + afterEach(() => { |
| 8 | + jest.clearAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should return usePresentation array of items', () => { |
| 12 | + const { result } = renderHook(() => |
| 13 | + usePresentation({ framesOptions: framesOnce, startTrigger: true }) |
| 14 | + ); |
| 15 | + |
| 16 | + const [Presentation, currentFrame, framesLength] = result.current; |
| 17 | + |
| 18 | + expect(Presentation).toBeTruthy(); |
| 19 | + expect(currentFrame).toEqual(1); |
| 20 | + expect(framesLength).toEqual(framesOnce.length); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return usePresentation array of items for startTrigger false', () => { |
| 24 | + const { result } = renderHook(() => |
| 25 | + usePresentation({ framesOptions: framesOnce, startTrigger: false }) |
| 26 | + ); |
| 27 | + |
| 28 | + const [Presentation, currentFrame] = result.current; |
| 29 | + |
| 30 | + expect(Presentation).toBeTruthy(); |
| 31 | + expect(currentFrame).toEqual(0); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should render Presentation component without callback', () => { |
| 35 | + function RenderPresentation() { |
| 36 | + const [Presentation] = usePresentation({ |
| 37 | + framesOptions: [{ component: <div>test</div>, time: 1 }], |
| 38 | + startTrigger: true, |
| 39 | + }); |
| 40 | + |
| 41 | + return <Presentation />; |
| 42 | + } |
| 43 | + |
| 44 | + render(<RenderPresentation />); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should render Presentation component with callback and loop', async () => { |
| 48 | + const consoleInfoSpy = jest.spyOn(console, 'info'); |
| 49 | + const myCallback = () => console.info('presentation finished!'); |
| 50 | + |
| 51 | + function RenderPresentation() { |
| 52 | + const [Presentation] = usePresentation({ |
| 53 | + framesOptions: [{ component: <div>test</div>, time: 1 }], |
| 54 | + startTrigger: true, |
| 55 | + isLoop: true, |
| 56 | + callback: myCallback, |
| 57 | + }); |
| 58 | + |
| 59 | + return <Presentation />; |
| 60 | + } |
| 61 | + |
| 62 | + render(<RenderPresentation />); |
| 63 | + |
| 64 | + await waitFor( |
| 65 | + () => { |
| 66 | + expect(consoleInfoSpy).toHaveBeenCalledWith('presentation finished!'); |
| 67 | + }, |
| 68 | + { timeout: 100 } |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should render Presentation component 0 framesOptions length', () => { |
| 73 | + function RenderPresentation() { |
| 74 | + const [Presentation] = usePresentation({ |
| 75 | + framesOptions: [], |
| 76 | + startTrigger: true, |
| 77 | + }); |
| 78 | + |
| 79 | + return <Presentation />; |
| 80 | + } |
| 81 | + |
| 82 | + const { container } = render(<RenderPresentation />); |
| 83 | + |
| 84 | + expect(container.querySelectorAll('.animation-frame').length).toBe(0); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should render Presentation component with children', () => { |
| 88 | + function RenderPresentationWithChildren() { |
| 89 | + const [PresentationWithChildren] = usePresentation({ |
| 90 | + framesOptions: [{ component: <div>Animation Frame</div>, time: 50 }], |
| 91 | + startTrigger: true, |
| 92 | + }); |
| 93 | + |
| 94 | + return ( |
| 95 | + <PresentationWithChildren> |
| 96 | + <p>I am a child</p> |
| 97 | + </PresentationWithChildren> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const { container } = render(<RenderPresentationWithChildren />); |
| 102 | + |
| 103 | + expect( |
| 104 | + container.querySelectorAll('.animation-frame.with-children').length |
| 105 | + ).toBe(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should render Presentation component with its own className', () => { |
| 109 | + function RenderPresentationOwnClassName() { |
| 110 | + const [PresentationOwnClassName] = usePresentation({ |
| 111 | + framesOptions: [{ component: <div>Animation Frame</div>, time: 50 }], |
| 112 | + startTrigger: true, |
| 113 | + }); |
| 114 | + |
| 115 | + return <PresentationOwnClassName className="my-very-own-class" />; |
| 116 | + } |
| 117 | + |
| 118 | + const { container } = render(<RenderPresentationOwnClassName />); |
| 119 | + |
| 120 | + expect( |
| 121 | + container.querySelectorAll('.animation-frame.my-very-own-class').length |
| 122 | + ).toBe(1); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should render Presentation with 3 components with callback', async () => { |
| 126 | + const consoleInfoSpy = jest.spyOn(console, 'info'); |
| 127 | + const myCallback = () => console.info('presentation finished!'); |
| 128 | + |
| 129 | + function RenderPresentation() { |
| 130 | + const [Presentation] = usePresentation({ |
| 131 | + framesOptions: [ |
| 132 | + { component: <div>test 1</div>, time: 10 }, |
| 133 | + { component: <div>test 2</div>, time: 10 }, |
| 134 | + { component: <div>test 3</div>, time: 10 }, |
| 135 | + ], |
| 136 | + startTrigger: true, |
| 137 | + callback: myCallback, |
| 138 | + }); |
| 139 | + |
| 140 | + return <Presentation />; |
| 141 | + } |
| 142 | + |
| 143 | + render(<RenderPresentation />); |
| 144 | + |
| 145 | + await waitFor( |
| 146 | + () => { |
| 147 | + expect(consoleInfoSpy).toHaveBeenCalledWith('presentation finished!'); |
| 148 | + expect(screen.getByText('test 3')).toBeInTheDocument(); |
| 149 | + }, |
| 150 | + { timeout: 100 } |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should render Presentation with 1000 ms delay', async () => { |
| 155 | + function RenderPresentation() { |
| 156 | + const [Presentation] = usePresentation({ |
| 157 | + framesOptions: [{ component: <div>test 1</div>, time: 10 }], |
| 158 | + startTrigger: true, |
| 159 | + startDelay: 1000, |
| 160 | + }); |
| 161 | + |
| 162 | + return <Presentation />; |
| 163 | + } |
| 164 | + |
| 165 | + render(<RenderPresentation />); |
| 166 | + |
| 167 | + await waitFor( |
| 168 | + () => { |
| 169 | + expect(screen.queryByText('test 1')).toBeNull(); |
| 170 | + }, |
| 171 | + { timeout: 900 } |
| 172 | + ); |
| 173 | + |
| 174 | + await waitFor( |
| 175 | + () => { |
| 176 | + expect(screen.getByText('test 1')).toBeInTheDocument(); |
| 177 | + }, |
| 178 | + { timeout: 1100 } |
| 179 | + ); |
| 180 | + }); |
| 181 | +}); |
0 commit comments