1
1
import React from 'react' ;
2
- import { mount , ReactWrapper } from 'enzyme ' ;
2
+ import { render , RenderResult } from '@testing-library/react ' ;
3
3
4
4
/** Mocks */
5
5
import { mockSdk , Event } from './testUtils/mockSplitSdk' ;
@@ -32,7 +32,7 @@ describe('SplitTreatments', () => {
32
32
33
33
it ( 'passes as treatments prop the value returned by the function "getControlTreatmentsWithConfig" if the SDK is not ready.' , ( done ) => {
34
34
const splitNames = [ 'split1' , 'split2' ] ;
35
- mount (
35
+ render (
36
36
< SplitFactory config = { sdkBrowser } > {
37
37
( { factory } ) => {
38
38
return (
@@ -57,7 +57,7 @@ describe('SplitTreatments', () => {
57
57
const outerFactory = SplitSdk ( sdkBrowser ) ;
58
58
( outerFactory as any ) . client ( ) . __emitter__ . emit ( Event . SDK_READY ) ;
59
59
setTimeout ( ( ) => {
60
- mount (
60
+ render (
61
61
< SplitFactory factory = { outerFactory } > {
62
62
( { factory, isReady } ) => {
63
63
expect ( getStatus ( outerFactory . client ( ) ) . isReady ) . toBe ( isReady ) ;
@@ -83,7 +83,7 @@ describe('SplitTreatments', () => {
83
83
it ( 'logs error and passes control treatments ("getControlTreatmentsWithConfig") if rendered outside an SplitProvider component.' , ( ) => {
84
84
const splitNames = [ 'split1' , 'split2' ] ;
85
85
let passedTreatments ;
86
- mount (
86
+ render (
87
87
< SplitTreatments names = { splitNames } >
88
88
{ ( { treatments } : ISplitTreatmentsChildProps ) => {
89
89
passedTreatments = treatments ;
@@ -102,7 +102,7 @@ describe('SplitTreatments', () => {
102
102
it ( 'Input validation: invalid "names" and "attributes" props in SplitTreatments.' , ( done ) => {
103
103
const splitNames = [ 'split1' , 'split2' ] ;
104
104
105
- mount (
105
+ render (
106
106
< SplitFactory config = { sdkBrowser } > {
107
107
( ) => {
108
108
return (
@@ -175,49 +175,49 @@ describe('SplitTreatments optimization', () => {
175
175
const attributes = { att1 : 'att1' } ;
176
176
const splitKey = sdkBrowser . core . key ;
177
177
178
- let wrapper : ReactWrapper ;
178
+ let wrapper : RenderResult ;
179
179
180
180
beforeEach ( ( ) => {
181
181
renderTimes = 0 ;
182
182
( outerFactory . client ( ) . getTreatmentsWithConfig as jest . Mock ) . mockClear ( ) ;
183
- wrapper = mount ( < Component names = { names } attributes = { attributes } splitKey = { splitKey } /> ) ;
183
+ wrapper = render ( < Component names = { names } attributes = { attributes } splitKey = { splitKey } /> ) ;
184
184
} )
185
185
186
186
afterEach ( ( ) => {
187
187
wrapper . unmount ( ) ; // unmount to remove event listener from factory
188
188
} )
189
189
190
190
it ( 'rerenders but does not re-evaluate splits if client, lastUpdate, names and attributes are the same object.' , ( ) => {
191
- wrapper . setProps ( { names, attributes, splitKey } ) ;
191
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { splitKey } /> ) ;
192
192
193
193
expect ( renderTimes ) . toBe ( 2 ) ;
194
194
expect ( outerFactory . client ( ) . getTreatmentsWithConfig ) . toBeCalledTimes ( 1 ) ;
195
195
} ) ;
196
196
197
197
it ( 'rerenders but does not re-evaluate splits if client, lastUpdate, names and attributes are equals (shallow comparison).' , ( ) => {
198
- wrapper . setProps ( { names : [ ...names ] , attributes : { ...attributes } , splitKey } ) ;
198
+ wrapper . rerender ( < Component names = { [ ...names ] } attributes = { { ...attributes } } splitKey = { splitKey } /> ) ;
199
199
200
200
expect ( renderTimes ) . toBe ( 2 ) ;
201
201
expect ( outerFactory . client ( ) . getTreatmentsWithConfig ) . toBeCalledTimes ( 1 ) ;
202
202
} ) ;
203
203
204
204
it ( 'rerenders and re-evaluates splits if names are not equals (shallow array comparison).' , ( ) => {
205
- wrapper . setProps ( { names : [ ...names , 'split3' ] , attributes : { ...attributes } , splitKey } ) ;
205
+ wrapper . rerender ( < Component names = { [ ...names , 'split3' ] } attributes = { { ...attributes } } splitKey = { splitKey } /> ) ;
206
206
207
207
expect ( renderTimes ) . toBe ( 2 ) ;
208
208
expect ( outerFactory . client ( ) . getTreatmentsWithConfig ) . toBeCalledTimes ( 2 ) ;
209
209
} ) ;
210
210
211
211
it ( 'rerenders and re-evaluates splits if attributes are not equals (shallow object comparison).' , ( ) => {
212
212
const attributesRef = { ...attributes , att2 : 'att2' } ;
213
- wrapper . setProps ( { names : [ ...names ] , attributes : attributesRef , splitKey } ) ;
213
+ wrapper . rerender ( < Component names = { [ ...names ] } attributes = { attributesRef } splitKey = { splitKey } /> ) ;
214
214
215
215
expect ( renderTimes ) . toBe ( 2 ) ;
216
216
expect ( outerFactory . client ( ) . getTreatmentsWithConfig ) . toBeCalledTimes ( 2 ) ;
217
217
218
218
// If passing same reference but mutated (bad practice), the component re-renders but doesn't re-evaluate splits
219
219
attributesRef . att2 = 'att2_val2' ;
220
- wrapper . setProps ( { names : [ ...names ] , attributes : attributesRef , splitKey } ) ;
220
+ wrapper . rerender ( < Component names = { [ ...names ] } attributes = { attributesRef } splitKey = { splitKey } /> ) ;
221
221
expect ( renderTimes ) . toBe ( 3 ) ;
222
222
expect ( outerFactory . client ( ) . getTreatmentsWithConfig ) . toBeCalledTimes ( 2 ) ;
223
223
} ) ;
@@ -230,7 +230,7 @@ describe('SplitTreatments optimization', () => {
230
230
231
231
// State update after destroy doesn't re-evaluate because the sdk is not operational
232
232
( outerFactory as any ) . client ( ) . destroy ( ) ;
233
- wrapper . setProps ( { names, attributes, splitKey } ) ;
233
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { splitKey } /> ) ;
234
234
235
235
setTimeout ( ( ) => {
236
236
// Updates were batched as a single render, due to automatic batching https://reactjs.org/blog/2022/03/29/react-v18.html#new-feature-automatic-batching
@@ -245,7 +245,7 @@ describe('SplitTreatments optimization', () => {
245
245
} ) ;
246
246
247
247
it ( 'rerenders and re-evaluates splits if client changes.' , ( done ) => {
248
- wrapper . setProps ( { names, attributes, splitKey : 'otherKey' } ) ;
248
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { 'otherKey' } /> ) ;
249
249
( outerFactory as any ) . client ( 'otherKey' ) . __emitter__ . emit ( Event . SDK_READY ) ;
250
250
251
251
setTimeout ( ( ) => {
@@ -266,7 +266,7 @@ describe('SplitTreatments optimization', () => {
266
266
let renderTimesComp2 = 0 ;
267
267
268
268
// test context updates on SplitFactory
269
- mount (
269
+ render (
270
270
< SplitFactory factory = { outerFactory } updateOnSdkReady = { false } updateOnSdkTimedout = { true } updateOnSdkUpdate = { true } >
271
271
< SplitTreatments names = { names } attributes = { attributes } >
272
272
{ ( ) => {
@@ -277,7 +277,7 @@ describe('SplitTreatments optimization', () => {
277
277
</ SplitFactory > ) ;
278
278
279
279
// test context updates on SplitClient
280
- mount (
280
+ render (
281
281
< SplitFactory factory = { outerFactory } >
282
282
< SplitClient splitKey = 'user2' updateOnSdkReadyFromCache = { false } updateOnSdkTimedout = { true } updateOnSdkUpdate = { true } >
283
283
< SplitTreatments names = { names } attributes = { attributes } >
@@ -333,18 +333,17 @@ describe('SplitTreatments optimization', () => {
333
333
}
334
334
335
335
client . on ( client . Event . SDK_READY , ( ) => {
336
- wrapper = mount ( < Component names = { names } attributes = { attributes } splitKey = { 'emma2' } /> ) ;
336
+ wrapper = render ( < Component names = { names } attributes = { attributes } splitKey = { 'emma2' } /> ) ;
337
337
expect ( clientSpy . getTreatmentsWithConfig ) . toBeCalledTimes ( 1 ) ;
338
-
339
- wrapper . setProps ( { names, attributes, clientAttributes : { att2 : 'att1_val1' } } ) ;
338
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { 'emma2' } clientAttributes = { { att2 : 'att1_val1' } } /> ) ;
340
339
expect ( renderTimes ) . toBe ( 3 ) ;
341
340
expect ( clientSpy . getTreatmentsWithConfig ) . toBeCalledTimes ( 2 ) ;
342
341
343
- wrapper . setProps ( { names, attributes, clientAttributes : { att2 : 'att1_val2' } } ) ;
342
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { 'emma2' } clientAttributes = { { att2 : 'att1_val2' } } /> ) ;
344
343
expect ( renderTimes ) . toBe ( 4 ) ;
345
344
expect ( clientSpy . getTreatmentsWithConfig ) . toBeCalledTimes ( 3 ) ;
346
345
347
- wrapper . setProps ( { names, attributes, clientAttributes : { att2 : 'att1_val2' } } ) ;
346
+ wrapper . rerender ( < Component names = { names } attributes = { attributes } splitKey = { 'emma2' } clientAttributes = { { att2 : 'att1_val2' } } /> ) ;
348
347
expect ( renderTimes ) . toBe ( 5 ) ;
349
348
expect ( clientSpy . getTreatmentsWithConfig ) . toBeCalledTimes ( 3 ) ; // not called again. clientAttributes object is shallow equal
350
349
0 commit comments