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
@@ -55,10 +55,6 @@ npm run test -- Sketchlist.test.js -u
55
55
56
56
Find more commands in the [Jest documentation](https://jestjs.io/docs/cli).
57
57
58
-
## When to run tests
59
-
60
-
Are they actually being run automaticlly???
61
-
62
58
## Why write tests
63
59
- Good place to start if you're learning the codebase because it's harder to mess up production code
64
60
- Benefits all future contributors by allowing them to check their changes for errors
@@ -67,20 +63,39 @@ Are they actually being run automaticlly???
67
63
- Good practice for large projects
68
64
- Many of the existing components don't have tests yet, and you could write one :-)
69
65
66
+
## When to run tests
67
+
68
+
When you make a git commit, the tests will be run automatically for you.
69
+
70
+
When you modify an existing component, it's a good idea to run the test suite to make sure it didn't make any changes that break the rest of the application. If they did break some tests, you would either have to fix a bug component or update the tests to match the new expected functionality.
71
+
70
72
## Writing a test
71
73
Want to get started writing a test for a new file or an existing file, but not sure how?
72
74
### For React components
73
-
75
+
(the below assumes we're using proposed folder structure 1)
74
76
1. Make a new file in the ``__tests__`` folder that's directly adjacent to your file. For example, if ``example.jsx`` is in ``src/components``, then you would make a file called ``example.test.jsx`` in ``src/components/__tests__``
75
77
2. Check if the component is connected to redux or not.
76
78
3. If it is, see the redux section below on how to write tests for that.
77
79
4. If it's not, see the section below on writing tests for unconnected components.
78
80
79
81
### For Redux action creators or reducers
80
-
See the redux section below :)
82
+
See the [redux section](#Testing-Redux) below :)
83
+
84
+
### Troubleshooting
85
+
1. Check if the component makes any API calls. If it's using axios, jest should already be set up to replace the axios library with a mocked version; however, you may want to [mock](https://jestjs.io/docs/mock-function-api#mockfnmockimplementationoncefn) the axios.get() function with your own version so that GET calls "return" whatever data makes sense for that test.
86
+
87
+
```
88
+
axios.get.mockImplementationOnce(
89
+
(x) => Promise.resolve({ data: 'foo' })
90
+
);
91
+
```
92
+
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
81
93
82
-
### For server side code
83
-
lol no clue how to do this yet
94
+
2. 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:
95
+
```
96
+
jest.mock('_path_to_file_/i18n');
97
+
```
98
+
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
84
99
85
100
## Files to be aware of
86
101
@@ -224,7 +239,6 @@ function reduxRender(
224
239
```
225
240
226
241
227
-
228
242
### redux_test_stores
229
243
This folder contains the 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.
230
244
@@ -233,59 +247,226 @@ This folder contains the inital redux states that you can provide to the ``redux
233
247
this i dont know much about yet but want to understand
234
248
235
249
## Testing plain components
236
-
If it doesn't export connect()__stuffhere_ or use redux hooks like adfasdf, then testing your component will be simpler and will look like this:
250
+
If it doesn't export connect()___ or use redux hooks like ___, then testing your component will be simpler and might look something like this:
251
+
252
+
```
253
+
import React from 'react';
254
+
import { unmountComponentAtNode } from 'react-dom';
255
+
import { act } from 'react-dom/test-utils';
256
+
import { fireEvent, render, screen } from '../../../../test-utils';
257
+
import FakePreferences from './index';
258
+
259
+
/* a helper function to render the components with the
260
+
* props that that component needs to be passed in
261
+
* if you want to access the rendered component itself
262
+
* you'd have to modify it a little to return the what
263
+
* gets returned from the render function, along with the props, which is what it's returning now.
264
+
* the default props in this can be overwritten by using extraProps
- The text or divs that you expect to be on the page are actually there.
332
+
- a previously saved snapshot of the HTML matches a snapshot taken during testing.
333
+
- what else???? help!
238
334
239
335
## Testing Redux
240
336
241
-
split up testing between:
337
+
When testing redux, the general guidance [1] seems to suggest splitting up testing between:
242
338
1. action creators
243
339
2. reducers
244
340
3. connected components
245
341
342
+
Testing reducers and action creators is covered pretty well in [Redux's documentation](https://redux.js.org/recipes/writing-tests). An example of testing an action creator can be found at [projects.test.js](../client/modules/IDE/components/actions/__tests__/projects.test.jsx)
246
343
247
-
4. unconnected components
344
+
### Connected Components
248
345
249
-
### action creators
250
-
write example code here
251
-
- can show cassie projects.test.js because that one is working :)
346
+
Although it's possible to export the components as unconnected components for testing (and in this case you would just manually pass in the props that redux provides), the codebase is being migrated to use hooks, and in this case, that approach no longer works. It also doesn't work if we render components that have connected subcomponents. Thus, for consistency, we suggest testing all redux components while they're connected to redux. We can do this with redux-mock-store.
252
347
253
-
### reducers
254
-
write example code here
255
-
### connected components
256
-
3 approaches im trying for sketchlist
257
-
- mock all of axios, let it run the action creators as usual, redux-mock-store and then render component
258
-
- export unconnected component and use that
259
-
- this method didn't work because the subcomponent that was also redux connected failed. I could use shallow rendering but that's not supported in react-testing-library (I think)
260
-
- mock getProjects itself so it never calls apiClient at all
348
+
This works like so:
349
+
1. Import the reduxRender function from ``client/test_utils.js``
350
+
2. Configure the mock store.
351
+
```
352
+
import configureStore from 'redux-mock-store';
353
+
import thunk from 'redux-thunk';
261
354
262
-
each has its own errors :/ i realized that the third approach is flawed because a lot of the functions rely on apiClient. Also, apiClient calls axios.create before any of the tests even run at all. overall, only mocking getProjects is a fragile solution
355
+
356
+
const mockStore = configureStore([thunk]);
357
+
```
358
+
3. Create a mock store. There's an initial state that you can import from ``client/redux_test_stores/test_store.js``
359
+
```
360
+
store = mockStore(initialTestState);
361
+
```
362
+
3. Render the component with reduxRender and the store that you just created.
- The text or divs that you expect to be on the page are actually there.
441
+
- a previously saved snapshot of the HTML matches a snapshot taken during testing.
442
+
- what else???? help!
263
443
264
444
## How to handle API calls in tests
265
445
266
-
doesnt seem to like it when you make calls in a test
267
-
so we mock axios
268
-
also a little trickery in i18n .use(Backend)
269
-
- editor uses axios, we mock the whole library and jest automatically does this since we have a axios.js file in the __mocks__ folder at the root of the client folder.
270
-
- the benefit of this is that you can control exactly what happens when any axios function gets called, and you can check how many times it's been called.
271
-
-[see this for more](https://stackoverflow.com/questions/51393952/mock-inner-axios-create/51414152#51414152)
272
-
-[and this too](https://medium.com/asos-techblog/how-to-test-your-react-redux-application-48d90481a253)
446
+
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 jest to replace those functions with [mock functions](https://jestjs.io/docs/mock-functions).
447
+
448
+
The code in question for the client side is mostly related to the axios library. We mock the whole library - jest automatically does this since we have an ``axios.js`` file in the ``__mocks__`` folder at the root of the client folder. [1][2]
449
+
450
+
The benefit of this is that you can control exactly what happens when any axios function gets called, and you can check how many times it's been called.
451
+
452
+
A few components also import ``./client/i18n.js`` (or ``./client/utils/formatDate``, which imports the first file), in which the ``i18n.use(Backend)`` line can sometimes throw a sneaky ERRCONNECTED error. You can resolve this by mocking that file as described in [this section](#Troubleshooting).
273
453
274
454
## Some more background on tests
275
455
276
456
### test driven development (TDD)
457
+
BDD???
277
458
278
459
### snapshot testing
279
-
want to make an example
460
+
You can save a snapshot of what the HTML looks like when the component is rendered.
280
461
281
462
### integration tests
463
+
Testing multiple components together. A small example is rendering a parent component and a child component within that.
282
464
283
465
### unit tests
284
-
285
-
### mocking functions
286
-
Sometimes you might want to mock a function
466
+
Most of our tests are of this type. In this, you're testing a the functionality of a single component and no more.
287
467
288
468
## Internationalization
469
+
Project uses i18n.
289
470
290
471
## Tips
291
472
1. Make test fail at least once to make sure it was a meaningful test
@@ -296,6 +477,6 @@ Sometimes you might want to mock a function
0 commit comments