Skip to content

Commit dc69ed4

Browse files
committed
synchronous tests working, async ones are not
1 parent d868a3b commit dc69ed4

File tree

3 files changed

+92
-28
lines changed

3 files changed

+92
-28
lines changed

client/modules/IDE/components/SketchList.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ class SketchList extends React.Component {
449449
className="sketch-list__sort-button"
450450
onClick={() => this.props.toggleDirectionForField(fieldName)}
451451
aria-label={buttonLabel}
452+
data-testid={`toggle-direction-${fieldName}`}
452453
>
453454
<span className={headerClass}>{displayName}</span>
454455
{field === fieldName &&

client/modules/IDE/components/SketchList.test.jsx

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,33 @@ import configureStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
44
import { unmountComponentAtNode } from 'react-dom';
55
import { act } from 'react-dom/test-utils';
6-
import { reduxRender, prettyDOM } from '../../../test-utils';
6+
import moxios from 'moxios';
7+
import * as ProjectActions from '../actions/projects';
8+
import * as ActionTypes from '../../../constants';
79
import SketchList from './SketchList';
10+
import { reduxRender, fireEvent, screen } from '../../../test-utils';
811

912
const mockStore = configureStore([thunk]);
1013

14+
const mockProjects = [
15+
{
16+
name: 'testsketch1',
17+
_id: 'testid1',
18+
updatedAt: '2021-02-26T04:58:29.390Z',
19+
files: [],
20+
createdAt: '2021-02-26T04:58:14.136Z',
21+
id: 'testid1'
22+
},
23+
{
24+
name: 'testsketch2',
25+
_id: 'testid2',
26+
updatedAt: '2021-02-23T17:40:43.789Z',
27+
files: [],
28+
createdAt: '2021-02-23T17:40:43.789Z',
29+
id: 'testid2'
30+
}
31+
];
32+
1133
const initialTestState = {
1234
ide: null,
1335
files: [],
@@ -23,24 +45,7 @@ const initialTestState = {
2345
authenticated: true
2446
},
2547
project: null,
26-
sketches: [
27-
{
28-
name: 'testsketch1',
29-
_id: 'testid1',
30-
updatedAt: '2021-02-26T04:58:29.390Z',
31-
files: [],
32-
createdAt: '2021-02-26T04:58:14.136Z',
33-
id: 'testid1'
34-
},
35-
{
36-
name: 'testsketch2',
37-
_id: 'testid2',
38-
updatedAt: '2021-02-23T17:40:43.789Z',
39-
files: [],
40-
createdAt: '2021-02-23T17:40:43.789Z',
41-
id: 'testid2'
42-
}
43-
],
48+
sketches: mockProjects,
4449
search: {
4550
collectionSearchTerm: '',
4651
sketchSearchTerm: ''
@@ -64,24 +69,81 @@ describe('<Sketchlist />', () => {
6469
// setup a DOM element as a render target
6570
container = document.createElement('div');
6671
document.body.appendChild(container);
67-
store = mockStore(initialTestState);
6872
});
6973

7074
afterEach(() => {
7175
// cleanup on exiting
7276
unmountComponentAtNode(container);
7377
container.remove();
7478
container = null;
79+
store.clearActions();
7580
});
7681

77-
describe('<SketchListRow />', () => {
78-
it('render', () => {
79-
let component;
80-
// render the component with autosave set to false as default
81-
act(() => {
82-
component = reduxRender(<SketchList />, { store, container });
83-
});
84-
console.log(prettyDOM(container));
82+
// it('creates GET_PROJECTS after successfuly fetching projects', async () => {
83+
// moxios.install();
84+
// function resolveAfter2Seconds() {
85+
// console.log("called resolve");
86+
// return new Promise(resolve => {
87+
// setTimeout(() => {
88+
// resolve('resolved');
89+
// }, 5000);
90+
// });
91+
// }
92+
// console.log("moxios", moxios);
93+
// moxios.wait(() => {
94+
95+
// const request = moxios.requests.mostRecent();
96+
// console.log(moxios.requests, request)
97+
// console.log("recieved request for get projects")
98+
// request.respondWith({
99+
// status: 200,
100+
// response: mockProjects,
101+
// }).then(() => done());
102+
// });
103+
104+
// const expectedActions = [
105+
// {type: ActionTypes.START_LOADING},
106+
// { type: ActionTypes.SET_PROJECTS,
107+
// projects: mockProjects }
108+
// ];
109+
110+
// store = mockStore(initialTestState);
111+
// console.log("dispatching action");
112+
// //store.dispatch(ProjectActions.getProjects("happydog"))
113+
// act(() => {
114+
// reduxRender(<SketchList />, { store, container });
115+
// });
116+
117+
// const hasSetProjectKey = (currActions) => {
118+
// return currActions.filter(ac => ac.type === ActionTypes.SET_PROJECTS).length > 0;
119+
// }
120+
121+
// await waitFor(() => expect(hasSetProjectKey(store.getActions())).toEqual(true));
122+
// moxios.uninstall();
123+
// //expect(store.getActions()).toEqual(expectedActions);
124+
// //return resolveAfter2Seconds().then(res => console.log("resolved"))
125+
// });
126+
127+
it('has both of the sample projects', () => {
128+
store = mockStore(initialTestState);
129+
let component;
130+
act(() => {
131+
component = reduxRender(<SketchList />, { store, container });
132+
});
133+
expect(screen.getByText('testsketch1')).toBeInTheDocument();
134+
expect(screen.getByText('testsketch2')).toBeInTheDocument();
135+
});
136+
137+
it('clicking on date created row header dispatches a reordering action', () => {
138+
store = mockStore(initialTestState);
139+
let component;
140+
act(() => {
141+
component = reduxRender(<SketchList />, { store, container });
142+
});
143+
act(() => {
144+
fireEvent.click(screen.getByTestId('toggle-direction-createdAt'));
85145
});
146+
const expectedAction = [{ type: 'TOGGLE_DIRECTION', field: 'createdAt' }];
147+
expect(store.getActions()).toEqual(expect.arrayContaining(expectedAction));
86148
});
87149
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"jest": "^26.0.1",
116116
"lint-staged": "^10.1.3",
117117
"mini-css-extract-plugin": "^1.3.4",
118+
"moxios": "^0.4.0",
118119
"node-sass": "^5.0.0",
119120
"nodemon": "^2.0.7",
120121
"optimize-css-assets-webpack-plugin": "^5.0.3",

0 commit comments

Comments
 (0)