Skip to content

Creates Index.integration.test.jsx and Sketchlist.unit.test.jsx #1780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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;
1 change: 1 addition & 0 deletions client/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
2 changes: 2 additions & 0 deletions client/components/Nav.unit.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
175 changes: 175 additions & 0 deletions client/index.integration.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import React from 'react';
import { Router, browserHistory } from 'react-router';

import {
reduxRender,
act,
waitFor,
fireEvent,
screen,
within
} from './test-utils';
import configureStore from './store';
import routes from './routes';
import * as Actions from './modules/User/actions';
import { userResponse } from './testData/testServerResponses';

// setup for the app
const history = browserHistory;
const initialState = window.__INITIAL_STATE__;
const store = configureStore(initialState);

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

// setup for the msw fake server
const server = setupServer(
rest.get('/session', (req, res, ctx) =>
// console.log("called server get session");
res(ctx.json(userResponse))
)
);

beforeAll(() =>
server.listen({
onUnhandledRequest: 'warn'
})
);
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

// below are fixes for jsdom-specific errors
// https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t
const noop = () => {};
Object.defineProperty(window, 'focus', { value: noop, writable: true });

// https://github.com/jsdom/jsdom/issues/3002
document.createRange = () => {
const range = new Range();

range.getBoundingClientRect = jest.fn();

range.getClientRects = () => ({
item: () => null,
length: 0,
[Symbol.iterator]: jest.fn()
});

return range;
};

// start testing
describe('index.jsx integration', () => {
// the subject under test
const subject = () =>
reduxRender(<Router history={history} routes={routes(store)} />, { store });

// spy on this function and wait for it to be called before making assertions
const spy = jest.spyOn(Actions, 'getUser');

beforeEach(async () => {
act(() => {
subject();
});

await waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
});

afterEach(() => {
spy.mockClear();
});

it('navbar items and the dropdowns in the navbar exist', () => {
const navigation = screen.getByRole('navigation');
expect(navigation).toBeInTheDocument();

const fileButton = within(navigation).getByRole('button', {
name: /^file$/i
});
expect(fileButton).toBeInTheDocument();

const newFileButton = within(navigation).getByRole('button', {
name: /^new$/i
});
expect(newFileButton).toBeInTheDocument();

// save file button not shown?

// const exampleFileButton = within(navigation).getByRole('link', {name: /^examples$/i});
// expect(exampleFileButton).toBeInTheDocument();

const editButton = within(navigation).getByRole('button', {
name: /^edit$/i
});
expect(editButton).toBeInTheDocument();

const sketchButton = within(navigation).getByRole('button', {
name: /^sketch$/i
});
expect(sketchButton).toBeInTheDocument();

const helpButton = within(navigation).getByRole('button', {
name: /^help$/i
});
expect(helpButton).toBeInTheDocument();
});

it('toolbar elements exist', () => {
const playButton = screen.getByRole('button', {
name: /play only visual sketch/i
});
expect(playButton).toBeInTheDocument();

const stopButton = screen.getByRole('button', {
name: /stop sketch/i
});
expect(stopButton).toBeInTheDocument();

const editSketchNameButton = screen.getByRole('button', {
name: /edit sketch name/i
});
expect(editSketchNameButton).toBeInTheDocument();

expect(screen.getByText('Auto-refresh')).toBeInTheDocument();
});

it('preview exists', () => {
expect(
screen.getByRole('heading', { name: /preview/i })
).toBeInTheDocument();
const preview = screen.getByRole('main', { name: /sketch output/i });
expect(preview).toBeInTheDocument();
});

it('code editor exists', () => {
const codeeditor = screen.getByRole('article');
expect(codeeditor).toBeInTheDocument();
});

it('sidebar exists', () => {
expect(screen.getByText('Sketch Files')).toBeInTheDocument();
});

it('clicking on play updates the preview iframe with a srcdoc, stop clears it', () => {
const playButton = screen.getByRole('button', {
name: /play only visual sketch/i
});
const preview = screen.getByRole('main', { name: /sketch output/i });
expect(preview.getAttribute('srcdoc')).toBeFalsy();
act(() => {
fireEvent.click(playButton);
});

expect(preview.getAttribute('srcdoc')).toBeTruthy();

const stopButton = screen.getByRole('button', {
name: /stop sketch/i
});
act(() => {
fireEvent.click(stopButton);
});
expect(preview.getAttribute('srcdoc')).toMatch(/(^|")\s*($|")/);
});
});
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
45 changes: 45 additions & 0 deletions client/modules/IDE/actions/projects.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { setupServer } from 'msw/node';
import { rest } from 'msw';

import * as ProjectActions from './projects';
import * as ActionTypes from '../../../constants';
import {
initialTestState,
mockProjects
} from '../../../testData/testReduxStore';

const mockStore = configureStore([thunk]);

const server = setupServer(
rest.get(`/${initialTestState.user.username}/projects`, (req, res, ctx) =>
res(ctx.json(mockProjects))
)
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

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

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

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

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));
});
});
28 changes: 28 additions & 0 deletions client/modules/IDE/components/Editor.unit.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { act } from 'react-dom/test-utils';
import Editor from './Editor';
import { reduxRender } from '../../../test-utils';
import { initialTestState } from '../../../testData/testReduxStore';

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

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

const subjectProps = { provideController: jest.fn() };

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

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

it('renders successfully', () => {
act(() => {
subject();
});
});
});
Loading