|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 | import React from 'react';
|
4 |
| -import { render } from '@testing-library/react'; |
| 4 | +import { act, cleanup, render } from '@testing-library/react'; |
5 | 5 |
|
6 |
| -import { useIntersectionObserver } from '..'; |
7 |
| - |
8 |
| -declare global { |
9 |
| - interface Window { |
10 |
| - IntersectionObserver: any; |
11 |
| - } |
12 |
| -} |
| 6 | +import { useIntersectionObserver } from '../../../../../lib/components/internal/hooks/use-intersection-observer'; |
13 | 7 |
|
14 | 8 | function TestComponent({ initialState }: { initialState?: boolean }) {
|
15 | 9 | const { ref, isIntersecting } = useIntersectionObserver({ initialState });
|
16 | 10 | return <div ref={ref} data-testid="test" data-value={isIntersecting} />;
|
17 | 11 | }
|
18 | 12 |
|
19 |
| -const mockObserve = jest.fn(); |
20 |
| -const mockIntersectionObserver = jest.fn(() => ({ |
21 |
| - observe: mockObserve, |
22 |
| - disconnect: jest.fn(), |
23 |
| -})); |
| 13 | +type MockIntersectionObserverCallback = ( |
| 14 | + // only fields actually used in our implementation |
| 15 | + entries: Array<Pick<IntersectionObserverEntry, 'isIntersecting' | 'time'>> |
| 16 | +) => void; |
| 17 | +let mockObserverInstance: (IntersectionObserver & { callback: MockIntersectionObserverCallback }) | undefined; |
| 18 | + |
| 19 | +class MockIntersectionObserver { |
| 20 | + constructor(callback: MockIntersectionObserverCallback) { |
| 21 | + if (mockObserverInstance) { |
| 22 | + throw new Error('only one observer instance expected per test'); |
| 23 | + } |
| 24 | + const instance = { |
| 25 | + observe: jest.fn() as IntersectionObserver['observe'], |
| 26 | + disconnect: jest.fn() as IntersectionObserver['disconnect'], |
| 27 | + } as IntersectionObserver; |
| 28 | + mockObserverInstance = { |
| 29 | + ...instance, |
| 30 | + callback, |
| 31 | + }; |
| 32 | + return instance; |
| 33 | + } |
| 34 | +} |
24 | 35 |
|
25 | 36 | describe('useIntersectionObserver', () => {
|
26 | 37 | beforeEach(() => {
|
27 |
| - window.IntersectionObserver = mockIntersectionObserver; |
| 38 | + window.IntersectionObserver = MockIntersectionObserver as unknown as typeof IntersectionObserver; |
28 | 39 | jest.clearAllMocks();
|
29 | 40 | });
|
30 | 41 |
|
31 |
| - it('should create an observer with the defined target element', async () => { |
32 |
| - const { findByTestId } = render(<TestComponent />); |
33 |
| - const target = await findByTestId('test'); |
| 42 | + afterEach(() => { |
| 43 | + cleanup(); |
| 44 | + expect(mockObserverInstance!.disconnect).toHaveBeenCalled(); |
| 45 | + mockObserverInstance = undefined; |
| 46 | + }); |
| 47 | + |
| 48 | + it('should create an observer with the defined target element', () => { |
| 49 | + const { getByTestId } = render(<TestComponent />); |
| 50 | + const target = getByTestId('test'); |
| 51 | + |
| 52 | + expect(mockObserverInstance!.observe).toHaveBeenCalledWith(target); |
| 53 | + }); |
| 54 | + |
| 55 | + it('defaults to not intersecting', () => { |
| 56 | + const { getByTestId } = render(<TestComponent />); |
| 57 | + const target = getByTestId('test'); |
| 58 | + |
| 59 | + expect(target.dataset.value).toBe('false'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('allows overriding the default value', () => { |
| 63 | + const { getByTestId } = render(<TestComponent initialState={true} />); |
| 64 | + const target = getByTestId('test'); |
34 | 65 |
|
35 |
| - expect(mockIntersectionObserver).toHaveBeenCalled(); |
36 |
| - expect(mockObserve).toHaveBeenCalledWith(target); |
| 66 | + expect(target.dataset.value).toBe('true'); |
37 | 67 | });
|
38 | 68 |
|
39 |
| - it('defaults to not intersecting', async () => { |
40 |
| - const { findByTestId } = render(<TestComponent />); |
41 |
| - const target = await findByTestId('test'); |
| 69 | + it('updates value with a callback', () => { |
| 70 | + const { getByTestId } = render(<TestComponent />); |
| 71 | + |
| 72 | + const target = getByTestId('test'); |
| 73 | + expect(target.dataset.value).toBe('false'); |
| 74 | + |
| 75 | + act(() => mockObserverInstance!.callback([{ time: 0, isIntersecting: true }])); |
| 76 | + expect(target.dataset.value).toBe('true'); |
42 | 77 |
|
| 78 | + act(() => mockObserverInstance!.callback([{ time: 0, isIntersecting: false }])); |
43 | 79 | expect(target.dataset.value).toBe('false');
|
44 | 80 | });
|
45 | 81 |
|
46 |
| - it('allows overriding the default value', async () => { |
47 |
| - const { findByTestId } = render(<TestComponent initialState={true} />); |
48 |
| - const target = await findByTestId('test'); |
| 82 | + it('should resolve the final state if multiple observations occurred', () => { |
| 83 | + const { getByTestId } = render(<TestComponent />); |
| 84 | + |
| 85 | + act(() => |
| 86 | + mockObserverInstance!.callback([ |
| 87 | + { time: 0, isIntersecting: true }, |
| 88 | + { time: 1, isIntersecting: false }, |
| 89 | + { time: 2, isIntersecting: true }, |
| 90 | + ]) |
| 91 | + ); |
| 92 | + |
| 93 | + const target = getByTestId('test'); |
| 94 | + expect(target.dataset.value).toBe('true'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should sort observations by time', () => { |
| 98 | + const { getByTestId } = render(<TestComponent />); |
| 99 | + |
| 100 | + act(() => |
| 101 | + mockObserverInstance!.callback([ |
| 102 | + { time: 1, isIntersecting: false }, |
| 103 | + { time: 2, isIntersecting: true }, |
| 104 | + { time: 0, isIntersecting: false }, |
| 105 | + ]) |
| 106 | + ); |
49 | 107 |
|
| 108 | + const target = getByTestId('test'); |
50 | 109 | expect(target.dataset.value).toBe('true');
|
51 | 110 | });
|
52 | 111 | });
|
0 commit comments