-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuseFlags.test.ts
64 lines (58 loc) · 1.44 KB
/
useFlags.test.ts
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
import { useContext } from 'react';
import { renderHook } from '@testing-library/react-hooks/native';
import useFlags from './useFlags';
import type { IToggle } from 'unleash-proxy-client';
import { act } from 'react-dom/test-utils';
const toggles = [
{
name: 'string',
enabled: true,
variant: {
name: 'string',
enabled: false,
},
impressionData: false,
},
{
name: 'string',
enabled: true,
variant: {
name: 'string',
enabled: false,
},
impressionData: false,
},
];
vi.mock('react', async () => ({
...((await vi.importActual('react')) as any),
useContext: vi.fn(),
}));
test('should return flags', () => {
vi.mocked(useContext).mockReturnValue({
client: {
getAllToggles: () => toggles,
on: vi.fn(),
},
});
const { result } = renderHook(() => useFlags());
expect(result.current).toEqual(toggles);
});
test('should update flags on update event', async () => {
const updatedToggles: IToggle[] = [];
const client = {
getAllToggles: vi.fn(),
on: vi.fn(),
off: vi.fn(),
};
client.getAllToggles.mockReturnValue(toggles);
vi.mocked(useContext).mockReturnValue({
client,
});
const { result } = renderHook(() => useFlags());
expect(result.current).toEqual(toggles);
client.getAllToggles.mockReturnValue(updatedToggles);
await act(async () => {
client.on.mock.calls[0][1]();
});
expect(result.current).toEqual(updatedToggles);
});