@@ -270,9 +270,7 @@ describe('<MyComponent />', () => {
270270 let subjectProps = {
271271 t: jest .fn (),
272272 fontSize: 12 ,
273- autosave: false ,
274- setFontSize: jest .fn (),
275- setAutosave: jest .fn (),
273+ setFontSize: jest .fn ()
276274 };
277275
278276 const subject = () => {
@@ -301,20 +299,22 @@ describe('<MyComponent />', () => {
301299 subject ();
302300 });
303301
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+ */
306306
307307 });
308308
309309 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+ });
318318 });
319319
320320});
@@ -365,21 +365,34 @@ reduxRender(<SketchList username="happydog1" />, {store, container});
365365
366366All together, it might look something like this.
367367
368+
369+ *MyReduxComponent.test.jsx*
368370` ` ` js
369371import React from ' react' ;
370372import configureStore from ' redux-mock-store' ;
371373import thunk from ' redux-thunk' ;
372374import { unmountComponentAtNode } from ' react-dom' ;
373375import { act } from ' react-dom/test-utils' ;
374- import MyComponent from ' ./MyComponent ' ;
376+ import MyReduxComponent from ' ./MyReduxComponent ' ;
375377import { reduxRender , fireEvent , screen } from ' ../../../test-utils' ;
376378import { initialTestState } from ' ../../../redux_test_stores/test_store' ;
377379
378- describe (< MyComponent / > , () => {
379- let container;
380+ describe (' <MyReduxComponent />' , () => {
381+ let container = null ;
380382 const mockStore = configureStore ([thunk]);
381383 const store = mockStore (initialTestState);
382384
385+ let subjectProps = {
386+ sampleprop: " foo"
387+ };
388+
389+ const subject = () => {
390+ reduxRender (< MyComponent {... subjectProps} / > , {
391+ store,
392+ container
393+ });
394+ };
395+
383396 beforeEach (() => {
384397 // setup a DOM element as a render target
385398 container = document .createElement (' div' );
@@ -391,22 +404,39 @@ describe(<MyComponent />, () => {
391404 unmountComponentAtNode (container);
392405 container .remove ();
393406 container = null ;
407+
408+ // reset the mocks in subjectProps
409+ jest .clearAllMocks ();
410+
411+ // clear the mock store too
394412 store .clearActions ();
395413 });
396-
397- it (' stuff about the test' , () => {
398- let component;
414+
415+ it (' I am the test description ' , () => {
416+ // render the component
399417 act (() => {
400- component = reduxRender (< MyComponent sampleprop= " foo" / > , {
401- store,
402- container
403- });
418+ subject ();
404419 });
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 }
405430
406- // your tests go here
431+ it (" here's that test with a different prop" , () => {
432+ act (() => {
433+ subject ();
434+ });
435+ // test here
436+ });
407437 });
408438
409- })
439+ });
410440` ` `
411441
412442Some things to consider testing:
0 commit comments