Skip to content

Commit

Permalink
DraftsRedux test
Browse files Browse the repository at this point in the history
  • Loading branch information
aradmargalit committed Jan 3, 2021
1 parent ffb345b commit 037ad53
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion 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
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
60 changes: 60 additions & 0 deletions client/src/components/Drafts/Drafts.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable no-return-assign */
/* eslint-disable no-console */
import { screen } 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', () => {
// eslint-disable-next-line react/react-in-jsx-scope
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();
});
});
});
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 037ad53

Please sign in to comment.