Skip to content

Commit 255e4f8

Browse files
committed
rename files, fix index and editor tests
1 parent a8299ea commit 255e4f8

File tree

9 files changed

+77
-89
lines changed

9 files changed

+77
-89
lines changed

client/index.integration.test.jsx

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import { setupServer } from 'msw/node';
22
import { rest } from 'msw';
3+
import React from 'react';
4+
import { Router, browserHistory } from 'react-router';
5+
36
import {
7+
reduxRender,
48
act,
9+
waitFor,
510
fireEvent,
6-
prettyDOM,
711
screen,
812
within
9-
} from '@testing-library/react';
10-
import userResponse from './testData/testServerResponses';
13+
} from './test-utils';
14+
import configureStore from './store';
15+
import routes from './routes';
16+
import * as Actions from './modules/User/actions';
17+
import { userResponse } from './testData/testServerResponses';
18+
19+
// setup for the app
20+
const history = browserHistory;
21+
const initialState = window.__INITIAL_STATE__;
22+
const store = configureStore(initialState);
1123

1224
// need to mock this file or it'll throw ERRCONNECTED
1325
jest.mock('./i18n');
1426

1527
// setup for the msw fake server
1628
const server = setupServer(
1729
rest.get('/session', (req, res, ctx) =>
18-
res(ctx.json(userResponse.userResponse))
30+
// console.log("called server get session");
31+
res(ctx.json(userResponse))
1932
)
2033
);
2134

@@ -27,6 +40,7 @@ beforeAll(() =>
2740
afterEach(() => server.resetHandlers());
2841
afterAll(() => server.close());
2942

43+
// below are fixes for jsdom-specific errors
3044
// https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t
3145
const noop = () => {};
3246
Object.defineProperty(window, 'focus', { value: noop, writable: true });
@@ -46,17 +60,29 @@ document.createRange = () => {
4660
return range;
4761
};
4862

63+
// start testing
4964
describe('index.jsx integration', () => {
50-
let container = null;
51-
52-
// we only run the setup once because require only works once
53-
beforeAll(() => {
54-
// setup a DOM element as a render target
55-
container = document.createElement('div');
56-
container.id = 'root';
57-
document.body.appendChild(container);
58-
// eslint-disable-next-line global-require
59-
require('./index');
65+
// the subject under test
66+
const subject = () =>
67+
reduxRender(<Router history={history} routes={routes(store)} />, { store });
68+
69+
// spy on this function and wait for it to be called before making assertions
70+
const spy = jest.spyOn(Actions, 'getUser');
71+
72+
beforeEach(async () => {
73+
// console.log("TRYING TO SPY ON GETUSER");
74+
75+
act(() => {
76+
subject();
77+
});
78+
79+
// console.log("WAITING....");
80+
await waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
81+
// console.log("SPY DONE");
82+
});
83+
84+
afterEach(() => {
85+
spy.mockClear();
6086
});
6187

6288
it('navbar items and the dropdowns in the navbar exist', () => {
@@ -158,7 +184,7 @@ describe('index.jsx integration', () => {
158184

159185
// expect(screen.getByText("createCanvas")).toBeInTheDocument();
160186
const codeeditor = screen.getByRole('article');
161-
console.log(prettyDOM(codeeditor));
187+
// console.log(prettyDOM(codeeditor));
162188
expect(indexHTMLButton).toBeInTheDocument();
163189

164190
const startingeditorcode = codeeditor.textContent;

client/modules/App/App.test.jsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

client/modules/IDE/actions/__tests__/projects.test.js renamed to client/modules/IDE/actions/__tests__/projects.unit.test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import configureStore from 'redux-mock-store';
22
import thunk from 'redux-thunk';
3-
import axios from 'axios';
4-
import { unmountComponentAtNode } from 'react-dom';
5-
import { act } from 'react-dom/test-utils';
3+
import { setupServer } from 'msw/node';
4+
import { rest } from 'msw';
5+
66
import * as ProjectActions from '../projects';
77
import * as ActionTypes from '../../../../constants';
88
import {
@@ -12,6 +12,16 @@ import {
1212

1313
const mockStore = configureStore([thunk]);
1414

15+
const server = setupServer(
16+
rest.get(`/${initialTestState.user.username}/projects`, (req, res, ctx) =>
17+
res(ctx.json(mockProjects))
18+
)
19+
);
20+
21+
beforeAll(() => server.listen());
22+
afterEach(() => server.resetHandlers());
23+
afterAll(() => server.close());
24+
1525
describe('projects action creator tests', () => {
1626
let store;
1727

@@ -22,11 +32,6 @@ describe('projects action creator tests', () => {
2232
it('creates GET_PROJECTS after successfuly fetching projects', () => {
2333
store = mockStore(initialTestState);
2434

25-
axios.get.mockImplementationOnce((x) => {
26-
console.log('get was called with ', x);
27-
return Promise.resolve({ data: mockProjects });
28-
});
29-
3035
const expectedActions = [
3136
{ type: ActionTypes.START_LOADING },
3237
{ type: ActionTypes.SET_PROJECTS, projects: mockProjects },

client/modules/IDE/components/Editor.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class Editor extends React.Component {
298298

299299
getContent() {
300300
const content = this._cm.getValue();
301+
console.log(content);
301302
const updatedFile = Object.assign({}, this.props.file, { content });
302303
return updatedFile;
303304
}

client/modules/IDE/components/Editor.unit.test.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import configureStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
4-
import axios from 'axios';
54
import { act } from 'react-dom/test-utils';
65
import Editor from './Editor';
76
import {
@@ -15,6 +14,19 @@ import { initialTestState } from '../../../testData/testReduxStore';
1514

1615
jest.mock('../../../i18n');
1716

17+
// const server = setupServer(
18+
// rest.get(`/${initialTestState.user.username}/projects`, (req, res, ctx) =>
19+
// // it just needs to return something so it doesn't throw an error
20+
// // Sketchlist tries to grab projects on creation but for the unit test
21+
// // we just feed those in as part of the initial state
22+
// res(ctx.json({ data: 'foo' }))
23+
// )
24+
// );
25+
26+
// beforeAll(() => server.listen());
27+
// afterEach(() => server.resetHandlers());
28+
// afterAll(() => server.close());
29+
1830
describe('<Editor />', () => {
1931
const mockStore = configureStore([thunk]);
2032
const store = mockStore(initialTestState);
@@ -23,10 +35,6 @@ describe('<Editor />', () => {
2335

2436
const subject = () => reduxRender(<Editor {...subjectProps} />, { store });
2537

26-
beforeEach(() => {
27-
axios.get.mockImplementationOnce((x) => Promise.resolve({ data: 'foo' }));
28-
});
29-
3038
afterEach(() => {
3139
store.clearActions();
3240
});

client/test-utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Provider } from 'react-redux';
1919
import { I18nextProvider } from 'react-i18next';
2020
import i18n from './i18n-test';
2121
import rootReducer from './reducers';
22+
import ThemeProvider from './modules/App/components/ThemeProvider';
2223

2324
// re-export everything
2425
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -44,7 +45,9 @@ function reduxRender(
4445
function Wrapper({ children }) {
4546
return (
4647
<I18nextProvider i18n={i18n}>
47-
<Provider store={store}>{children}</Provider>
48+
<Provider store={store}>
49+
<ThemeProvider>{children}</ThemeProvider>
50+
</Provider>
4851
</I18nextProvider>
4952
);
5053
}

client/testData/testServerResponses.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// test data to use with msw
22

3-
const userResponse = {
3+
export const sketchResponse = {};
4+
5+
export const userResponse = {
46
57
username: 'happydog',
68
preferences: {
@@ -26,5 +28,3 @@ const userResponse = {
2628
github: 'githubusername',
2729
google: 'googleusername'
2830
};
29-
30-
export default { userResponse };

developer_docs/testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ See the [redux section](#Testing-Redux) below :)
112112
You might still want to write tests for non-component or non-redux files, such as modules with utility functions. What gets tested in this case depends a lot on the module itself, but generally, you would import the module and test the functions within it.
113113

114114
### Querying for elements
115-
Read about the recommended order of priority for queries in [the testing library docs](https://testing-library.com/docs/guide-which-query/#priority). We recommend using roles and text, or labels. You can use this [handy extension](https://chrome.google.com/webstore/detail/testing-playground/hejbmebodbijjdhflfknehhcgaklhano/related) to do this.
115+
Read about the recommended order of priority for queries in [the testing library docs](https://testing-library.com/docs/guide-which-query/#priority). We recommend using roles and text, or labels. You can use this [handy extension called Testing Playground](https://chrome.google.com/webstore/detail/testing-playground/hejbmebodbijjdhflfknehhcgaklhano/related) to do this.
116116

117117
### File structure
118118
Each test should have a top-level ``describe`` block to group related blocks together, with the name of the component under test.
@@ -439,7 +439,7 @@ Some things to consider testing:
439439
440440
## How to handle API calls in tests
441441
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).
442+
Some tests throw errors if a part of the client-side code tries to make an API call. 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).
443443
444444
There's a longer explaination of the benefits of ``msw`` in [this article by Kent C Dodds](https://kentcdodds.com/blog/stop-mocking-fetch).
445445

0 commit comments

Comments
 (0)