-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuseVariant.test.tsx
160 lines (136 loc) · 5.25 KB
/
useVariant.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { vi } from 'vitest';
import { renderHook } from '@testing-library/react-hooks/native';
import { useContext } from 'react';
import useVariant from './useVariant';
vi.mock('react', async () => {
const react = (await vi.importActual('react')) as any;
return {
...react,
useContext: vi.fn(react.useContext),
};
});
const getVariantMock = vi.fn();
const givenFlagName: string = 'Test';
const clientMock: any = {
on: vi.fn(),
off: vi.fn(),
};
const givenVariantA = { name: 'A', enabled: true };
const givenVariantB = { name: 'B', enabled: true };
const givenVariantA_disabled = { name: 'A', enabled: false };
beforeEach(() => {
getVariantMock.mockClear();
clientMock.on.mockClear();
clientMock.off.mockClear();
});
test('should return false when the flag is NOT enabled in context', () => {
getVariantMock.mockReturnValue(givenVariantA);
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(clientMock.on).toHaveBeenCalledWith('update', expect.any(Function));
expect(clientMock.on).toHaveBeenCalledWith('ready', expect.any(Function));
expect(result.current).toBe(givenVariantA);
expect(getVariantMock).toHaveBeenCalledTimes(1);
});
test('should return variant when the client is ready and re-call getVariant', () => {
getVariantMock.mockReturnValue(givenVariantA);
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
clientMock.on.mockImplementation((eventName: string, cb: Function) => {
if (eventName === 'ready') {
cb();
}
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(clientMock.on).toHaveBeenCalledWith('update', expect.any(Function));
expect(clientMock.on).toHaveBeenCalledWith('ready', expect.any(Function));
expect(result.current).toBe(givenVariantA);
expect(getVariantMock).toHaveBeenCalledTimes(2);
});
test('should return `B` when the variant is first `A` and is updated with `B`', () => {
getVariantMock.mockReturnValueOnce(givenVariantA);
getVariantMock.mockReturnValueOnce(givenVariantB);
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
clientMock.on.mockImplementation((eventName: string, cb: Function) => {
if (eventName === 'update') {
cb();
}
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(getVariantMock).toHaveBeenCalledTimes(3);
expect(result.current).toBe(givenVariantB);
expect(clientMock.on).toHaveBeenCalledWith('update', expect.any(Function));
expect(clientMock.on).toHaveBeenCalledWith('ready', expect.any(Function));
});
test('should return `A` when the variant is first `A` and is updated with `A` disabled', () => {
getVariantMock.mockReturnValueOnce(givenVariantA);
getVariantMock.mockReturnValueOnce(givenVariantA_disabled);
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
clientMock.on.mockImplementation((eventName: string, cb: Function) => {
if (eventName === 'update') {
cb();
}
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(getVariantMock).toHaveBeenCalledTimes(3);
expect(result.current).toBe(givenVariantA_disabled);
expect(clientMock.on).toHaveBeenCalledWith('update', expect.any(Function));
expect(clientMock.on).toHaveBeenCalledWith('ready', expect.any(Function));
});
test('should return `A` and update the local state just once when the variant is the same', () => {
getVariantMock.mockReturnValueOnce(givenVariantA);
getVariantMock.mockReturnValueOnce(givenVariantA);
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
clientMock.on.mockImplementation((eventName: string, cb: Function) => {
if (eventName === 'update') {
cb();
}
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(getVariantMock).toHaveBeenCalledTimes(2);
expect(result.current).toBe(givenVariantA);
expect(clientMock.on).toHaveBeenCalledWith('update', expect.any(Function));
expect(clientMock.on).toHaveBeenCalledWith('ready', expect.any(Function));
});
test('should NOT subscribe to ready or update if client does NOT exist', () => {
getVariantMock.mockReturnValueOnce(false);
vi.mocked(useContext).mockReturnValue({
client: undefined,
getVariant: getVariantMock,
});
clientMock.on.mockImplementation((eventName: string, cb: Function) => {
if (eventName === 'update') {
cb();
}
});
const { result } = renderHook(() => useVariant(givenFlagName));
expect(result.current).toStrictEqual({});
expect(clientMock.on).not.toHaveBeenCalled();
expect(clientMock.on).not.toHaveBeenCalled();
expect(getVariantMock).toHaveBeenCalledTimes(1);
});
test('should remove event listeners when unmounted', () => {
vi.mocked(useContext).mockReturnValue({
client: clientMock,
getVariant: getVariantMock,
});
const { unmount } = renderHook(() => useVariant(givenFlagName));
unmount();
expect(clientMock.off).toHaveBeenCalledTimes(2);
expect(clientMock.off).nthCalledWith(1, ...clientMock.on.mock.calls[0]);
expect(clientMock.off).nthCalledWith(2, ...clientMock.on.mock.calls[1]);
});