-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuseVariant.test.tsx
119 lines (98 loc) · 4.49 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
import { renderHook } from '@testing-library/react-hooks/native';
import React from 'react';
import useVariant from './useVariant';
const getVariantMock = jest.fn()
const useContextSpy = jest.spyOn(React, 'useContext');
const givenFlagName: string = 'Test';
const clientMock: any = {
on: jest.fn(),
}
const givenVariantA = { name: 'A', enabled: true }
const givenVariantB = { name: 'B', enabled: true }
const givenVariantA_disabled = { name: 'A', enabled: false }
beforeEach(() => {
getVariantMock.mockClear();
})
test('should return false when the flag is NOT enabled in context', () => {
getVariantMock.mockReturnValue(givenVariantA);
useContextSpy.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);
useContextSpy.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);
useContextSpy.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);
useContextSpy.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);
useContextSpy.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', () => {
clientMock.on.mockClear()
getVariantMock.mockReturnValueOnce(false);
useContextSpy.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);
});