@@ -262,36 +262,26 @@ import React from 'react';
262262import { unmountComponentAtNode } from ' react-dom' ;
263263import { act } from ' react-dom/test-utils' ;
264264import { fireEvent , render , screen } from ' ../../../../test-utils' ;
265- import FakePreferences from ' ./index' ;
266-
267- /* a helper function to render the components with the
268- * props that that component needs to be passed in
269- * if you want to access the rendered component itself
270- * you'd have to modify it a little to return the what
271- * gets returned from the render function, along with the props, which is what it's returning now.
272- * the default props in this can be overwritten by using extraProps
273- */
274- const renderComponent = (extraProps = {}, container ) => {
275- // if we want to overwrite any of these props, we can do it with extraProps because later keys overwrite earlier ones in the spread operator
276- const props = {
265+ import MyComponent from ' ./MyComponent' ;
266+
267+ describe (' <MyComponent />' , () => {
268+ let container = null ;
269+
270+ let subjectProps = {
277271 t: jest .fn (),
278272 fontSize: 12 ,
279273 autosave: false ,
280274 setFontSize: jest .fn (),
281275 setAutosave: jest .fn (),
282- ... extraProps
283276 };
284- render (< FakePreferences {... props} / > , { container });
285277
286- return props;
287- };
278+ const subject = () => {
279+ render (< FakePreferences {... subjectProps} / > , { container });
280+ };
288281
289- describe (' <FakePreferences />' , () => {
290- let container = null ;
291282 beforeEach (() => {
292283 // setup a DOM element as a render target
293284 container = document .createElement (' div' );
294- container .classList .add (' testing-container' );
295285 document .body .appendChild (container);
296286 });
297287
@@ -300,22 +290,34 @@ describe('<FakePreferences />', () => {
300290 unmountComponentAtNode (container);
301291 container .remove ();
302292 container = null ;
303- });
304293
305- describe (' font tests' , () => {
306- it (' font size increase button says increase' , () => {
307- let props;
308- // render the component
309- act (() => {
310- props = renderComponent ({fontSize: 15 }, container);
311- });
312-
313- // I do tests here.
314- // you can access mock functions from props
315- // for example, props.setFontSize
316-
294+ // reset the mocks in subjectProps
295+ jest .clearAllMocks ();
296+ });
297+
298+ it (' I am the test description' , () => {
299+ // render the component
300+ act (() => {
301+ subject ();
317302 });
303+
304+ // I do tests here.
305+ // you can access mock functions from subjectProps. For example, subjectProps.setFontSize
306+
307+ });
308+
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+ })
318318 });
319+
320+ });
319321` ` `
320322
321323Consider what you want to test. Some possible things might be:
0 commit comments