@@ -270,9 +270,7 @@ describe('<MyComponent />', () => {
270
270
let subjectProps = {
271
271
t: jest .fn (),
272
272
fontSize: 12 ,
273
- autosave: false ,
274
- setFontSize: jest .fn (),
275
- setAutosave: jest .fn (),
273
+ setFontSize: jest .fn ()
276
274
};
277
275
278
276
const subject = () => {
@@ -301,20 +299,22 @@ describe('<MyComponent />', () => {
301
299
subject ();
302
300
});
303
301
304
- // I do tests here.
305
- // you can access mock functions from subjectProps. For example, subjectProps.setFontSize
302
+ /* Tests go here!
303
+ * You can access mock functions from subjectProps.
304
+ * For example, subjectProps.setFontSize
305
+ */
306
306
307
307
});
308
308
309
309
describe (' test with a different prop' , () => {
310
- let subjectProps = {... subjectProps, fontSize: 14 }
311
-
312
- it (" here's that test with a different prop" , () => {
313
- act (() => {
314
- subject ();
315
- });
316
- // test here
317
- })
310
+ let subjectProps = {... subjectProps, fontSize: 14 }
311
+
312
+ it (" here's that test with a different prop" , () => {
313
+ act (() => {
314
+ subject ();
315
+ });
316
+ // test here
317
+ });
318
318
});
319
319
320
320
});
@@ -365,21 +365,34 @@ reduxRender(<SketchList username="happydog1" />, {store, container});
365
365
366
366
All together, it might look something like this.
367
367
368
+
369
+ *MyReduxComponent.test.jsx*
368
370
` ` ` js
369
371
import React from ' react' ;
370
372
import configureStore from ' redux-mock-store' ;
371
373
import thunk from ' redux-thunk' ;
372
374
import { unmountComponentAtNode } from ' react-dom' ;
373
375
import { act } from ' react-dom/test-utils' ;
374
- import MyComponent from ' ./MyComponent ' ;
376
+ import MyReduxComponent from ' ./MyReduxComponent ' ;
375
377
import { reduxRender , fireEvent , screen } from ' ../../../test-utils' ;
376
378
import { initialTestState } from ' ../../../redux_test_stores/test_store' ;
377
379
378
- describe (< MyComponent / > , () => {
379
- let container;
380
+ describe (' <MyReduxComponent />' , () => {
381
+ let container = null ;
380
382
const mockStore = configureStore ([thunk]);
381
383
const store = mockStore (initialTestState);
382
384
385
+ let subjectProps = {
386
+ sampleprop: " foo"
387
+ };
388
+
389
+ const subject = () => {
390
+ reduxRender (< MyComponent {... subjectProps} / > , {
391
+ store,
392
+ container
393
+ });
394
+ };
395
+
383
396
beforeEach (() => {
384
397
// setup a DOM element as a render target
385
398
container = document .createElement (' div' );
@@ -391,22 +404,39 @@ describe(<MyComponent />, () => {
391
404
unmountComponentAtNode (container);
392
405
container .remove ();
393
406
container = null ;
407
+
408
+ // reset the mocks in subjectProps
409
+ jest .clearAllMocks ();
410
+
411
+ // clear the mock store too
394
412
store .clearActions ();
395
413
});
396
-
397
- it (' stuff about the test' , () => {
398
- let component;
414
+
415
+ it (' I am the test description ' , () => {
416
+ // render the component
399
417
act (() => {
400
- component = reduxRender (< MyComponent sampleprop= " foo" / > , {
401
- store,
402
- container
403
- });
418
+ subject ();
404
419
});
420
+
421
+ /* Tests go here!
422
+ * You can access mock functions from subjectProps.
423
+ * For example, subjectProps.setFontSize
424
+ */
425
+
426
+ });
427
+
428
+ describe (' test with a different prop' , () => {
429
+ let subjectProps = {... subjectProps, fontSize: 14 }
405
430
406
- // your tests go here
431
+ it (" here's that test with a different prop" , () => {
432
+ act (() => {
433
+ subject ();
434
+ });
435
+ // test here
436
+ });
407
437
});
408
438
409
- })
439
+ });
410
440
` ` `
411
441
412
442
Some things to consider testing:
0 commit comments