You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_docs/testing.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -261,7 +261,7 @@ function reduxRender(
261
261
262
262
263
263
### testData
264
-
This folder contains the test data that you can use in your tests, including inital redux states that you can provide to the ``reduxRender`` function when testing. For example, if you want to render the SketchList component with a username of ``happydog`` and some sample sketches, ``redux_test_stores\test_store.js`` contains a definition for that state that you can import and provide to the renderer. The folder also contains test data that you can use for msw server so that the server returns json with the correct format and fields.
264
+
This folder contains the test data that you can use in your tests, including inital redux states that you can provide to the ``reduxRender`` function when testing. For example, if you want to render the SketchList component with a username of ``happydog`` and some sample sketches, ``testData/testReduxStore.js`` contains a definition for that state that you can import and provide to the renderer. The folder also contains test data that you can use for ``msw`` server so that the server returns json with the correct format and fields.
265
265
266
266
## Testing plain components
267
267
If it doesn't contain ``connect(mapStateToProps, mapDispatchToProps)(ComponentName)`` or use hooks like ``useSelector``, then your component is not directly using Redux and testing your component will be simpler and might look something like the code below. Notably, we descibe the component being tested as the [subject under test](http://xunitpatterns.com/SUT.html) by creating a function called ``subject`` that renders the component with the subject dependencies (the props) that are defined in the same scope. They're declared with ``let`` so that they can be overwritten in a nested ``describe``block that tests different dependencies. This keeps the subject function consistent between test suites and explicitly declares variables that can affect the outcome of the test.
@@ -341,7 +341,7 @@ Testing reducers and action creators is covered pretty well in [Redux's document
341
341
342
342
### Connected Components
343
343
344
-
Although it'spossibletoexportthecomponentsasunconnectedcomponentsfortesting (andinthiscaseyouwouldjustmanuallypassinthepropsthatreduxprovides), thecodebaseisbeingmigratedtousehooks, andinthiscase, thatapproachnolongerworks. Italsodoesn't work if we render components that have connected subcomponents. Thus, for consistency, we suggest testing all redux components while they'reconnectedtoredux. Wecandothiswithredux-mock-store.
344
+
Although it'spossibletoexportthecomponentsasunconnectedcomponentsfortesting (andinthiscaseyouwouldjustmanuallypassinthepropsthatreduxprovides), thecodebaseisbeingmigratedtousehooks, andinthiscase, thatapproachnolongerworks. Italsodoesn't work if we render components that have connected subcomponents. Thus, for consistency, we suggest testing all redux components while they'reconnectedtoredux. Wecandothiswith``redux-mock-store``.
345
345
346
346
Thisworkslikeso:
347
347
1.ImportthereduxRenderfunction from ``client/test_utils.js``
@@ -353,7 +353,7 @@ import thunk from 'redux-thunk';
353
353
354
354
const mockStore = configureStore([thunk]);
355
355
```
356
-
3. Create a mock store. There's an initial state that you can import from ``client/redux_test_stores/test_store.js``
356
+
3. Create a mock store. There's an initial state that you can import from ``client/testData/testReduxStore.js``
357
357
```js
358
358
store = mockStore(initialTestState);
359
359
```
@@ -441,6 +441,8 @@ Some things to consider testing:
441
441
442
442
Some tests throw errors if a part of the client-side code tries to make an API call or AJAX request. Our solution to this is to use the [Mock Service Worker library](https://mswjs.io/) to mock the API requests by intercepting requests on the network level [[2]](#References). It can handle API calls and return appropriate data (you can see what shape of data gets returned by looking through the server files). There is some test data available in the ``client/testData/testServerResponse.js`` file, but you may need to edit the file to add a new json response if an appropriate one doesn't exist already. The example code below sets up a server to respond to a GET request at ``/exampleendpoint`` by returning ``{data: foo}`` You can see it in the context of a test [in the SketchList.test.jsx file](../client/modules/IDE/components/SketchList.test.jsx).
443
443
444
+
There's a longer explaination of the benefits of ``msw`` in [this article by Kent C Dodds](https://kentcdodds.com/blog/stop-mocking-fetch).
If the component makes use of the formatDate util, some of the functions in that rely on the ``./client/i18n.js`` file that also makes an ajax request, which sometimes leads to an ERRCONNECTED error on the console, even though your tests pass. You can fix it by adding a mock for that specific i18n file:
459
+
If the component makes use of the ``formatDate`` util, some of the functions in that rely on the ``./client/i18n.js`` file that also makes an ajax request, which sometimes leads to an ERRCONNECTED error on the console, even though your tests pass. You can fix it by adding a mock for that specific i18n file:
0 commit comments