@@ -262,36 +262,26 @@ import React from 'react';
262
262
import { unmountComponentAtNode } from ' react-dom' ;
263
263
import { act } from ' react-dom/test-utils' ;
264
264
import { 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 = {
277
271
t: jest .fn (),
278
272
fontSize: 12 ,
279
273
autosave: false ,
280
274
setFontSize: jest .fn (),
281
275
setAutosave: jest .fn (),
282
- ... extraProps
283
276
};
284
- render (< FakePreferences {... props} / > , { container });
285
277
286
- return props;
287
- };
278
+ const subject = () => {
279
+ render (< FakePreferences {... subjectProps} / > , { container });
280
+ };
288
281
289
- describe (' <FakePreferences />' , () => {
290
- let container = null ;
291
282
beforeEach (() => {
292
283
// setup a DOM element as a render target
293
284
container = document .createElement (' div' );
294
- container .classList .add (' testing-container' );
295
285
document .body .appendChild (container);
296
286
});
297
287
@@ -300,22 +290,34 @@ describe('<FakePreferences />', () => {
300
290
unmountComponentAtNode (container);
301
291
container .remove ();
302
292
container = null ;
303
- });
304
293
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 ();
317
302
});
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
+ })
318
318
});
319
+
320
+ });
319
321
` ` `
320
322
321
323
Consider what you want to test. Some possible things might be:
0 commit comments