forked from Stellar-split/split-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoiceListSentinel.test.tsx
More file actions
116 lines (97 loc) · 2.89 KB
/
Copy pathInvoiceListSentinel.test.tsx
File metadata and controls
116 lines (97 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { render, screen, waitFor } from '@testing-library/react';
import InvoiceListSentinel from '@/components/InvoiceListSentinel';
describe('InvoiceListSentinel', () => {
let observerMock: { observe: jest.Mock; disconnect: jest.Mock; unobserve: jest.Mock };
let IntersectionObserverMock: jest.Mock;
beforeEach(() => {
observerMock = {
observe: jest.fn(),
disconnect: jest.fn(),
unobserve: jest.fn(),
};
IntersectionObserverMock = jest.fn(() => observerMock);
(global as any).IntersectionObserver = IntersectionObserverMock;
});
afterEach(() => {
jest.clearAllMocks();
});
it('should create an IntersectionObserver on mount', () => {
const mockOnVisible = jest.fn();
render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={false}
allLoaded={false}
/>
);
expect(IntersectionObserverMock).toHaveBeenCalled();
expect(observerMock.observe).toHaveBeenCalled();
});
it('should disconnect the IntersectionObserver on unmount', () => {
const mockOnVisible = jest.fn();
const { unmount } = render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={false}
allLoaded={false}
/>
);
unmount();
expect(observerMock.disconnect).toHaveBeenCalled();
});
it('should call onVisible when intersection is detected', () => {
const mockOnVisible = jest.fn();
IntersectionObserverMock.mockImplementation((callback) => {
setTimeout(() => {
callback([{ isIntersecting: true }] as any);
}, 0);
return observerMock;
});
render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={false}
allLoaded={false}
/>
);
waitFor(() => {
expect(mockOnVisible).toHaveBeenCalled();
});
});
it('should display loading spinner when loading is true', () => {
const mockOnVisible = jest.fn();
render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={true}
allLoaded={false}
/>
);
expect(screen.getByText('Loading more invoices…')).toBeInTheDocument();
});
it('should display all loaded message when allLoaded is true', () => {
const mockOnVisible = jest.fn();
render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={false}
allLoaded={true}
/>
);
expect(screen.getByText('All invoices loaded')).toBeInTheDocument();
});
it('should pass custom rootMargin to IntersectionObserver', () => {
const mockOnVisible = jest.fn();
const customMargin = '500px';
render(
<InvoiceListSentinel
onVisible={mockOnVisible}
loading={false}
allLoaded={false}
rootMargin={customMargin}
/>
);
const callArgs = IntersectionObserverMock.mock.calls[0];
expect(callArgs[1]).toEqual({ rootMargin: customMargin });
});
});