1
1
import React , { useContext , useEffect , useState } from 'react' ;
2
- import { render , screen , RenderOptions } from '@testing-library/react' ;
3
- import * as UnleashClientModule from 'unleash-proxy-client' ;
2
+ import { render , screen } from '@testing-library/react' ;
3
+ import { type Mock } from 'vitest' ;
4
+ import { UnleashClient , type IVariant , EVENTS } from 'unleash-proxy-client' ;
4
5
import FlagProvider from './FlagProvider' ;
5
6
import FlagContext from './FlagContext' ;
6
-
7
7
import '@testing-library/jest-dom' ;
8
- import { EVENTS } from 'unleash-proxy-client' ;
9
- interface IFlagProvider {
10
- config : UnleashClientModule . IConfig ;
11
- }
12
- interface renderConsumerOptions {
13
- providerProps : IFlagProvider ;
14
- renderOptions : RenderOptions ;
15
- }
16
8
17
9
const getVariantMock = vi . fn ( ) . mockReturnValue ( 'A' ) ;
18
10
const updateContextMock = vi . fn ( ) ;
@@ -21,7 +13,6 @@ const stopClientMock = vi.fn();
21
13
const onMock = vi . fn ( ) . mockReturnValue ( 'subscribed' ) ;
22
14
const offMock = vi . fn ( ) ;
23
15
const isEnabledMock = vi . fn ( ) . mockReturnValue ( true ) ;
24
- const UnleashClientSpy = vi . spyOn ( UnleashClientModule , 'UnleashClient' ) ;
25
16
26
17
const givenConfig = {
27
18
appName : 'my-app' ,
@@ -31,12 +22,7 @@ const givenConfig = {
31
22
const givenFlagName = 'test' ;
32
23
const givenContext = { session : 'context' } ;
33
24
34
- beforeEach ( ( ) => {
35
- onMock . mockClear ( ) ;
36
- } ) ;
37
-
38
- // @ts -expect-error
39
- UnleashClientSpy . mockReturnValue ( {
25
+ const UnleashClientSpy = vi . fn ( ) . mockReturnValue ( {
40
26
getVariant : getVariantMock ,
41
27
updateContext : updateContextMock ,
42
28
start : startClientMock ,
@@ -46,18 +32,24 @@ UnleashClientSpy.mockReturnValue({
46
32
off : offMock ,
47
33
} ) ;
48
34
35
+ vi . mock ( 'unleash-proxy-client' , async ( importOriginal ) => {
36
+ const mod = await importOriginal ( ) ;
37
+
38
+ return {
39
+ ...mod as any ,
40
+ UnleashClient : vi . fn ( ) ,
41
+ } ;
42
+ } ) ;
43
+
49
44
const noop = ( ) => { } ;
50
45
51
46
const FlagConsumerAfterClientInit = ( ) => {
52
47
const { updateContext, isEnabled, getVariant, client, on } =
53
48
useContext ( FlagContext ) ;
54
49
const [ enabled , setIsEnabled ] = useState ( false ) ;
55
- const [ variant , setVariant ] = useState < UnleashClientModule . IVariant | null > (
56
- null
57
- ) ;
50
+ const [ variant , setVariant ] = useState < IVariant | null > ( null ) ;
58
51
const [ context , setContext ] = useState < any > ( 'nothing' ) ;
59
- const [ currentOn , setCurrentOn ] =
60
- useState < UnleashClientModule . UnleashClient | null > ( null ) ;
52
+ const [ currentOn , setCurrentOn ] = useState < UnleashClient | null > ( null ) ;
61
53
62
54
useEffect ( ( ) => {
63
55
if ( client ) {
@@ -82,12 +74,9 @@ const FlagConsumerBeforeClientInit = () => {
82
74
const { updateContext, isEnabled, getVariant, client, on } =
83
75
useContext ( FlagContext ) ;
84
76
const [ enabled , setIsEnabled ] = useState ( false ) ;
85
- const [ variant , setVariant ] = useState < UnleashClientModule . IVariant | null > (
86
- null
87
- ) ;
77
+ const [ variant , setVariant ] = useState < IVariant | null > ( null ) ;
88
78
const [ context , setContext ] = useState < any > ( 'nothing' ) ;
89
- const [ currentOn , setCurrentOn ] =
90
- useState < UnleashClientModule . UnleashClient | null > ( null ) ;
79
+ const [ currentOn , setCurrentOn ] = useState < UnleashClient | null > ( null ) ;
91
80
92
81
useEffect ( ( ) => {
93
82
if ( ! client ) {
@@ -101,36 +90,18 @@ const FlagConsumerBeforeClientInit = () => {
101
90
return < > </ > ;
102
91
} ;
103
92
104
- const renderConsumer = (
105
- ui : any ,
106
- { providerProps, renderOptions } : renderConsumerOptions
107
- ) => {
108
- return render (
109
- < FlagProvider config = { providerProps . config } > { ui } </ FlagProvider > ,
110
- renderOptions
111
- ) ;
112
- } ;
113
-
114
- const renderConsumerWithUnleashClient = (
115
- ui : any ,
116
- { providerProps, renderOptions } : renderConsumerOptions
117
- ) => {
118
- const client = new UnleashClientModule . UnleashClient ( providerProps . config ) ;
119
- return render (
120
- < FlagProvider unleashClient = { client } > { ui } </ FlagProvider > ,
121
- renderOptions
122
- ) ;
123
- } ;
93
+ beforeEach ( ( ) => {
94
+ onMock . mockClear ( ) ;
95
+ ( UnleashClient as Mock ) . mockImplementation ( UnleashClientSpy ) ;
96
+ } ) ;
124
97
125
98
test ( 'A consumer that subscribes AFTER client init shows values from provider and calls all the functions' , ( ) => {
126
- const providerProps = {
127
- config : givenConfig ,
128
- } ;
129
-
130
- renderConsumer ( < FlagConsumerAfterClientInit /> , {
131
- providerProps,
132
- renderOptions : { } ,
133
- } ) ;
99
+ render (
100
+ < FlagProvider config = { givenConfig } >
101
+ < FlagConsumerAfterClientInit />
102
+ </ FlagProvider > ,
103
+ { }
104
+ ) ;
134
105
135
106
expect ( getVariantMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
136
107
expect ( isEnabledMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
@@ -150,29 +121,25 @@ test('A consumer that subscribes AFTER client init shows values from provider an
150
121
} ) ;
151
122
152
123
test ( 'A consumer that subscribes BEFORE client init shows values from provider and calls all the functions' , ( ) => {
153
- const providerProps = {
154
- config : givenConfig ,
155
- } ;
156
-
157
- renderConsumer ( < FlagConsumerBeforeClientInit /> , {
158
- providerProps,
159
- renderOptions : { } ,
160
- } ) ;
124
+ render (
125
+ < FlagProvider config = { givenConfig } >
126
+ < FlagConsumerBeforeClientInit />
127
+ </ FlagProvider > ,
128
+ { }
129
+ ) ;
161
130
162
131
expect ( getVariantMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
163
132
expect ( isEnabledMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
164
133
expect ( updateContextMock ) . toHaveBeenCalledWith ( givenContext ) ;
165
134
} ) ;
166
135
167
136
test ( 'A consumer should be able to get a variant when the client is passed into the provider as props' , ( ) => {
168
- const providerProps = {
169
- config : givenConfig ,
170
- } ;
171
-
172
- renderConsumerWithUnleashClient ( < FlagConsumerAfterClientInit /> , {
173
- providerProps,
174
- renderOptions : { } ,
175
- } ) ;
137
+ render (
138
+ < FlagProvider unleashClient = { new UnleashClient ( givenConfig ) } >
139
+ < FlagConsumerAfterClientInit />
140
+ </ FlagProvider > ,
141
+ { }
142
+ ) ;
176
143
177
144
expect ( getVariantMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
178
145
expect ( isEnabledMock ) . toHaveBeenCalledWith ( givenFlagName ) ;
@@ -224,7 +191,6 @@ test('A memoized consumer should not rerender when the context provider values a
224
191
225
192
test ( 'should update when ready event is sent' , ( ) => {
226
193
const localMock = vi . fn ( ) ;
227
- // @ts -expect-error
228
194
UnleashClientSpy . mockReturnValue ( {
229
195
getVariant : getVariantMock ,
230
196
updateContext : updateContextMock ,
@@ -235,11 +201,7 @@ test('should update when ready event is sent', () => {
235
201
off : offMock ,
236
202
} ) ;
237
203
238
- const providerProps = {
239
- config : givenConfig ,
240
- } ;
241
-
242
- const client = new UnleashClientModule . UnleashClient ( providerProps . config ) ;
204
+ const client = new UnleashClient ( givenConfig ) ;
243
205
244
206
render (
245
207
< FlagProvider unleashClient = { client } >
@@ -258,7 +220,6 @@ test('should update when ready event is sent', () => {
258
220
259
221
test ( 'should register error when error event is sent' , ( ) => {
260
222
const localMock = vi . fn ( ) ;
261
- // @ts -expect-error
262
223
UnleashClientSpy . mockReturnValue ( {
263
224
getVariant : getVariantMock ,
264
225
updateContext : updateContextMock ,
@@ -269,11 +230,7 @@ test('should register error when error event is sent', () => {
269
230
off : offMock ,
270
231
} ) ;
271
232
272
- const providerProps = {
273
- config : givenConfig ,
274
- } ;
275
-
276
- const client = new UnleashClientModule . UnleashClient ( providerProps . config ) ;
233
+ const client = new UnleashClient ( givenConfig ) ;
277
234
278
235
render (
279
236
< FlagProvider unleashClient = { client } >
@@ -293,7 +250,6 @@ test('should register error when error event is sent', () => {
293
250
test ( 'should not start client if startClient is false' , ( ) => {
294
251
const localMock = vi . fn ( ) ;
295
252
const stopMock = vi . fn ( ) ;
296
- // @ts -expect-error
297
253
UnleashClientSpy . mockReturnValue ( {
298
254
getVariant : getVariantMock ,
299
255
updateContext : updateContextMock ,
@@ -304,11 +260,7 @@ test('should not start client if startClient is false', () => {
304
260
off : offMock ,
305
261
} ) ;
306
262
307
- const providerProps = {
308
- config : givenConfig ,
309
- } ;
310
-
311
- const client = new UnleashClientModule . UnleashClient ( providerProps . config ) ;
263
+ const client = new UnleashClient ( givenConfig ) ;
312
264
313
265
render (
314
266
< FlagProvider unleashClient = { client } startClient = { false } >
0 commit comments