|
| 1 | +import React from 'react' |
| 2 | +import { render, act } from '@testing-library/react' |
| 3 | +import '@testing-library/jest-dom/extend-expect' |
| 4 | + |
| 5 | +import mockIntersectionObserver from '../../../src/mocks/intersection-observer' |
| 6 | + |
| 7 | +import App, { Section } from './App' |
| 8 | + |
| 9 | +const intersectionObserver = mockIntersectionObserver() |
| 10 | + |
| 11 | +describe('Section is intersecting', () => { |
| 12 | + it('should render correctly when not inside the intersection zone', () => { |
| 13 | + const { getByText } = render(<Section number={1} />) |
| 14 | + |
| 15 | + expect(getByText('A section 1 - not intersecting')).toBeInTheDocument() |
| 16 | + }) |
| 17 | + |
| 18 | + it('should render correctly when entering the intersection zone', () => { |
| 19 | + const { getByText, container } = render(<Section number={1} />) |
| 20 | + |
| 21 | + act(() => { |
| 22 | + intersectionObserver.enterNode(container.firstChild) |
| 23 | + }) |
| 24 | + |
| 25 | + expect(getByText('A section 1 - intersecting')).toBeInTheDocument() |
| 26 | + }) |
| 27 | + |
| 28 | + it('should render correctly when leaving the intersection zone', () => { |
| 29 | + const { getByText, container } = render(<Section number={1} />) |
| 30 | + |
| 31 | + act(() => { |
| 32 | + intersectionObserver.enterNode(container.firstChild) |
| 33 | + }) |
| 34 | + |
| 35 | + expect(getByText('A section 1 - intersecting')).toBeInTheDocument() |
| 36 | + |
| 37 | + act(() => { |
| 38 | + intersectionObserver.leaveNode(container.firstChild) |
| 39 | + }) |
| 40 | + |
| 41 | + expect(getByText('A section 1 - not intersecting')).toBeInTheDocument() |
| 42 | + }) |
| 43 | + |
| 44 | + it('should enter all the nodes at once', () => { |
| 45 | + const { getAllByText } = render(<App />) |
| 46 | + |
| 47 | + act(() => { |
| 48 | + intersectionObserver.enterAll() |
| 49 | + }) |
| 50 | + |
| 51 | + expect(getAllByText(/A section/).map(node => node.textContent)).toEqual([ |
| 52 | + 'A section 0 - intersecting', |
| 53 | + 'A section 1 - intersecting', |
| 54 | + 'A section 2 - intersecting', |
| 55 | + 'A section 3 - intersecting', |
| 56 | + 'A section 4 - intersecting', |
| 57 | + 'A section 5 - intersecting', |
| 58 | + 'A section 6 - intersecting', |
| 59 | + 'A section 7 - intersecting', |
| 60 | + 'A section 8 - intersecting', |
| 61 | + 'A section 9 - intersecting' |
| 62 | + ]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should enter and leave all the nodes at once', () => { |
| 66 | + const { getAllByText } = render(<App />) |
| 67 | + |
| 68 | + act(() => { |
| 69 | + intersectionObserver.enterAll() |
| 70 | + }) |
| 71 | + |
| 72 | + expect(getAllByText(/A section/).map(node => node.textContent)).toEqual([ |
| 73 | + 'A section 0 - intersecting', |
| 74 | + 'A section 1 - intersecting', |
| 75 | + 'A section 2 - intersecting', |
| 76 | + 'A section 3 - intersecting', |
| 77 | + 'A section 4 - intersecting', |
| 78 | + 'A section 5 - intersecting', |
| 79 | + 'A section 6 - intersecting', |
| 80 | + 'A section 7 - intersecting', |
| 81 | + 'A section 8 - intersecting', |
| 82 | + 'A section 9 - intersecting' |
| 83 | + ]) |
| 84 | + |
| 85 | + act(() => { |
| 86 | + intersectionObserver.leaveAll() |
| 87 | + }) |
| 88 | + |
| 89 | + expect(getAllByText(/A section/).map(node => node.textContent)).toEqual([ |
| 90 | + 'A section 0 - not intersecting', |
| 91 | + 'A section 1 - not intersecting', |
| 92 | + 'A section 2 - not intersecting', |
| 93 | + 'A section 3 - not intersecting', |
| 94 | + 'A section 4 - not intersecting', |
| 95 | + 'A section 5 - not intersecting', |
| 96 | + 'A section 6 - not intersecting', |
| 97 | + 'A section 7 - not intersecting', |
| 98 | + 'A section 8 - not intersecting', |
| 99 | + 'A section 9 - not intersecting' |
| 100 | + ]) |
| 101 | + }) |
| 102 | +}) |
0 commit comments