-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathuseStatus.test.tsx
62 lines (48 loc) · 1.8 KB
/
useStatus.test.tsx
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
import React from 'react';
import { act, renderHook } from '@testing-library/react';
import { vi, describe, expect, it, afterEach } from 'vitest';
import { useStatus } from '../src/hooks/useStatus';
import { PowerSyncContext } from '../src/hooks/PowerSyncContext';
const callback = vi.fn();
const mockPowerSync = {
currentStatus: { status: 'initial' },
registerListener: () => callback
};
vi.mock('./PowerSyncContext', () => ({
useContext: vi.fn(() => mockPowerSync)
}));
describe('useStatus', () => {
afterEach(() => {
vi.resetAllMocks();
});
it('should initialize with the current status', () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useStatus(), { wrapper });
expect(result.current).toEqual(mockPowerSync.currentStatus);
});
// TODO: Get this test to work
it.skip('should update the status when the listener is called', () => {
const mockPowerSyncInTest = {
currentStatus: { status: 'initial' },
registerListener: vi.fn(() => {})
};
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSyncInTest as any}>{children}</PowerSyncContext.Provider>
);
const { result } = renderHook(() => useStatus(), { wrapper });
act(() => {
expect(result.current).toEqual({ status: 'updated' });
});
});
it('should run the listener on unmount', () => {
const wrapper = ({ children }) => (
<PowerSyncContext.Provider value={mockPowerSync as any}>{children}</PowerSyncContext.Provider>
);
const { unmount } = renderHook(() => useStatus(), { wrapper });
const listenerUnsubscribe = callback;
unmount();
expect(listenerUnsubscribe).toHaveBeenCalled();
});
});