Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikmonsen committed Jan 10, 2025
1 parent 26afb67 commit c424a4c
Show file tree
Hide file tree
Showing 13 changed files with 2,643 additions and 1,823 deletions.
9 changes: 5 additions & 4 deletions __tests__/app/[id]/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import Page from '@/app/[id]/page';
import {fetchNewspaperTitleFromCatalog, getLinkToNewspaperInCatalog} from '@/services/catalog.data';
import {MockBox1, MockCatalogTitle1, MockNewspaper1, MockTitle} from '../../mockdata';
import {getBoxForTitle, getLocalTitle, getNewspapersForBoxOnTitle} from '@/services/local.data';
import {MockBox1, MockCatalogTitle1, MockContactEmail1, MockContactPhone1, MockNewspaper1, MockTitle} from '../../mockdata';
import {getBoxForTitle, getContactInfoForTitle, getLocalTitle, getNewspapersForBoxOnTitle} from '@/services/local.data';
import {NotFoundError} from '@/models/Errors';

beforeEach(() => {
Expand All @@ -12,6 +12,7 @@ beforeEach(() => {
vi.mocked(getLocalTitle).mockImplementation(() => Promise.resolve(MockTitle));
vi.mocked(getBoxForTitle).mockImplementation(() => Promise.resolve(MockBox1));
vi.mocked(getNewspapersForBoxOnTitle).mockImplementation(() => Promise.resolve([MockNewspaper1]));
vi.mocked(getContactInfoForTitle).mockImplementation(() => Promise.resolve([MockContactEmail1, MockContactPhone1]));
render(<Page params={{id: ''}}/>);
});

Expand All @@ -31,8 +32,8 @@ test('Title page displays shelf', async () => {
test('Title page displays contact info', async () => {
await screen.findByText('Kontaktinformasjon:');
expect(screen.findByText(MockTitle.contact_name!)).toBeTruthy();
expect(screen.findByText(MockTitle.contact_email!)).toBeTruthy();
expect(screen.findByText(MockTitle.contact_phone!)).toBeTruthy();
expect(screen.findByText(MockContactEmail1.contact_value)).toBeTruthy();
expect(screen.findByText(MockContactPhone1.contact_value)).toBeTruthy();
expect(screen.findByText(MockTitle.vendor!)).toBeTruthy();
});

Expand Down
73 changes: 0 additions & 73 deletions __tests__/components/ContactAndReleaseInfo.test.tsx

This file was deleted.

101 changes: 101 additions & 0 deletions __tests__/components/ContactInformation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {beforeEach, expect, test} from 'vitest';
import {render, screen} from '@testing-library/react';
import {MockContactEmail1, MockContactPhone1, MockTitle} from '../mockdata';
import ContactInformation from '@/components/ContactInformation';

beforeEach(() => {
render(
<ContactInformation
vendor={MockTitle.vendor}
contactName={MockTitle.contact_name}
contactInformation={[MockContactEmail1, MockContactPhone1]}
/>);
});

test('ContactInformation should render vendor', () => {
expect(screen.getByText('Avleverer:')).toBeTruthy();
expect(screen.getByText(MockTitle.vendor!)).toBeTruthy();
});

test('ContactInformation should render contact name', () => {
expect(screen.getByText('Kontaktperson:')).toBeTruthy();
expect(screen.getByText(MockTitle.contact_name!)).toBeTruthy();
});

test('ContactInformation should render phone numbers', () => {
expect(screen.getByText('Telefonnummer')).toBeTruthy();
expect(screen.getByText(MockContactPhone1.contact_value)).toBeTruthy();
});

test('ContactInformation should render emails', () => {
expect(screen.getByText('E-postadresse')).toBeTruthy();
expect(screen.getByText(MockContactEmail1.contact_value)).toBeTruthy();
});

// test('Title page contact and release does not open in edit mode', async () => {
// await screen.findByText('Legg til telefon');
//
// expect(screen.getByText('Kontaktinformasjon', {exact: false})).toBeTruthy();
// expect(screen.getByText('Avleverer:', {exact: false})).toBeTruthy();
// expect(screen.getByText('Kontaktperson:', {exact: false})).toBeTruthy();
// expect(screen.getByText('E-post:', {exact: false})).toBeTruthy();
// expect(screen.getByText('Telefon:', {exact: false})).toBeTruthy();
//
// expect(screen.queryByRole('textbox', {name: 'Avleverer'})).toBeNull();
// expect(screen.queryByRole('textbox', {name: 'Navn'})).toBeNull();
// expect(screen.queryByRole('textbox', {name: 'E-post'})).toBeNull();
// expect(screen.queryByRole('textbox', {name: 'Telefon'})).toBeNull();
// });
//
// test('Title page contact and release can be edited after button press', async () => {
// screen.getByRole('button', {name: 'Rediger'}).click();
// await vi.waitFor(() => screen.getByRole('textbox', {name: 'Avleverer'}));
//
// expect(screen.getByRole('textbox', {name: 'Avleverer'})).toBeTruthy();
// expect(screen.getByRole('textbox', {name: 'Navn'})).toBeTruthy();
// expect(screen.getByRole('textbox', {name: 'E-post'})).toBeTruthy();
// expect(screen.getByRole('textbox', {name: 'Telefon'})).toBeTruthy();
// });
//
// test('Title page contact and release shows success on successful button press', async () => {
// screen.getByRole('button', {name: 'Rediger'}).click();
// await vi.waitFor(() => screen.getByRole('textbox', {name: 'Avleverer'}));
//
// screen.getByRole('button', {name: 'Lagre'}).click();
// await vi.waitFor(() => screen.getByText('Kontakt- og utgivelsesinformasjon lagret!'));
// });
//
// test('Title page contact and release should have release pattern', async () => {
// expect(screen.getByText('Mandag:')).toBeTruthy();
// expect(screen.getByText('Tirsdag:')).toBeTruthy();
// expect(screen.getByText('Onsdag:')).toBeTruthy();
// expect(screen.getByText('Torsdag:')).toBeTruthy();
// expect(screen.getByText('Fredag:')).toBeTruthy();
// expect(screen.getByText('Lørdag:')).toBeTruthy();
// expect(screen.getByText('Søndag:')).toBeTruthy();
//
// screen.getByRole('button', {name: 'Rediger'}).click();
// await vi.waitFor(() => screen.getByRole('heading', {name: 'Utgivelsesmønster:'}));
// expect(screen.getAllByRole('gridcell', {name: '1'})).toBeTruthy();
// expect(screen.getAllByRole('gridcell', {name: '0'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Mandag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Tirsdag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Onsdag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Torsdag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Fredag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Lørdag'})).toBeTruthy();
// expect(screen.getByRole('rowheader', {name: 'Søndag'})).toBeTruthy();
// });
//
// test('Title page contact and release should show error message on unsuccessful submit', async () => {
// cleanup();
// render(<Page params={{id: MockTitle.id.toString()}} />);
// // Mock rejected promise
// vi.mocked(putLocalTitle).mockImplementation(() => Promise.reject(new Error('Noe gikk galt ved lagring')));
//
// screen.getByRole('button', {name: 'Rediger'}).click();
// await vi.waitFor(() => screen.getByRole('textbox', {name: 'Avleverer'}));
//
// screen.getByRole('button', {name: 'Lagre'}).click();
// await vi.waitFor(() => screen.getByText('Noe gikk galt ved lagring', {exact: false}));
// });
61 changes: 61 additions & 0 deletions __tests__/components/ContactInformationForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import ContactInformationForm from '@/components/ContactInformationForm';
import {MockContactEmail1, MockContactPhone1, MockTitle} from '../mockdata';
import {Form, Formik} from 'formik';
import {TitleContactInfo} from '@/models/TitleContactInfo';

beforeEach(() => {
const titleContact: TitleContactInfo = {
title: MockTitle,
contactInfo: [MockContactEmail1, MockContactPhone1]
};
render(
<Formik
enableReinitialize
initialValues={titleContact}
onSubmit={() => {}}
>
{({
values,
handleChange,
handleBlur,
}) => (
<Form>
<ContactInformationForm
values={values}
handleChange={handleChange}
handleBlur={handleBlur}
handleAdd={vi.fn()}
handleRemove={vi.fn()}
/>
</Form>
)}
</Formik>
);
});

test('ContactInformation should render vendor', () => {
expect(screen.getByText('Avleverer')).toBeTruthy();
expect(screen.getByDisplayValue(MockTitle.vendor!)).toBeTruthy();
});

test('ContactInformation should render contact name', () => {
expect(screen.getByText('Navn')).toBeTruthy();
expect(screen.getByDisplayValue(MockTitle.contact_name!)).toBeTruthy();
});

test('ContactInformation should render phone numbers', () => {
expect(screen.getByText('Telefon')).toBeTruthy();
expect(screen.getByDisplayValue(MockContactPhone1.contact_value)).toBeTruthy();
});

test('ContactInformation should render emails', () => {
expect(screen.getByText('E-post')).toBeTruthy();
expect(screen.getByDisplayValue(MockContactEmail1.contact_value)).toBeTruthy();
});

test('ContactInformation should render add buttons', () => {
expect(screen.getByRole('button', {name: '+ Legg til telefon'})).toBeTruthy();
expect(screen.getByRole('button', {name: '+ Legg til e-post'})).toBeTruthy();
});
21 changes: 21 additions & 0 deletions __tests__/components/ReleasePattern.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import {MockTitle} from '../mockdata';
import ReleasePattern from '@/components/ReleasePattern';

beforeEach(() => {
render(<ReleasePattern releasePattern={MockTitle.release_pattern}/>);
});

test('ReleasePattern should render', async () => {
await vi.waitFor(() => expect(screen.getByRole('table')).toBeTruthy());

expect(screen.getByText('Utgivelsesmønster:')).toBeTruthy();
expect(screen.getByText('Mandag:')).toBeTruthy();
expect(screen.getByText('Tirsdag:')).toBeTruthy();
expect(screen.getByText('Onsdag:')).toBeTruthy();
expect(screen.getByText('Torsdag:')).toBeTruthy();
expect(screen.getByText('Fredag:')).toBeTruthy();
expect(screen.getByText('Lørdag:')).toBeTruthy();
expect(screen.getByText('Søndag:')).toBeTruthy();
});
47 changes: 47 additions & 0 deletions __tests__/components/ReleasePatternForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import {MockTitle} from '../mockdata';
import {Form, Formik} from 'formik';
import ReleasePatternForm from '@/components/ReleasePatternForm';

beforeEach(() => {
render(
<Formik
enableReinitialize
initialValues={MockTitle.release_pattern}
onSubmit={() => {}}
>
{({
values,
handleChange,
handleBlur,
}) => (
<Form>
<ReleasePatternForm
releasePattern={values}
handleChange={handleChange}
handleBlur={handleBlur}
/>
</Form>
)}
</Formik>
);
});

test('ReleasePatternForm should render all day labels', () => {
expect(screen.getByText('Mandag')).toBeTruthy();
expect(screen.getByText('Tirsdag')).toBeTruthy();
expect(screen.getByText('Onsdag')).toBeTruthy();
expect(screen.getByText('Torsdag')).toBeTruthy();
expect(screen.getByText('Fredag')).toBeTruthy();
expect(screen.getByText('Lørdag')).toBeTruthy();
expect(screen.getByText('Søndag')).toBeTruthy();
});

test('ReleasePatternForm should render all input fields with correct values', () => {
const inputsWith1: number = MockTitle.release_pattern.filter(val => val === 1).length;
const inputsWith0: number = MockTitle.release_pattern.filter(val => val === 0).length;

expect(screen.getAllByRole('gridcell', {name: '1'}).length).toEqual(inputsWith1);
expect(screen.getAllByRole('gridcell', {name: '0'}).length).toEqual(inputsWith0);
});
2 changes: 1 addition & 1 deletion __tests__/components/SearchBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('SearchBar should render', async () => {
await vi.waitFor(() => expect(screen.getByRole('combobox')).toBeTruthy());
});

test('SearchBar should have help test', async () => {
test('SearchBar should have help text', async () => {
await vi.waitFor(() => expect(screen.getByText('Søk etter avistittel')).toBeTruthy());
});

Expand Down
24 changes: 13 additions & 11 deletions __tests__/mockdata.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {box, newspaper, title} from '@prisma/client';
import {box, contact_info, newspaper, title} from '@prisma/client';
import {CatalogTitle} from '@/models/CatalogTitle';

/* eslint-disable @typescript-eslint/naming-convention */

export const MockTitle: title = {
id: 987,
contact_email: '[email protected]',
contact_name: 'Hugin Huginson',
contact_phone: '12345678',
vendor: 'Hugin AS',
release_pattern: [1, 0, 1, 0, 1, 0, 0],
shelf: '1-1',
Expand Down Expand Up @@ -50,12 +48,16 @@ export const MockNewspaper1: newspaper = {
catalog_id: '123456'
};

export const MockNewspaper2: newspaper = {
edition: '2',
date: new Date(2024, 1, 2),
received: true,
username: 'testuser',
notes: 'notater på avis',
box_id: MockBox1.id,
catalog_id: '123456'
export const MockContactEmail1: contact_info = {
id: 'c15d58c6-bc44-49d9-9cbb-e4df84748fde',
title_id: MockTitle.id,
contact_type: 'email',
contact_value: '[email protected]',
};

export const MockContactPhone1: contact_info = {
id: 'd712ce79-f51a-49ed-b7e6-0df2552d325c',
title_id: MockTitle.id,
contact_type: 'phone',
contact_value: '12345678',
};
Loading

0 comments on commit c424a4c

Please sign in to comment.