Skip to content

Commit

Permalink
Incremental UI Tests #3 (#82)
Browse files Browse the repository at this point in the history
* App tests

* Add scaffolding for useIsMounted

* Ignore .js, .jsx, and other CRA files

* Action creator tests

* renderCommaSepList tests

* removeMiddleAuthorsTest

* shortenAuthors test

* shortenTableString

* getTagColor test

* isDOI test

* markdown test

* shhhh is okay

* self review feedback

* DraftsRedux test

* Woohoo! A Redux test

* finish drafts tests

Co-authored-by: Eshed Margalit <eshedmargalit@users.noreply.github.com>
  • Loading branch information
aradmargalit and eshedmargalit authored Jan 10, 2021
1 parent 730b110 commit f4e8fd6
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 21 deletions.
4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cognitive-services": "^1.2.3",
"formik": "^2.2.3",
"fuse.js": "^3.4.5",
"history": "^5.0.0",
"history": "^4.0.0",
"http-proxy-middleware": "^0.19.1",
"katex": "^0.12.0",
"lodash": "^4.17.20",
Expand Down Expand Up @@ -54,7 +54,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "DEBUG_PRINT_LIMIT=100000 react-scripts test",
"eject": "react-scripts eject",
"test:ci": "CI=true yarn test --coverage --colors"
},
Expand Down
9 changes: 9 additions & 0 deletions client/src/components/DraftPage/DraftsPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import DraftPageRedux from '.';
import { renderWithRouterRedux } from '../../testUtils/reduxRender';

describe('<DraftsPage />', () => {
it('renders without crashing', () => {
renderWithRouterRedux(<DraftPageRedux />);
});
});
4 changes: 2 additions & 2 deletions client/src/components/Drafts/Drafts-redux.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function DraftsRedux(): JSX.Element {
const dispatch = useDispatch();
const drafts: Review[] = useSelector((state: RootState) => state.drafts);
const renderMath: boolean = useSelector((state: RootState) => state.user.renderMath);
const { back } = useHistory();
const { goBack } = useHistory();

// function to delete the specified draft
const deleteDraft = (draftToDelete: Review) => {
Expand All @@ -29,7 +29,7 @@ export default function DraftsRedux(): JSX.Element {

const pageHeaderProps = {
title: 'Your Drafts',
onBack: () => back(),
onBack: goBack,
};

return (
Expand Down
89 changes: 89 additions & 0 deletions client/src/components/Drafts/Drafts.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable no-return-assign */
/* eslint-disable no-console */
import { screen, waitFor } from '@testing-library/react';
import React from 'react';
import userEvent from '@testing-library/user-event';
import { RootState } from '../../reducers';
import { blankNotes, blankReview } from '../../templates';
import { getBlankInitialState, renderWithRouterRedux } from '../../testUtils/reduxRender';
import DraftsRedux from './Drafts-redux';

const mockGoBack = jest.fn();

jest.mock('react-router-dom', () => ({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(jest.requireActual('react-router-dom') as any),
useHistory: () => ({
goBack: mockGoBack,
}),
}));

const initialStateWithDrafts: RootState = {
...getBlankInitialState(),
drafts: [{ ...blankReview, _id: 'id', notes: { ...blankNotes, tldr: 'it was nice' } }],
};

describe('<Drafts />', () => {
describe('on back', () => {
it('goes back to the previous page', () => {
renderWithRouterRedux(<DraftsRedux />);
const backButton = screen.getByLabelText('Back');
userEvent.click(backButton);
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
});

describe('drafts table', () => {
// This is silly, but the react-katex library throws an ugly warning: https://github.com/talyssonoc/react-katex/issues/59
// so we'll just suppress it :)
const originalConsoleWarn = console.warn;
beforeAll(() => (console.warn = jest.fn()));
afterAll(() => (console.warn = originalConsoleWarn));

it('renders without crashing', () => {
renderWithRouterRedux(<DraftsRedux />);
});

it('renders the draft information in the table', () => {
renderWithRouterRedux(<DraftsRedux />, { initialState: initialStateWithDrafts });
expect(screen.getByText(/it was nice/)).toBeDefined();
});

it('displays more options and actions when user clicks on a draft', () => {
renderWithRouterRedux(<DraftsRedux />, { initialState: initialStateWithDrafts });
const draftRow = screen.getByText(/it was nice/);
userEvent.click(draftRow);
expect(screen.getByText(/Delete this Draft/)).toBeDefined();
});

it('deletes a draft from Redux when the delete button is clicked', async () => {
renderWithRouterRedux(<DraftsRedux />, { initialState: initialStateWithDrafts });

// Find the draft's row and click on it.
userEvent.click(screen.getByText(/it was nice/));

// Now that the modal is open, find and click the delete button
userEvent.click(screen.getByText(/Delete this Draft/));

// Wait for antd to render the confirm modal, then click Yes
await waitFor(() => screen.getByText('Yes'));
userEvent.click(screen.getByText('Yes'));

// Wait for the async logic to complete and for the draft to disappear
await waitFor(() => expect(screen.queryByText(/it was nice/)).toBeNull());
});

it('redirects the user to the edit form when the edit button is clicked', async () => {
renderWithRouterRedux(<DraftsRedux />, { initialState: initialStateWithDrafts, redirectTo: '/form' });

// Find the draft's row and click on it.
userEvent.click(screen.getByText(/it was nice/));

// Now that the modal is open, find and click the edit button
userEvent.click(screen.getByText(/Edit this Draft/));

// Confirm that we navigate to the "form" after clicking edit
expect(screen.getByText(/Redirected to a new page/)).toBeDefined();
});
});
});
9 changes: 8 additions & 1 deletion client/src/components/ReviewModal/ReviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ export default function ReviewModal(props: ReviewModalProps): JSX.Element {

return (
<div>
<Modal title={titleDiv} visible={props.visible} onCancel={props.onClose} footer={props.footer} width="80%">
<Modal
title={titleDiv}
visible={props.visible}
onCancel={props.onClose}
footer={props.footer}
destroyOnClose
width="80%"
>
<div>
{renderCommaSepList(paper.authors)}
{paper.institutions ? renderCommaSepList(paper.institutions) : null}
Expand Down
1 change: 0 additions & 1 deletion client/src/components/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe('utils', () => {
expectedText: 'N/A',
},
{
// TODO EM: is this desired?
description: 'with a string and a negative cutoff',
inString: 'testing one two',
cutoff: -5,
Expand Down
5 changes: 4 additions & 1 deletion client/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { rest } from 'msw';
import { blankUser } from '../templates';

export const handlers = [
rest.get('/api/current_user', (req, res, ctx) => {
rest.get('/api/current_user', (_req, res, ctx) => {
// Return a user
return res(ctx.status(200), ctx.json(blankUser));
}),
rest.delete('/api/drafts/:draftId', (_req, res, ctx) => {
return res(ctx.status(200));
}),
];
14 changes: 9 additions & 5 deletions client/src/testUtils/reduxRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import { render, RenderResult } from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter, Route } from 'react-router-dom';
import { MemoryRouter, Route, Switch } from 'react-router-dom';
import { applyMiddleware, createStore, Store } from 'redux';
import thunk from 'redux-thunk';
import rootReducer, { RootState } from '../reducers';
Expand Down Expand Up @@ -30,10 +30,14 @@ export function renderWithRouterRedux(
return {
...render(
<MemoryRouter initialEntries={['/starting']}>
<Route path="/starting">
<Provider store={store}>{ui}</Provider>
</Route>
<Route path={redirectTo}>Redirected to a new page.</Route>
<Switch>
<Route exact path="/starting">
<Provider store={store}>{ui}</Provider>
</Route>
<Route exact path={redirectTo || '/redirected'}>
Redirected to a new page.
</Route>
</Switch>
</MemoryRouter>
),
store,
Expand Down
11 changes: 2 additions & 9 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.1", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.4", "@babel/runtime@^7.10.5", "@babel/runtime@^7.11.1", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.2.0", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.6":
"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.1", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.4", "@babel/runtime@^7.10.5", "@babel/runtime@^7.11.1", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.2.0", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.6":
version "7.12.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.5.tgz#410e7e487441e1b360c29be715d870d9b985882e"
integrity sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==
Expand Down Expand Up @@ -6745,7 +6745,7 @@ hex-color-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==

history@^4.9.0:
history@^4.0.0, history@^4.9.0:
version "4.10.1"
resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3"
integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==
Expand All @@ -6757,13 +6757,6 @@ history@^4.9.0:
tiny-warning "^1.0.0"
value-equal "^1.0.1"

history@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/history/-/history-5.0.0.tgz#0cabbb6c4bbf835addb874f8259f6d25101efd08"
integrity sha512-3NyRMKIiFSJmIPdq7FxkNMJkQ7ZEtVblOQ38VtKaA0zZMW1Eo6Q6W8oDKEflr1kNNTItSnk4JMCO1deeSgbLLg==
dependencies:
"@babel/runtime" "^7.7.6"

hmac-drbg@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
Expand Down

0 comments on commit f4e8fd6

Please sign in to comment.