Skip to content

Commit ab7d9c3

Browse files
committed
formatting fixes
1 parent f57e7c1 commit ab7d9c3

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

developer_docs/testing.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ Many files still don't have tests, so if you're looking to get started as a cont
3030
## Useful testing commands
3131
Run the whole test suite
3232
```
33-
npm run test
33+
$ npm run test
3434
```
3535

3636
-----
3737

3838
[Run tests that match the pattern](https://stackoverflow.com/questions/28725955/how-do-i-test-a-single-file-using-jest/28775887). Useful if you're writing one specific test and want to only run that one.
3939
```
40-
npm run test -- someFileName
40+
$ npm run test -- someFileName
4141
```
4242

4343
-----
4444
Run the tests but update the snapshot if they don't match.
4545

4646
```
47-
npm run test -- -u
47+
$ npm run test -- -u
4848
```
4949

5050
----
5151
For example, if you wanted to run just the SketchList test but also update the snapshot, you could do:
5252
```
53-
npm run test -- Sketchlist.test.js -u
53+
$ npm run test -- Sketchlist.test.js -u
5454
```
5555

5656
Find more commands in the [Jest documentation](https://jestjs.io/docs/cli).
@@ -81,7 +81,7 @@ Want to get started writing a test for a new file or an existing file, but not s
8181
### What to test
8282
For any type of component, you might want to consider testing:
8383
- User input results in the class's method being called.
84-
```
84+
```js
8585
//component is the return value of calling render()
8686
const spy1 = jest.spyOn(component.instance(), 'func1');
8787
act(() => {
@@ -99,18 +99,18 @@ See the [redux section](#Testing-Redux) below :)
9999
### Troubleshooting
100100
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.
101101

102-
```
102+
```js
103103
axios.get.mockImplementationOnce(
104104
(x) => Promise.resolve({ data: 'foo' })
105105
);
106106
```
107-
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
107+
You can see it used in the context of a test [in the SketchList.test.jsx file](../client/modules/IDE/components/SketchList.test.jsx).
108108

109109
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:
110-
```
110+
```js
111111
jest.mock('_path_to_file_/i18n');
112112
```
113-
You can see it used in the context of a test [here](../client/modules/IDE/components/SketchList.test.jsx).
113+
You can also see it used in the context of a test [in the SketchList.test.jsx file](../client/modules/IDE/components/SketchList.test.jsx).
114114

115115
## Files to be aware of
116116

@@ -218,19 +218,19 @@ It exports a render function with a i18n wrapper as ``render`` and a render func
218218
Thus, in your component test files, instead of calling ``import {functions you want} from 'react-testing-libary'`` importing react-testing library might look something like this:
219219

220220
If your component only needs i18n and not redux:
221-
```
221+
```js
222222
import { render, fireEvent, screen, waitFor } from '../../../test-utils';
223223
```
224224
If your component needs i18n and redux:
225-
```
225+
```js
226226
import { reduxRender, fireEvent, screen, waitFor } from '../../../test-utils';
227227
```
228228

229229
Redux and i18next are made accessible by placing wrappers around the component. We can do this by replacing the render function with one that renders the requested component WITH an additional wrapper added around it.
230230

231231
For example, the exported render function that adds a wrapper for both redux and i18n looks roughly like this:
232232

233-
```
233+
```js
234234
function reduxRender(
235235
ui,
236236
{
@@ -264,7 +264,7 @@ in progress
264264
## Testing plain components
265265
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 this:
266266

267-
```
267+
```js
268268
import React from 'react';
269269
import { unmountComponentAtNode } from 'react-dom';
270270
import { act } from 'react-dom/test-utils';
@@ -327,7 +327,7 @@ describe('<FakePreferences />', () => {
327327
328328
Consider what you want to test. Some possible things might be:
329329
- User input results in the expected function being called with the expected argument.
330-
```
330+
```js
331331
act(() => {
332332
fireEvent.click(screen.getByTestId("testid"));
333333
});
@@ -352,26 +352,26 @@ Although it's possible to export the components as unconnected components for te
352352
This works like so:
353353
1. Import the reduxRender function from ``client/test_utils.js``
354354
2. Configure the mock store.
355-
```
355+
```js
356356
import configureStore from 'redux-mock-store';
357357
import thunk from 'redux-thunk';
358358

359359

360360
const mockStore = configureStore([thunk]);
361361
```
362362
3. Create a mock store. There's an initial state that you can import from ``client/redux_test_stores/test_store.js``
363-
```
363+
```js
364364
store = mockStore(initialTestState);
365365
```
366366
3. Render the component with reduxRender and the store that you just created.
367-
```
367+
```js
368368
reduxRender(<SketchList username="happydog1" />, {store, container});
369369
```
370370
4. Test things! You may need to use jest to mock certain functions if the component is making API calls.
371371
372372
All together, it might look something like this.
373373
374-
```
374+
```js
375375
import React from 'react';
376376
import configureStore from 'redux-mock-store';
377377
import thunk from 'redux-thunk';
@@ -417,7 +417,7 @@ describe(<MyComponent />, () => {
417417
418418
Some things to consider testing:
419419
- User input results in the expected redux action.
420-
```
420+
```js
421421
act(() => {
422422
component = reduxRender(<SketchList username="happydog2" />, {
423423
store,

0 commit comments

Comments
 (0)