|
| 1 | +import React from 'react' |
| 2 | +import { screen, fireEvent, render } from '@testing-library/react' |
| 3 | +import { intersectionMockInstance, mockAllIsIntersecting } from '../test-utils' |
| 4 | +import { InView } from '../InView' |
| 5 | + |
| 6 | +it('Should render <InView /> intersecting', () => { |
| 7 | + const callback = jest.fn() |
| 8 | + render( |
| 9 | + <InView onChange={callback}> |
| 10 | + {({ inView, ref }) => <div ref={ref}>{inView.toString()}</div>} |
| 11 | + </InView>, |
| 12 | + ) |
| 13 | + |
| 14 | + mockAllIsIntersecting(false) |
| 15 | + expect(callback).toHaveBeenLastCalledWith( |
| 16 | + false, |
| 17 | + expect.objectContaining({ isIntersecting: false }), |
| 18 | + ) |
| 19 | + |
| 20 | + mockAllIsIntersecting(true) |
| 21 | + expect(callback).toHaveBeenLastCalledWith( |
| 22 | + true, |
| 23 | + expect.objectContaining({ isIntersecting: true }), |
| 24 | + ) |
| 25 | +}) |
| 26 | + |
| 27 | +it('should render plain children', () => { |
| 28 | + render(<InView>inner</InView>) |
| 29 | + screen.getByText('inner') |
| 30 | +}) |
| 31 | + |
| 32 | +it('should render with tag', () => { |
| 33 | + const { container } = render(<InView tag="span">inner</InView>) |
| 34 | + const tagName = container.firstChild.tagName.toLowerCase() |
| 35 | + expect(tagName).toBe('span') |
| 36 | +}) |
| 37 | + |
| 38 | +it('should render with className', () => { |
| 39 | + const { container } = render(<InView className="inner-class">inner</InView>) |
| 40 | + expect(container.firstChild).toHaveClass('inner-class') |
| 41 | +}) |
| 42 | + |
| 43 | +it('Should respect skip', () => { |
| 44 | + const cb = jest.fn() |
| 45 | + render(<InView skip onChange={cb}></InView>) |
| 46 | + mockAllIsIntersecting() |
| 47 | + |
| 48 | + expect(cb).not.toHaveBeenCalled() |
| 49 | +}) |
| 50 | +it('Should unobserve old node', () => { |
| 51 | + const { rerender, container } = render( |
| 52 | + <InView> |
| 53 | + {({ inView, ref }) => ( |
| 54 | + <div key="1" ref={ref}> |
| 55 | + Inview: {inView.toString()} |
| 56 | + </div> |
| 57 | + )} |
| 58 | + </InView>, |
| 59 | + ) |
| 60 | + rerender( |
| 61 | + <InView> |
| 62 | + {({ inView, ref }) => ( |
| 63 | + <div key="2" ref={ref}> |
| 64 | + Inview: {inView.toString()} |
| 65 | + </div> |
| 66 | + )} |
| 67 | + </InView>, |
| 68 | + ) |
| 69 | + mockAllIsIntersecting(true) |
| 70 | +}) |
| 71 | + |
| 72 | +it('Should ensure node exists before observing and unobserving', () => { |
| 73 | + const { unmount } = render(<InView>{() => null}</InView>) |
| 74 | + unmount() |
| 75 | +}) |
| 76 | + |
| 77 | +it('Should recreate observer when threshold change', () => { |
| 78 | + const { container, rerender } = render(<InView>Inner</InView>) |
| 79 | + mockAllIsIntersecting(true) |
| 80 | + const instance = intersectionMockInstance(container.firstChild) |
| 81 | + jest.spyOn(instance, 'unobserve') |
| 82 | + |
| 83 | + rerender(<InView threshold={0.5}>Inner</InView>) |
| 84 | + expect(instance.unobserve).toHaveBeenCalled() |
| 85 | +}) |
| 86 | + |
| 87 | +it('Should recreate observer when root change', () => { |
| 88 | + const { container, rerender } = render(<InView>Inner</InView>) |
| 89 | + mockAllIsIntersecting(true) |
| 90 | + const instance = intersectionMockInstance(container.firstChild) |
| 91 | + jest.spyOn(instance, 'unobserve') |
| 92 | + |
| 93 | + rerender(<InView root={{}}>Inner</InView>) |
| 94 | + expect(instance.unobserve).toHaveBeenCalled() |
| 95 | +}) |
| 96 | + |
| 97 | +it('Should recreate observer when rootMargin change', () => { |
| 98 | + const { container, rerender } = render(<InView>Inner</InView>) |
| 99 | + mockAllIsIntersecting(true) |
| 100 | + const instance = intersectionMockInstance(container.firstChild) |
| 101 | + jest.spyOn(instance, 'unobserve') |
| 102 | + |
| 103 | + rerender(<InView rootMargin="10px">Inner</InView>) |
| 104 | + expect(instance.unobserve).toHaveBeenCalled() |
| 105 | +}) |
| 106 | + |
| 107 | +it('Should unobserve when triggerOnce comes into view', () => { |
| 108 | + const { container, rerender } = render(<InView triggerOnce>Inner</InView>) |
| 109 | + mockAllIsIntersecting(false) |
| 110 | + const instance = intersectionMockInstance(container.firstChild) |
| 111 | + jest.spyOn(instance, 'unobserve') |
| 112 | + mockAllIsIntersecting(true) |
| 113 | + |
| 114 | + expect(instance.unobserve).toHaveBeenCalled() |
| 115 | +}) |
| 116 | + |
| 117 | +it('Should unobserve when unmounted', () => { |
| 118 | + const { container, unmount } = render(<InView triggerOnce>Inner</InView>) |
| 119 | + const instance = intersectionMockInstance(container.firstChild) |
| 120 | + jest.spyOn(instance, 'unobserve') |
| 121 | + |
| 122 | + unmount() |
| 123 | + |
| 124 | + expect(instance.unobserve).toHaveBeenCalled() |
| 125 | +}) |
| 126 | + |
| 127 | +it('plain children should not catch bubbling onChange event', () => { |
| 128 | + const onChange = jest.fn() |
| 129 | + const { getByLabelText } = render( |
| 130 | + <InView onChange={onChange}> |
| 131 | + <label> |
| 132 | + <input name="field" /> |
| 133 | + input |
| 134 | + </label> |
| 135 | + </InView>, |
| 136 | + ) |
| 137 | + const input = getByLabelText('input') |
| 138 | + fireEvent.change(input, { target: { value: 'changed value' } }) |
| 139 | + expect(onChange).not.toHaveBeenCalled() |
| 140 | +}) |
0 commit comments