-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathFlagProvider.test.tsx
115 lines (97 loc) · 4.13 KB
/
FlagProvider.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
/**
* @jest-environment jsdom
*/
import React, { useContext, useEffect, useState } from 'react';
import { render, screen , RenderOptions} from '@testing-library/react';
import * as UnleashClientModule from 'unleash-proxy-client';
import FlagProvider from './FlagProvider';
import FlagContext from './FlagContext';
import '@testing-library/jest-dom'
interface IFlagProvider {
config: UnleashClientModule.IConfig;
}
interface renderConsumerOptions {
providerProps: IFlagProvider,
renderOptions: RenderOptions
}
const getVariantMock = jest.fn().mockReturnValue('A');
const updateContextMock = jest.fn();
const startClientMock = jest.fn();
const onMock = jest.fn().mockReturnValue('subscribed');
const isEnabledMock = jest.fn().mockReturnValue(true)
const UnleashClientSpy:jest.SpyInstance = jest.spyOn(UnleashClientModule, 'UnleashClient');
const givenConfig = {
appName: 'my-app',
clientKey: 'my-secret',
url: 'https://my-unleash-proxy'
}
const givenFlagName = 'test';
const givenContext = { session: 'context' };
UnleashClientSpy.mockReturnValue({ getVariant: getVariantMock, updateContext: updateContextMock , start:startClientMock , isEnabled: isEnabledMock, on: onMock });
const FlagConsumerAfterClientInit = () => {
const { updateContext, isEnabled, getVariant, client, on } = useContext(FlagContext);
const [enabled, setIsEnabled] = useState('nothing');
const [variant, setVariant] = useState('nothing');
const [context, setContext] = useState('nothing');
const [currentOn, setCurrentOn] = useState('nothing');
useEffect(() => {
if (client) {
setIsEnabled(isEnabled(givenFlagName));
setVariant(getVariant(givenFlagName))
setContext(updateContext(givenContext));
setCurrentOn(on('someEvent', 'someArgument'));
}
}, [client])
return (
<>
<div>{`consuming value isEnabled ${enabled}`}</div>
<div>{`consuming value updateContext ${context}`}</div>
<div>{`consuming value getVariant ${variant}`}</div>
<div>{`consuming value on ${currentOn}`}</div>
</>
)
}
const FlagConsumerBeforeClientInit = () => {
const { updateContext, isEnabled, getVariant, client, on} = useContext(FlagContext);
const [enabled, setIsEnabled] = useState('nothing');
const [variant, setVariant] = useState('nothing');
const [context, setContext] = useState('nothing');
const [currentOn, setCurrentOn] = useState('nothing');
useEffect(() => {
if (!client) {
setIsEnabled(isEnabled(givenFlagName));
setVariant(getVariant(givenFlagName))
setContext(updateContext(givenContext));
setCurrentOn(on('someEvent', 'someArgument'));
}
}, [])
return <></>
}
const renderConsumer = (ui: any, { providerProps, renderOptions} : renderConsumerOptions) => {
return render(
<FlagProvider config={providerProps.config}>{ui}</FlagProvider>,
renderOptions,
)
}
test('A consumer that subscribes AFTER client init shows values from provider and calls all the functions', () => {
const providerProps = {
config: givenConfig
}
renderConsumer(<FlagConsumerAfterClientInit />, { providerProps, renderOptions: {} })
expect(getVariantMock).toHaveBeenCalledWith(givenFlagName);
expect(isEnabledMock).toHaveBeenCalledWith(givenFlagName);
expect(updateContextMock).toHaveBeenCalledWith(givenContext);
expect(screen.getByText(/consuming value isEnabled/)).toHaveTextContent('consuming value isEnabled true')
expect(screen.getByText(/consuming value updateContext/)).toHaveTextContent('consuming value updateContext undefined')
expect(screen.getByText(/consuming value getVariant/)).toHaveTextContent('consuming value getVariant A')
expect(screen.getByText(/consuming value on/)).toHaveTextContent('consuming value on subscribed')
});
test('A consumer that subscribes BEFORE client init shows values from provider and calls all the functions', () => {
const providerProps = {
config: givenConfig
}
renderConsumer(<FlagConsumerBeforeClientInit />, {providerProps, renderOptions:{}})
expect(getVariantMock).toHaveBeenCalledWith(givenFlagName);
expect(isEnabledMock).toHaveBeenCalledWith(givenFlagName);
expect(updateContextMock).toHaveBeenCalledWith(givenContext);
})