Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d868a3b
miraculously got something working with redux-mock-store for testing …
khanniie Feb 26, 2021
dc69ed4
synchronous tests working, async ones are not
khanniie Feb 26, 2021
4826969
remove moxios, add redux-mock-store to test redux
khanniie Mar 4, 2021
79eab16
Sketchlist and projects action creator tests
khanniie Mar 12, 2021
598095f
update documentation, rename projects.test file, fix small bug in nav…
khanniie Mar 16, 2021
4759823
Update testing.md
khanniie Mar 16, 2021
7bfebf7
Update testing.md
khanniie Mar 16, 2021
57d0fcf
Update testing.md
khanniie Mar 16, 2021
fe2d725
moved around the "what to test" suggestions
khanniie Mar 16, 2021
f57e7c1
added header
khanniie Mar 16, 2021
ab7d9c3
formatting fixes
khanniie Mar 17, 2021
f841150
Update testing.md
khanniie Mar 17, 2021
8782fbd
Update testing.md
khanniie Mar 17, 2021
8de9212
add links
khanniie Mar 17, 2021
a25c649
added description of testid
khanniie Mar 17, 2021
5368538
Update testing.md
khanniie Mar 26, 2021
88326b5
Update testing.md
khanniie Apr 1, 2021
1972a2a
Update testing.md
khanniie Apr 1, 2021
93951c2
Update testing.md
khanniie Apr 1, 2021
8439e70
fix typos, add links
khanniie Apr 1, 2021
9477cc3
remove duplicate cleanup functions
khanniie Apr 1, 2021
0cf005e
started writing tests, edited testing.md
khanniie Apr 11, 2021
9aa2350
integration test
khanniie Apr 12, 2021
0ed1c16
integration test ONE IS FAILING
khanniie Apr 12, 2021
00b02d8
Rename files, edit testing.md
khanniie Apr 12, 2021
a8299ea
small formatting fixes
khanniie Apr 12, 2021
255e4f8
rename files, fix index and editor tests
khanniie Apr 16, 2021
5060419
Delete projects.js
khanniie Apr 16, 2021
bda1507
fix path after moving folders
khanniie Apr 16, 2021
90a6f87
revert changes on Sketchlist
khanniie Apr 16, 2021
aa6ded8
reset sketchlist to last commit????
khanniie Apr 16, 2021
e8d69dd
update documentation and snapshot
khanniie Apr 16, 2021
d2619ce
checklist
khanniie Apr 16, 2021
4d02506
remove testing.md and move it to another branch
khanniie Apr 16, 2021
7175158
fix linting error
khanniie Apr 16, 2021
28ada7e
fix integeation test
khanniie Apr 16, 2021
85f2fa3
Merge develop
catarak Apr 21, 2021
beebe7e
Fix the SketchList snapshot test (maybe)
catarak Apr 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/__mocks__/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const mockAxios = jest.genMockFromModule('axios');

// this is the key to fix the axios.create() undefined error!
mockAxios.create = jest.fn(() => mockAxios);

export default mockAxios;
28 changes: 28 additions & 0 deletions client/__mocks__/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { enUS, es, ja, hi } from 'date-fns/locale';
import i18n from '../i18n-test';

export function languageKeyToLabel(lang) {
const languageMap = {
'en-US': 'English',
'es-419': 'Español',
ja: '日本語',
hi: 'हिन्दी'
};
return languageMap[lang];
}

export function languageKeyToDateLocale(lang) {
const languageMap = {
'en-US': enUS,
'es-419': es,
ja,
hi
};
return languageMap[lang];
}

export function currentDateLocale() {
return languageKeyToDateLocale(i18n.language);
}

export default i18n;
2 changes: 2 additions & 0 deletions client/components/__test__/Nav.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { render } from '@testing-library/react';

import { NavComponent } from '../Nav';

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

describe('Nav', () => {
const props = {
newProject: jest.fn(),
Expand Down
16 changes: 16 additions & 0 deletions client/modules/IDE/actions/__mocks__/projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as ActionTypes from '../../../../constants';
import { startLoader, stopLoader } from '../loader';
import { mockProjects } from '../../../../redux_test_stores/test_store';

// eslint-disable-next-line
export function getProjects(username) {
console.log(`mocked getProjects call with ${username}`);
return (dispatch) => {
dispatch(startLoader());
dispatch({
type: ActionTypes.SET_PROJECTS,
projects: mockProjects
});
dispatch(stopLoader());
};
}
40 changes: 40 additions & 0 deletions client/modules/IDE/actions/__tests__/projects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';
import { unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';
import * as ProjectActions from '../projects';
import * as ActionTypes from '../../../../constants';
import {
initialTestState,
mockProjects
} from '../../../../redux_test_stores/test_store';

const mockStore = configureStore([thunk]);

describe('projects action creator tests', () => {
let store;

afterEach(() => {
store.clearActions();
});

it('creates GET_PROJECTS after successfuly fetching projects', () => {
store = mockStore(initialTestState);

axios.get.mockImplementationOnce((x) => {
console.log('get was called with ', x);
return Promise.resolve({ data: mockProjects });
});

const expectedActions = [
{ type: ActionTypes.START_LOADING },
{ type: ActionTypes.SET_PROJECTS, projects: mockProjects },
{ type: ActionTypes.STOP_LOADING }
];

return store
.dispatch(ProjectActions.getProjects('happydog'))
.then(() => expect(store.getActions()).toEqual(expectedActions));
});
});
2 changes: 1 addition & 1 deletion client/modules/IDE/actions/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getProjects(username) {
} else {
url = '/projects';
}
apiClient
return apiClient
.get(url)
.then((response) => {
dispatch({
Expand Down
3 changes: 2 additions & 1 deletion client/modules/IDE/components/SketchList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class SketchListRowBase extends React.Component {
renderDropdown = () => {
const { optionsOpen } = this.state;
const userIsOwner = this.props.user.username === this.props.username;

return (
<td className="sketch-list__dropdown-column">
<button
Expand All @@ -188,6 +187,7 @@ class SketchListRowBase extends React.Component {
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
aria-label={this.props.t('SketchList.ToggleLabelARIA')}
data-testid="sketch-list-toggle-options-arrow"
>
<DownFilledTriangleIcon focusable="false" aria-hidden="true" />
</button>
Expand Down Expand Up @@ -449,6 +449,7 @@ class SketchList extends React.Component {
className="sketch-list__sort-button"
onClick={() => this.props.toggleDirectionForField(fieldName)}
aria-label={buttonLabel}
data-testid={`toggle-direction-${fieldName}`}
>
<span className={headerClass}>{displayName}</span>
{field === fieldName &&
Expand Down
130 changes: 130 additions & 0 deletions client/modules/IDE/components/SketchList.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';
import { unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';
import SketchList from './SketchList';
import { reduxRender, fireEvent, screen } from '../../../test-utils';
import {
initialTestState,
mockProjects
} from '../../../redux_test_stores/test_store';

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

/*
* there seem to be polarizing opinions about whether or not
* we should test the unconnected component or the
* connected one. For the sake of not editing the original SketchList file
* with an unneccessary export statement, I'm testing
* the connected component with redux-mock-store.
* this approach is outlined here -
* https://www.robinwieruch.de/react-connected-component-test
*/

describe('<Sketchlist />', () => {
let container;
const mockStore = configureStore([thunk]);
const store = mockStore(initialTestState);

beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement('div');
document.body.appendChild(container);
axios.get.mockImplementationOnce((x) => Promise.resolve({ data: 'foo' }));
});

afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
store.clearActions();
});

it('has sample projects', () => {
let component;
act(() => {
component = reduxRender(<SketchList username="happydog1" />, {
store,
container
});
});
expect(screen.getByText('testsketch1')).toBeInTheDocument();
expect(screen.getByText('testsketch2')).toBeInTheDocument();
});

it('clicking on date created row header dispatches a reordering action', () => {
let component;
act(() => {
component = reduxRender(<SketchList username="happydog2" />, {
store,
container
});
});
act(() => {
fireEvent.click(screen.getByTestId('toggle-direction-createdAt'));
});
const expectedAction = [{ type: 'TOGGLE_DIRECTION', field: 'createdAt' }];
expect(store.getActions()).toEqual(expect.arrayContaining(expectedAction));
});

it('clicking on dropdown arrow opens sketch options', () => {
let component;
act(() => {
component = reduxRender(<SketchList username="happydog2" />, {
store,
container
});
});
const dropdown = screen.queryAllByTestId(
'sketch-list-toggle-options-arrow'
);

if (dropdown.length > 0) {
act(() => {
fireEvent.click(dropdown[0]);
});

expect(screen.queryByText('Rename')).not.toBeInTheDocument();
expect(screen.queryByText('Duplicate')).toBeInTheDocument();
expect(screen.queryByText('Download')).toBeInTheDocument();
expect(screen.queryByText('Add to collection')).toBeInTheDocument();
expect(screen.queryByText('Delete')).not.toBeInTheDocument();
}
});

it('clicking on dropdown arrow opens sketch options - sketches belong to user', () => {
let component;
act(() => {
component = reduxRender(<SketchList username="happydog" />, {
store,
container
});
});
const dropdown = screen.queryAllByTestId(
'sketch-list-toggle-options-arrow'
);

if (dropdown.length > 0) {
act(() => {
fireEvent.click(dropdown[0]);
});

expect(screen.queryByText('Rename')).toBeInTheDocument();
expect(screen.queryByText('Duplicate')).toBeInTheDocument();
expect(screen.queryByText('Download')).toBeInTheDocument();
expect(screen.queryByText('Add to collection')).toBeInTheDocument();
expect(screen.queryByText('Delete')).toBeInTheDocument();
}
});

it('snapshot testing', () => {
const { asFragment } = reduxRender(<SketchList username="happydog2" />, {
store,
container
});
expect(asFragment()).toMatchSnapshot();
});
});
Loading