Skip to content

Commit

Permalink
#17 Reuse test object constants
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Nov 18, 2020
1 parent aa9c012 commit 695fc8f
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 164 deletions.
14 changes: 3 additions & 11 deletions src/__mocks__/next-auth/client.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { userSession } from '../../utils/testUtil';

const client = jest.genMockFromModule('next-auth/client');

const useSession = jest.fn(() => [
{
user: {
name: 'Test User',
email: '[email protected]',
groups: [{
id: 'abcd1234', name: 'Test Group', ownerName: 'Test User', memberCount: 2, role: 'owner',
}],
role: 'user',
},
expires: '2881-10-05T14:48:00.000',
},
userSession,
false]);

client.useSession = useSession;
Expand Down
13 changes: 1 addition & 12 deletions src/__tests__/groups/[id]/edit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { render, wait } from '@testing-library/react';
import EditGroup from '../../../pages/groups/[id]/edit';
import { group } from '../../../utils/testUtil';


jest.mock('next/router', () => ({
Expand All @@ -17,18 +18,6 @@ jest.mock('next/router', () => ({
}));

describe('Group Edit Page', () => {
const group = {
id: 'abcd1234',
name: 'Test Group',
members: [{
id: '1',
email: '[email protected]',
name: 'Test User',
role: 'owner',
}],
inviteUrl: '',
};

it('renders group card', async () => {
const { getByTestId } = render(<EditGroup group={group} />);
const cardBody = getByTestId('groupedit-card-body');
Expand Down
12 changes: 1 addition & 11 deletions src/__tests__/groups/[id]/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { render, wait } from '@testing-library/react';
import ViewGroup from '../../../pages/groups/[id]/index';
import { group } from '../../../utils/testUtil';

jest.mock('next/router', () => ({
useRouter() {
Expand All @@ -16,17 +17,6 @@ jest.mock('next/router', () => ({
}));

describe('Group View Page', () => {
const group = {
id: 'abcd1234',
name: 'Test Group',
members: [{
id: '1',
email: '[email protected]',
name: 'Test User',
role: 'owner',
}],
};

it('renders group card', async () => {
const { getByTestId } = render(<ViewGroup group={group} />);
const cardBody = getByTestId('groupview-card-body');
Expand Down
28 changes: 4 additions & 24 deletions src/__tests__/user/editprofile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { render, wait } from '@testing-library/react';
import EditProfile from '../../pages/user/[slug]/editprofile';
import { user } from '../../utils/testUtil';

jest.mock('next/router', () => ({
useRouter() {
Expand All @@ -16,44 +17,23 @@ jest.mock('next/router', () => ({
}));

test('renders edit profile card', async () => {
const { getAllByText } = render(<EditProfile user={{
name: 'Test User',
firstName: 'Test',
lastName: 'User',
email: '[email protected]',
affiliation: 'Jest Tests',
}}
/>);
const { getAllByText } = render(<EditProfile user={user} />);
const textElements = getAllByText(/Edit Profile/);
await wait(() => {
expect(textElements[1]).toBeInTheDocument();
});
});

test('renders edit profile form', async () => {
const { getAllByRole } = render(<EditProfile user={{
name: 'Test User',
firstName: 'Test',
lastName: 'User',
email: '[email protected]',
affiliation: 'Jest Tests',
}}
/>);
const { getAllByRole } = render(<EditProfile user={user} />);
const textboxElements = getAllByRole('textbox');
await wait(() => {
expect(textboxElements).toHaveLength(4);
});
});

test('renders submit button', async () => {
const { getByTestId } = render(<EditProfile user={{
name: 'Test User',
firstName: 'Test',
lastName: 'User',
email: '[email protected]',
affiliation: 'Jest Tests',
}}
/>);
const { getByTestId } = render(<EditProfile user={user} />);
const submitButton = getByTestId('editprofile-submit-button');
await wait(() => {
expect(submitButton).toBeInTheDocument();
Expand Down
13 changes: 1 addition & 12 deletions src/components/Admin/AdminPanel/AdminPanel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@

import { render } from '@testing-library/react';
import AdminPanel from './AdminPanel';

const adminUserSession = {
user: {
name: 'Admin User',
email: '[email protected]',
groups: [{
id: 'abcd1234', name: 'Test Group', ownerName: 'Test User', memberCount: 2, role: 'owner',
}],
role: 'admin',
},
expires: '2881-10-05T14:48:00.000',
};
import { adminUserSession } from '../../../utils/testUtil';

test('renders admin panel', async () => {
const { getByTestId } = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@

import { render } from '@testing-library/react';
import AdminDocumentTable from './AdminDocumentTable';

const document = {
_id: 'documenttestid',
title: 'test',
state: 'draft',
contributors: [],
createdAt: '2881-10-05T14:48:00.000',
updatedAt: '2881-10-05T14:48:00.000',
owner: 'testestestest',
groups: [],
};
import { document } from '../../../../utils/testUtil';

test('renders admin doc view', async () => {
const { getByTestId } = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@

import { render } from '@testing-library/react';
import AdminGroupTable from './AdminGroupTable';

const group = {
id: 'abcd1234',
name: 'Test Group',
members: [{
id: '1',
email: '[email protected]',
name: 'Test User',
role: 'owner',
}],
createdAt: '2881-10-05T14:48:00.000',
updatedAt: '2881-10-05T14:48:00.000',
};
import { group } from '../../../../utils/testUtil';

test('renders admin group view', async () => {
const { getByTestId } = render(
Expand Down
24 changes: 2 additions & 22 deletions src/components/DocumentForm/DocumentForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,10 @@

import { render } from '@testing-library/react';
import DocumentForm from './DocumentForm';
import { document, userSession } from '../../utils/testUtil';

// Mock session
const session = {
user: {
id: 'testestestest',
name: 'Test User',
email: '[email protected]',
groups: [{
id: 'abcd1234', name: 'Test Group', ownerName: 'Test User', memberCount: 2, role: 'owner',
}],
},
expires: '2881-10-05T14:48:00.000',
};

// Mock document
const document = {
_id: 'documenttestid',
title: 'test',
state: 'draft',
contributors: [],
createdAt: '2881-10-05T14:48:00.000',
owner: 'testestestest',
groups: [],
};
const session = userSession;

describe('document form', () => {
test('renders with mode = new', async () => {
Expand Down
12 changes: 1 addition & 11 deletions src/components/DocumentList/DocumentList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@

import { render } from '@testing-library/react';
import DocumentList from './DocumentList';

// Mock document
const document = {
_id: 'documenttestid',
title: 'test',
contributors: [],
createdAt: '2881-10-05T14:48:00.000',
state: 'draft',
owner: 'testestestest',
groups: [],
};
import { document } from '../../utils/testUtil';

describe('document list', () => {
test('renders with a mocked document', async () => {
Expand Down
16 changes: 3 additions & 13 deletions src/components/DocumentMetadata/DocumentMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
import { render } from '@testing-library/react';
import { Formik } from 'formik';
import DocumentMetadata from './DocumentMetadata';

// Mock document
const values = {
_id: 'documenttestid',
title: 'test',
contributors: [],
createdAt: '2881-10-05T14:48:00.000',
state: 'draft',
owner: 'testestestest',
groups: [],
};
import { document } from '../../utils/testUtil';


describe('document metadata form', () => {
Expand All @@ -26,11 +16,11 @@ describe('document metadata form', () => {
<form onSubmit={props.handleSubmit}>
<DocumentMetadata
resourceType="Book"
values={values}
values={document}
handleChange={props.handleChange}
handleBlur={props.handleBlur}
errors={[]}
touched={values}
touched={document}
/>
</form>
)}
Expand Down
27 changes: 3 additions & 24 deletions src/components/DocumentStatusSelect/DocumentStatusSelect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@
import { render } from '@testing-library/react';
import { Formik } from 'formik';
import DocumentStatusSelect from './DocumentStatusSelect';

// Mock document
const values = {
_id: 'documenttestid',
title: 'test',
contributors: [],
createdAt: '2881-10-05T14:48:00.000',
state: 'draft',
owner: 'testestestest',
groups: [],
};

import { document, userSession } from '../../utils/testUtil';

describe('document status select', () => {
test('renders with jest context and mocked values', async () => {
Expand All @@ -25,20 +14,10 @@ describe('document status select', () => {
{(props) => (
<form onSubmit={props.handleSubmit}>
<DocumentStatusSelect
values={values}
values={document}
onChange={props.handleChange}
onBlur={props.handleBlur}
session={{
user: {
name: 'Test User',
email: '[email protected]',
groups: [{
id: 'abcd1234', name: 'Test Group', ownerName: 'Test User', memberCount: 2, role: 'owner',
}],
role: 'user',
},
expires: '2881-10-05T14:48:00.000',
}}
session={userSession}
/>
</form>
)}
Expand Down
Loading

0 comments on commit 695fc8f

Please sign in to comment.