@wordpress/data 7.2.1-next.4d3b314fd5.0 → 7.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/package.json +8 -8
- package/src/components/use-dispatch/test/use-dispatch.js +84 -127
- package/src/components/use-select/test/index.js +93 -101
- package/src/components/use-select/test/suspense.js +6 -6
- package/src/components/with-dispatch/test/index.js +116 -178
- package/src/components/with-select/test/index.js +224 -309
- package/src/redux-store/test/index.js +3 -3
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
4
|
+
import { act, render, screen } from '@testing-library/react';
|
|
5
|
+
import userEvent from '@testing-library/user-event';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* WordPress dependencies
|
|
@@ -17,13 +18,11 @@ import withDispatch from '../../with-dispatch';
|
|
|
17
18
|
import { createRegistry } from '../../../registry';
|
|
18
19
|
import { RegistryProvider } from '../../registry-provider';
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
let registry;
|
|
22
|
-
beforeEach( () => {
|
|
23
|
-
registry = createRegistry();
|
|
24
|
-
} );
|
|
21
|
+
jest.useRealTimers();
|
|
25
22
|
|
|
23
|
+
describe( 'withSelect', () => {
|
|
26
24
|
it( 'passes the relevant data to the component', () => {
|
|
25
|
+
const registry = createRegistry();
|
|
27
26
|
registry.registerStore( 'reactReducer', {
|
|
28
27
|
reducer: () => ( { reactKey: 'reactState' } ),
|
|
29
28
|
selectors: {
|
|
@@ -38,42 +37,38 @@ describe( 'withSelect', () => {
|
|
|
38
37
|
// including both `withSelect` and `select` in the same scope, which
|
|
39
38
|
// shouldn't occur for a typical component, and if it did might wrongly
|
|
40
39
|
// encourage the developer to use `select` within the component itself.
|
|
41
|
-
const mapSelectToProps = jest
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const OriginalComponent = jest
|
|
50
|
-
.fn()
|
|
51
|
-
.mockImplementation( ( props ) => <div>{ props.data }</div> );
|
|
40
|
+
const mapSelectToProps = jest.fn( ( _select, ownProps ) => ( {
|
|
41
|
+
data: _select( 'reactReducer' ).reactSelector( ownProps.keyName ),
|
|
42
|
+
} ) );
|
|
43
|
+
|
|
44
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
45
|
+
<div role="status">{ props.data }</div>
|
|
46
|
+
) );
|
|
52
47
|
|
|
53
48
|
const DataBoundComponent =
|
|
54
49
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
<
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
} );
|
|
63
|
-
const testInstance = testRenderer.root;
|
|
50
|
+
|
|
51
|
+
render(
|
|
52
|
+
<RegistryProvider value={ registry }>
|
|
53
|
+
<DataBoundComponent keyName="reactKey" />
|
|
54
|
+
</RegistryProvider>
|
|
55
|
+
);
|
|
56
|
+
|
|
64
57
|
// Expected two times:
|
|
65
58
|
// - Once on initial render.
|
|
66
59
|
// - Once on effect before subscription set.
|
|
67
60
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
68
61
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
69
62
|
|
|
70
|
-
// Wrapper is the enhanced component.
|
|
71
|
-
expect(
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
// Wrapper is the enhanced component.
|
|
64
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent(
|
|
65
|
+
'reactState'
|
|
66
|
+
);
|
|
74
67
|
} );
|
|
75
68
|
|
|
76
|
-
it( 'should rerun selection on state changes', () => {
|
|
69
|
+
it( 'should rerun selection on state changes', async () => {
|
|
70
|
+
const user = userEvent.setup();
|
|
71
|
+
const registry = createRegistry();
|
|
77
72
|
registry.registerStore( 'counter', {
|
|
78
73
|
reducer: ( state = 0, action ) => {
|
|
79
74
|
if ( action.type === 'increment' ) {
|
|
@@ -90,38 +85,28 @@ describe( 'withSelect', () => {
|
|
|
90
85
|
},
|
|
91
86
|
} );
|
|
92
87
|
|
|
93
|
-
const mapSelectToProps = jest
|
|
94
|
-
.
|
|
95
|
-
|
|
96
|
-
count: _select( 'counter' ).getCount(),
|
|
97
|
-
} ) );
|
|
88
|
+
const mapSelectToProps = jest.fn( ( _select ) => ( {
|
|
89
|
+
count: _select( 'counter' ).getCount(),
|
|
90
|
+
} ) );
|
|
98
91
|
|
|
99
|
-
const mapDispatchToProps = jest
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
increment: _dispatch( 'counter' ).increment,
|
|
103
|
-
} ) );
|
|
92
|
+
const mapDispatchToProps = jest.fn( ( _dispatch ) => ( {
|
|
93
|
+
increment: _dispatch( 'counter' ).increment,
|
|
94
|
+
} ) );
|
|
104
95
|
|
|
105
|
-
const OriginalComponent = jest
|
|
106
|
-
.
|
|
107
|
-
|
|
108
|
-
<button onClick={ props.increment }>{ props.count }</button>
|
|
109
|
-
) );
|
|
96
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
97
|
+
<button onClick={ props.increment }>{ props.count }</button>
|
|
98
|
+
) );
|
|
110
99
|
|
|
111
100
|
const DataBoundComponent = compose( [
|
|
112
101
|
withSelect( mapSelectToProps ),
|
|
113
102
|
withDispatch( mapDispatchToProps ),
|
|
114
103
|
] )( OriginalComponent );
|
|
115
104
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
</RegistryProvider>
|
|
122
|
-
);
|
|
123
|
-
} );
|
|
124
|
-
const testInstance = testRenderer.root;
|
|
105
|
+
render(
|
|
106
|
+
<RegistryProvider value={ registry }>
|
|
107
|
+
<DataBoundComponent />
|
|
108
|
+
</RegistryProvider>
|
|
109
|
+
);
|
|
125
110
|
|
|
126
111
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
127
112
|
// 2 times:
|
|
@@ -131,11 +116,11 @@ describe( 'withSelect', () => {
|
|
|
131
116
|
expect( mapDispatchToProps ).toHaveBeenCalledTimes( 1 );
|
|
132
117
|
|
|
133
118
|
// Simulate a click on the button.
|
|
134
|
-
|
|
135
|
-
testInstance.findByType( 'button' ).props.onClick();
|
|
136
|
-
} );
|
|
119
|
+
const button = screen.getByRole( 'button' );
|
|
137
120
|
|
|
138
|
-
|
|
121
|
+
await user.click( button );
|
|
122
|
+
|
|
123
|
+
expect( button ).toHaveTextContent( '1' );
|
|
139
124
|
// 2 times =
|
|
140
125
|
// 1. Initial mount
|
|
141
126
|
// 2. When click handler is called.
|
|
@@ -184,56 +169,51 @@ describe( 'withSelect', () => {
|
|
|
184
169
|
}
|
|
185
170
|
|
|
186
171
|
render() {
|
|
187
|
-
return <div>{ this.props.count }</div>;
|
|
172
|
+
return <div role="status">{ this.props.count }</div>;
|
|
188
173
|
}
|
|
189
174
|
}
|
|
190
175
|
|
|
191
176
|
const renderSpy = jest.spyOn( OriginalComponent.prototype, 'render' );
|
|
192
177
|
|
|
193
|
-
const mapSelectToProps = jest
|
|
194
|
-
.
|
|
195
|
-
|
|
196
|
-
count: _select( 'counter' ).getCount(),
|
|
197
|
-
} ) );
|
|
178
|
+
const mapSelectToProps = jest.fn( ( _select ) => ( {
|
|
179
|
+
count: _select( 'counter' ).getCount(),
|
|
180
|
+
} ) );
|
|
198
181
|
|
|
199
|
-
const mapDispatchToProps = jest
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
increment: _dispatch( 'counter' ).increment,
|
|
203
|
-
} ) );
|
|
182
|
+
const mapDispatchToProps = jest.fn( ( _dispatch ) => ( {
|
|
183
|
+
increment: _dispatch( 'counter' ).increment,
|
|
184
|
+
} ) );
|
|
204
185
|
|
|
205
186
|
const DataBoundComponent = compose( [
|
|
206
187
|
withSelect( mapSelectToProps ),
|
|
207
188
|
withDispatch( mapDispatchToProps ),
|
|
208
189
|
] )( OriginalComponent );
|
|
209
190
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
TestRenderer.create(
|
|
191
|
+
it( 'should rerun if had dispatched action during mount', () => {
|
|
192
|
+
const { unmount } = render(
|
|
213
193
|
<RegistryProvider value={ testRegistry }>
|
|
214
194
|
<DataBoundComponent />
|
|
215
195
|
</RegistryProvider>
|
|
216
196
|
);
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
} );
|
|
220
|
-
testInstance = testRenderer.root;
|
|
221
|
-
it( 'should rerun if had dispatched action during mount', () => {
|
|
222
|
-
expect( testInstance.findByType( 'div' ).props.children ).toBe( 2 );
|
|
197
|
+
|
|
198
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( '2' );
|
|
223
199
|
// Expected 3 times because:
|
|
224
200
|
// - 1 on initial render
|
|
225
201
|
// - 1 on effect before subscription set.
|
|
226
202
|
// - 1 for the rerender because of the mapOutput change detected.
|
|
227
203
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
228
204
|
expect( renderSpy ).toHaveBeenCalledTimes( 2 );
|
|
205
|
+
|
|
206
|
+
unmount();
|
|
229
207
|
} );
|
|
208
|
+
|
|
230
209
|
it( 'should rerun on unmount and mount', () => {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
210
|
+
render(
|
|
211
|
+
<RegistryProvider value={ testRegistry }>
|
|
212
|
+
<DataBoundComponent />
|
|
213
|
+
</RegistryProvider>
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( '4' );
|
|
237
217
|
// Expected an additional 3 times because of the unmount and remount:
|
|
238
218
|
// - 1 on initial render
|
|
239
219
|
// - 1 on effect before subscription set.
|
|
@@ -244,6 +224,7 @@ describe( 'withSelect', () => {
|
|
|
244
224
|
} );
|
|
245
225
|
|
|
246
226
|
it( 'should rerun selection on props changes', () => {
|
|
227
|
+
const registry = createRegistry();
|
|
247
228
|
registry.registerStore( 'counter', {
|
|
248
229
|
reducer: ( state = 0, action ) => {
|
|
249
230
|
if ( action.type === 'increment' ) {
|
|
@@ -257,28 +238,22 @@ describe( 'withSelect', () => {
|
|
|
257
238
|
},
|
|
258
239
|
} );
|
|
259
240
|
|
|
260
|
-
const mapSelectToProps = jest
|
|
261
|
-
.
|
|
262
|
-
|
|
263
|
-
count: _select( 'counter' ).getCount( ownProps.offset ),
|
|
264
|
-
} ) );
|
|
241
|
+
const mapSelectToProps = jest.fn( ( _select, ownProps ) => ( {
|
|
242
|
+
count: _select( 'counter' ).getCount( ownProps.offset ),
|
|
243
|
+
} ) );
|
|
265
244
|
|
|
266
|
-
const OriginalComponent = jest
|
|
267
|
-
.
|
|
268
|
-
|
|
245
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
246
|
+
<div role="status">{ props.count }</div>
|
|
247
|
+
) );
|
|
269
248
|
|
|
270
249
|
const DataBoundComponent =
|
|
271
250
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
272
251
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
</RegistryProvider>
|
|
279
|
-
);
|
|
280
|
-
} );
|
|
281
|
-
const testInstance = testRenderer.root;
|
|
252
|
+
const { rerender } = render(
|
|
253
|
+
<RegistryProvider value={ registry }>
|
|
254
|
+
<DataBoundComponent offset={ 0 } />
|
|
255
|
+
</RegistryProvider>
|
|
256
|
+
);
|
|
282
257
|
|
|
283
258
|
// 2 times:
|
|
284
259
|
// - 1 on initial render
|
|
@@ -286,20 +261,19 @@ describe( 'withSelect', () => {
|
|
|
286
261
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
287
262
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
288
263
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
<
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
);
|
|
295
|
-
} );
|
|
264
|
+
rerender(
|
|
265
|
+
<RegistryProvider value={ registry }>
|
|
266
|
+
<DataBoundComponent offset={ 10 } />
|
|
267
|
+
</RegistryProvider>
|
|
268
|
+
);
|
|
296
269
|
|
|
297
|
-
expect(
|
|
270
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( '10' );
|
|
298
271
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
299
272
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
|
|
300
273
|
} );
|
|
301
274
|
|
|
302
275
|
it( 'should not run selection if props have not changed', () => {
|
|
276
|
+
const registry = createRegistry();
|
|
303
277
|
registry.registerStore( 'unchanging', {
|
|
304
278
|
reducer: ( state = {} ) => state,
|
|
305
279
|
selectors: {
|
|
@@ -308,8 +282,7 @@ describe( 'withSelect', () => {
|
|
|
308
282
|
} );
|
|
309
283
|
|
|
310
284
|
const mapSelectToProps = jest.fn();
|
|
311
|
-
|
|
312
|
-
const OriginalComponent = jest.fn().mockImplementation( () => <div /> );
|
|
285
|
+
const OriginalComponent = jest.fn( () => <div /> );
|
|
313
286
|
|
|
314
287
|
const DataBoundComponent = compose( [
|
|
315
288
|
withSelect( mapSelectToProps ),
|
|
@@ -319,14 +292,11 @@ describe( 'withSelect', () => {
|
|
|
319
292
|
<DataBoundComponent propName={ props.propName } />
|
|
320
293
|
);
|
|
321
294
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
</RegistryProvider>
|
|
328
|
-
);
|
|
329
|
-
} );
|
|
295
|
+
const { rerender } = render(
|
|
296
|
+
<RegistryProvider value={ registry }>
|
|
297
|
+
<Parent propName="foo" />
|
|
298
|
+
</RegistryProvider>
|
|
299
|
+
);
|
|
330
300
|
|
|
331
301
|
// 2 times:
|
|
332
302
|
// - 1 on initial render
|
|
@@ -334,19 +304,18 @@ describe( 'withSelect', () => {
|
|
|
334
304
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
335
305
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
336
306
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
<
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
);
|
|
343
|
-
} );
|
|
307
|
+
rerender(
|
|
308
|
+
<RegistryProvider value={ registry }>
|
|
309
|
+
<Parent propName="foo" />
|
|
310
|
+
</RegistryProvider>
|
|
311
|
+
);
|
|
344
312
|
|
|
345
313
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
346
314
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
347
315
|
} );
|
|
348
316
|
|
|
349
|
-
it( 'should not rerender if state has changed but merge props the same', () => {
|
|
317
|
+
it( 'should not rerender if state has changed but merge props the same', async () => {
|
|
318
|
+
const registry = createRegistry();
|
|
350
319
|
registry.registerStore( 'demo', {
|
|
351
320
|
reducer: () => ( {} ),
|
|
352
321
|
selectors: {
|
|
@@ -357,24 +326,20 @@ describe( 'withSelect', () => {
|
|
|
357
326
|
},
|
|
358
327
|
} );
|
|
359
328
|
|
|
360
|
-
const mapSelectToProps = jest
|
|
361
|
-
.
|
|
362
|
-
|
|
363
|
-
value: _select( 'demo' ).getUnchangingValue(),
|
|
364
|
-
} ) );
|
|
329
|
+
const mapSelectToProps = jest.fn( ( _select ) => ( {
|
|
330
|
+
value: _select( 'demo' ).getUnchangingValue(),
|
|
331
|
+
} ) );
|
|
365
332
|
|
|
366
|
-
const OriginalComponent = jest.fn(
|
|
333
|
+
const OriginalComponent = jest.fn( () => <div /> );
|
|
367
334
|
|
|
368
335
|
const DataBoundComponent =
|
|
369
336
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
370
337
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
<
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
);
|
|
377
|
-
} );
|
|
338
|
+
render(
|
|
339
|
+
<RegistryProvider value={ registry }>
|
|
340
|
+
<DataBoundComponent />
|
|
341
|
+
</RegistryProvider>
|
|
342
|
+
);
|
|
378
343
|
|
|
379
344
|
// 2 times:
|
|
380
345
|
// - 1 on initial render
|
|
@@ -382,13 +347,14 @@ describe( 'withSelect', () => {
|
|
|
382
347
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
383
348
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
384
349
|
|
|
385
|
-
registry.dispatch( 'demo' ).update();
|
|
350
|
+
await act( async () => registry.dispatch( 'demo' ).update() );
|
|
386
351
|
|
|
387
352
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
388
353
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
389
354
|
} );
|
|
390
355
|
|
|
391
356
|
it( 'should render if props have changed but not state', () => {
|
|
357
|
+
const registry = createRegistry();
|
|
392
358
|
registry.registerStore( 'unchanging', {
|
|
393
359
|
reducer: ( state = {} ) => state,
|
|
394
360
|
selectors: {
|
|
@@ -397,21 +363,17 @@ describe( 'withSelect', () => {
|
|
|
397
363
|
} );
|
|
398
364
|
|
|
399
365
|
const mapSelectToProps = jest.fn();
|
|
400
|
-
|
|
401
|
-
const OriginalComponent = jest.fn().mockImplementation( () => <div /> );
|
|
366
|
+
const OriginalComponent = jest.fn( () => <div /> );
|
|
402
367
|
|
|
403
368
|
const DataBoundComponent = compose( [
|
|
404
369
|
withSelect( mapSelectToProps ),
|
|
405
370
|
] )( OriginalComponent );
|
|
406
371
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
</RegistryProvider>
|
|
413
|
-
);
|
|
414
|
-
} );
|
|
372
|
+
const { rerender } = render(
|
|
373
|
+
<RegistryProvider value={ registry }>
|
|
374
|
+
<DataBoundComponent />
|
|
375
|
+
</RegistryProvider>
|
|
376
|
+
);
|
|
415
377
|
|
|
416
378
|
// 2 times:
|
|
417
379
|
// - 1 on initial render
|
|
@@ -419,19 +381,18 @@ describe( 'withSelect', () => {
|
|
|
419
381
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
420
382
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
421
383
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
<
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
);
|
|
428
|
-
} );
|
|
384
|
+
rerender(
|
|
385
|
+
<RegistryProvider value={ registry }>
|
|
386
|
+
<DataBoundComponent propName="foo" />
|
|
387
|
+
</RegistryProvider>
|
|
388
|
+
);
|
|
429
389
|
|
|
430
390
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
431
391
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
|
|
432
392
|
} );
|
|
433
393
|
|
|
434
|
-
it( 'should not rerun selection on unchanging state', () => {
|
|
394
|
+
it( 'should not rerun selection on unchanging state', async () => {
|
|
395
|
+
const registry = createRegistry();
|
|
435
396
|
const store = registry.registerStore( 'unchanging', {
|
|
436
397
|
reducer: ( state = {} ) => state,
|
|
437
398
|
selectors: {
|
|
@@ -440,20 +401,17 @@ describe( 'withSelect', () => {
|
|
|
440
401
|
} );
|
|
441
402
|
|
|
442
403
|
const mapSelectToProps = jest.fn();
|
|
443
|
-
|
|
444
|
-
const OriginalComponent = jest.fn().mockImplementation( () => <div /> );
|
|
404
|
+
const OriginalComponent = jest.fn( () => <div /> );
|
|
445
405
|
|
|
446
406
|
const DataBoundComponent = compose( [
|
|
447
407
|
withSelect( mapSelectToProps ),
|
|
448
408
|
] )( OriginalComponent );
|
|
449
409
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
<
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
);
|
|
456
|
-
} );
|
|
410
|
+
render(
|
|
411
|
+
<RegistryProvider value={ registry }>
|
|
412
|
+
<DataBoundComponent />
|
|
413
|
+
</RegistryProvider>
|
|
414
|
+
);
|
|
457
415
|
|
|
458
416
|
// 2 times:
|
|
459
417
|
// - 1 on initial render
|
|
@@ -461,13 +419,14 @@ describe( 'withSelect', () => {
|
|
|
461
419
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
462
420
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
463
421
|
|
|
464
|
-
store.dispatch( { type: 'dummy' } );
|
|
422
|
+
await act( async () => store.dispatch( { type: 'dummy' } ) );
|
|
465
423
|
|
|
466
424
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
467
425
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
468
426
|
} );
|
|
469
427
|
|
|
470
428
|
it( 'omits props which are not returned on subsequent mappings', () => {
|
|
429
|
+
const registry = createRegistry();
|
|
471
430
|
registry.registerStore( 'demo', {
|
|
472
431
|
reducer: ( state = 'OK' ) => state,
|
|
473
432
|
selectors: {
|
|
@@ -475,32 +434,24 @@ describe( 'withSelect', () => {
|
|
|
475
434
|
},
|
|
476
435
|
} );
|
|
477
436
|
|
|
478
|
-
const mapSelectToProps = jest
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
};
|
|
484
|
-
} );
|
|
437
|
+
const mapSelectToProps = jest.fn( ( _select, ownProps ) => {
|
|
438
|
+
return {
|
|
439
|
+
[ ownProps.propName ]: _select( 'demo' ).getValue(),
|
|
440
|
+
};
|
|
441
|
+
} );
|
|
485
442
|
|
|
486
|
-
const OriginalComponent = jest
|
|
487
|
-
.
|
|
488
|
-
|
|
489
|
-
<div>{ JSON.stringify( props ) }</div>
|
|
490
|
-
) );
|
|
443
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
444
|
+
<div role="status">{ JSON.stringify( props ) }</div>
|
|
445
|
+
) );
|
|
491
446
|
|
|
492
447
|
const DataBoundComponent =
|
|
493
448
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
494
449
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
</RegistryProvider>
|
|
501
|
-
);
|
|
502
|
-
} );
|
|
503
|
-
const testInstance = testRenderer.root;
|
|
450
|
+
const { rerender } = render(
|
|
451
|
+
<RegistryProvider value={ registry }>
|
|
452
|
+
<DataBoundComponent propName="foo" />
|
|
453
|
+
</RegistryProvider>
|
|
454
|
+
);
|
|
504
455
|
|
|
505
456
|
// 2 times:
|
|
506
457
|
// - 1 on initial render
|
|
@@ -508,32 +459,31 @@ describe( 'withSelect', () => {
|
|
|
508
459
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
509
460
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
510
461
|
|
|
511
|
-
expect(
|
|
512
|
-
JSON.
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
462
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent(
|
|
463
|
+
JSON.stringify( {
|
|
464
|
+
propName: 'foo',
|
|
465
|
+
foo: 'OK',
|
|
466
|
+
} )
|
|
467
|
+
);
|
|
517
468
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
<
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
);
|
|
524
|
-
} );
|
|
469
|
+
rerender(
|
|
470
|
+
<RegistryProvider value={ registry }>
|
|
471
|
+
<DataBoundComponent propName="bar" />
|
|
472
|
+
</RegistryProvider>
|
|
473
|
+
);
|
|
525
474
|
|
|
526
475
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
527
476
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
|
|
528
|
-
expect(
|
|
529
|
-
JSON.
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
477
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent(
|
|
478
|
+
JSON.stringify( {
|
|
479
|
+
propName: 'bar',
|
|
480
|
+
bar: 'OK',
|
|
481
|
+
} )
|
|
482
|
+
);
|
|
534
483
|
} );
|
|
535
484
|
|
|
536
485
|
it( 'allows undefined return from mapSelectToProps', () => {
|
|
486
|
+
const registry = createRegistry();
|
|
537
487
|
registry.registerStore( 'demo', {
|
|
538
488
|
reducer: ( state = 'OK' ) => state,
|
|
539
489
|
selectors: {
|
|
@@ -541,72 +491,57 @@ describe( 'withSelect', () => {
|
|
|
541
491
|
},
|
|
542
492
|
} );
|
|
543
493
|
|
|
544
|
-
const mapSelectToProps = jest
|
|
545
|
-
.
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
}
|
|
552
|
-
} );
|
|
494
|
+
const mapSelectToProps = jest.fn( ( _select, ownProps ) => {
|
|
495
|
+
if ( ownProps.pass ) {
|
|
496
|
+
return {
|
|
497
|
+
count: _select( 'demo' ).getValue(),
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
} );
|
|
553
501
|
|
|
554
|
-
const OriginalComponent = jest
|
|
555
|
-
.
|
|
556
|
-
|
|
557
|
-
<div>{ props.count || 'Unknown' }</div>
|
|
558
|
-
) );
|
|
502
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
503
|
+
<div role="status">{ props.count || 'Unknown' }</div>
|
|
504
|
+
) );
|
|
559
505
|
|
|
560
506
|
const DataBoundComponent =
|
|
561
507
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
562
508
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
</RegistryProvider>
|
|
569
|
-
);
|
|
570
|
-
} );
|
|
571
|
-
const testInstance = testRenderer.root;
|
|
509
|
+
const { rerender } = render(
|
|
510
|
+
<RegistryProvider value={ registry }>
|
|
511
|
+
<DataBoundComponent pass={ false } />
|
|
512
|
+
</RegistryProvider>
|
|
513
|
+
);
|
|
572
514
|
|
|
573
515
|
// 2 times:
|
|
574
516
|
// - 1 on initial render
|
|
575
517
|
// - 1 on effect before subscription set.
|
|
576
518
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
577
519
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
578
|
-
expect(
|
|
579
|
-
'Unknown'
|
|
580
|
-
);
|
|
520
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'Unknown' );
|
|
581
521
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
<
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
);
|
|
588
|
-
} );
|
|
522
|
+
rerender(
|
|
523
|
+
<RegistryProvider value={ registry }>
|
|
524
|
+
<DataBoundComponent pass />
|
|
525
|
+
</RegistryProvider>
|
|
526
|
+
);
|
|
589
527
|
|
|
590
528
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 3 );
|
|
591
529
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
|
|
592
|
-
expect(
|
|
530
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'OK' );
|
|
593
531
|
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
<
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
);
|
|
600
|
-
} );
|
|
532
|
+
rerender(
|
|
533
|
+
<RegistryProvider value={ registry }>
|
|
534
|
+
<DataBoundComponent pass={ false } />
|
|
535
|
+
</RegistryProvider>
|
|
536
|
+
);
|
|
601
537
|
|
|
602
538
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 4 );
|
|
603
539
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 3 );
|
|
604
|
-
expect(
|
|
605
|
-
'Unknown'
|
|
606
|
-
);
|
|
540
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'Unknown' );
|
|
607
541
|
} );
|
|
608
542
|
|
|
609
|
-
it( 'should limit unnecessary selections run on children', () => {
|
|
543
|
+
it( 'should limit unnecessary selections run on children', async () => {
|
|
544
|
+
const registry = createRegistry();
|
|
610
545
|
registry.registerStore( 'childRender', {
|
|
611
546
|
reducer: ( state = true, action ) =>
|
|
612
547
|
action.type === 'TOGGLE_RENDER' ? ! state : state,
|
|
@@ -619,20 +554,14 @@ describe( 'withSelect', () => {
|
|
|
619
554
|
} );
|
|
620
555
|
|
|
621
556
|
const childMapSelectToProps = jest.fn();
|
|
622
|
-
const parentMapSelectToProps = jest
|
|
623
|
-
.
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
.mockImplementation( () => <div /> );
|
|
631
|
-
const ParentOriginalComponent = jest
|
|
632
|
-
.fn()
|
|
633
|
-
.mockImplementation( ( props ) => (
|
|
634
|
-
<div>{ props.isRenderingChild ? <Child /> : null }</div>
|
|
635
|
-
) );
|
|
557
|
+
const parentMapSelectToProps = jest.fn( ( _select ) => ( {
|
|
558
|
+
isRenderingChild: _select( 'childRender' ).getValue(),
|
|
559
|
+
} ) );
|
|
560
|
+
|
|
561
|
+
const ChildOriginalComponent = jest.fn( () => <div /> );
|
|
562
|
+
const ParentOriginalComponent = jest.fn( ( props ) => (
|
|
563
|
+
<div>{ props.isRenderingChild ? <Child /> : null }</div>
|
|
564
|
+
) );
|
|
636
565
|
|
|
637
566
|
const Child = withSelect( childMapSelectToProps )(
|
|
638
567
|
ChildOriginalComponent
|
|
@@ -641,13 +570,11 @@ describe( 'withSelect', () => {
|
|
|
641
570
|
ParentOriginalComponent
|
|
642
571
|
);
|
|
643
572
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
<
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
);
|
|
650
|
-
} );
|
|
573
|
+
render(
|
|
574
|
+
<RegistryProvider value={ registry }>
|
|
575
|
+
<Parent />
|
|
576
|
+
</RegistryProvider>
|
|
577
|
+
);
|
|
651
578
|
|
|
652
579
|
// 2 times:
|
|
653
580
|
// - 1 on initial render
|
|
@@ -657,7 +584,7 @@ describe( 'withSelect', () => {
|
|
|
657
584
|
expect( ChildOriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
658
585
|
expect( ParentOriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
659
586
|
|
|
660
|
-
act( () => {
|
|
587
|
+
await act( async () => {
|
|
661
588
|
registry.dispatch( 'childRender' ).toggleRender();
|
|
662
589
|
} );
|
|
663
590
|
|
|
@@ -668,7 +595,7 @@ describe( 'withSelect', () => {
|
|
|
668
595
|
} );
|
|
669
596
|
|
|
670
597
|
it( 'should rerun selection on registry change', () => {
|
|
671
|
-
const firstRegistry =
|
|
598
|
+
const firstRegistry = createRegistry();
|
|
672
599
|
firstRegistry.registerStore( 'demo', {
|
|
673
600
|
reducer: ( state = 'first' ) => state,
|
|
674
601
|
selectors: {
|
|
@@ -676,28 +603,22 @@ describe( 'withSelect', () => {
|
|
|
676
603
|
},
|
|
677
604
|
} );
|
|
678
605
|
|
|
679
|
-
const mapSelectToProps = jest
|
|
680
|
-
.
|
|
681
|
-
|
|
682
|
-
value: _select( 'demo' ).getValue(),
|
|
683
|
-
} ) );
|
|
606
|
+
const mapSelectToProps = jest.fn( ( _select ) => ( {
|
|
607
|
+
value: _select( 'demo' ).getValue(),
|
|
608
|
+
} ) );
|
|
684
609
|
|
|
685
|
-
const OriginalComponent = jest
|
|
686
|
-
.
|
|
687
|
-
|
|
610
|
+
const OriginalComponent = jest.fn( ( props ) => (
|
|
611
|
+
<div role="status">{ props.value }</div>
|
|
612
|
+
) );
|
|
688
613
|
|
|
689
614
|
const DataBoundComponent =
|
|
690
615
|
withSelect( mapSelectToProps )( OriginalComponent );
|
|
691
616
|
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
</RegistryProvider>
|
|
698
|
-
);
|
|
699
|
-
} );
|
|
700
|
-
const testInstance = testRenderer.root;
|
|
617
|
+
const { rerender } = render(
|
|
618
|
+
<RegistryProvider value={ firstRegistry }>
|
|
619
|
+
<DataBoundComponent />
|
|
620
|
+
</RegistryProvider>
|
|
621
|
+
);
|
|
701
622
|
|
|
702
623
|
// 2 times:
|
|
703
624
|
// - 1 on initial render
|
|
@@ -705,9 +626,7 @@ describe( 'withSelect', () => {
|
|
|
705
626
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 2 );
|
|
706
627
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 1 );
|
|
707
628
|
|
|
708
|
-
expect(
|
|
709
|
-
children: 'first',
|
|
710
|
-
} );
|
|
629
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'first' );
|
|
711
630
|
|
|
712
631
|
const secondRegistry = createRegistry();
|
|
713
632
|
secondRegistry.registerStore( 'demo', {
|
|
@@ -717,13 +636,12 @@ describe( 'withSelect', () => {
|
|
|
717
636
|
},
|
|
718
637
|
} );
|
|
719
638
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
<
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
} );
|
|
639
|
+
rerender(
|
|
640
|
+
<RegistryProvider value={ secondRegistry }>
|
|
641
|
+
<DataBoundComponent />
|
|
642
|
+
</RegistryProvider>
|
|
643
|
+
);
|
|
644
|
+
|
|
727
645
|
// 4 times:
|
|
728
646
|
// - 1 on initial render
|
|
729
647
|
// - 1 on effect before subscription set.
|
|
@@ -731,9 +649,6 @@ describe( 'withSelect', () => {
|
|
|
731
649
|
// - 1 on effect before new subscription set (because registry has changed)
|
|
732
650
|
expect( mapSelectToProps ).toHaveBeenCalledTimes( 4 );
|
|
733
651
|
expect( OriginalComponent ).toHaveBeenCalledTimes( 2 );
|
|
734
|
-
|
|
735
|
-
expect( testInstance.findByType( 'div' ).props ).toEqual( {
|
|
736
|
-
children: 'second',
|
|
737
|
-
} );
|
|
652
|
+
expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'second' );
|
|
738
653
|
} );
|
|
739
654
|
} );
|