Skip to content

Commit

Permalink
fix tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusLevang committed Sep 2, 2024
1 parent bf5aefd commit b2e7531
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
7 changes: 4 additions & 3 deletions __tests__/app/[id]/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import Page from '@/app/[id]/page';
import {fetchNewspaperTitleFromCatalog} from '@/services/catalog.data';
import {MockCatalogTitle1, MockNewspaper1, MockTitle} from '../../mockdata';
import {getIssuesForTitle, getLocalTitle} from '@/services/local.data';
import {MockBox1, MockCatalogTitle1, MockNewspaper1, MockTitle} from '../../mockdata';
import {getBoxForTitle, getLocalTitle, getNewspapersForBoxOnTitle} from '@/services/local.data';
import {NotFoundError} from '@/models/Errors';

beforeEach(() => {
vi.mocked(fetchNewspaperTitleFromCatalog).mockImplementation(() => Promise.resolve(MockCatalogTitle1));
vi.mocked(getLocalTitle).mockImplementation(() => Promise.resolve(MockTitle));
vi.mocked(getIssuesForTitle).mockImplementation(() => Promise.resolve([MockNewspaper1]));
vi.mocked(getBoxForTitle).mockImplementation(() => Promise.resolve(MockBox1));
vi.mocked(getNewspapersForBoxOnTitle).mockImplementation(() => Promise.resolve([MockNewspaper1]));
render(<Page params={{id: ''}}/>);
});

Expand Down
11 changes: 6 additions & 5 deletions __tests__/components/BoxRegistrationModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {beforeEach, expect, test, vi} from 'vitest';
import * as localData from '@/services/local.data';
import {updateBoxForTitle} from '@/services/local.data';
import {getBoxById, postNewBoxForTitle} from '@/services/local.data';
import {fireEvent, render, screen} from '@testing-library/react';
import {NextResponse} from 'next/server';
import BoxRegistrationModal from '@/components/BoxRegistrationModal';
import {MockBox1} from '../mockdata';

beforeEach(() => {
vi.mocked(updateBoxForTitle).mockImplementation(() => Promise.resolve(new NextResponse(null, {status: 204})));
render(<BoxRegistrationModal text={'boxText'} titleId='123' closeModal={() => {}} updateBoxInfo={() => {}}/>);
vi.mocked(getBoxById).mockImplementation(() => Promise.resolve(MockBox1));
vi.mocked(postNewBoxForTitle).mockImplementation(() => Promise.resolve(MockBox1));
render(<BoxRegistrationModal text={'boxText'} titleId='123' titleName='title' closeModal={() => {}} updateBoxInfo={() => {}}/>);
});

test('Box registration has field for box id', () => {
Expand All @@ -25,7 +26,7 @@ test('Box registration has calendar for start date', () => {
});

test('Box registration saves on button press', async () => {
const saveSpy = vi.spyOn(localData, 'updateBoxForTitle');
const saveSpy = vi.spyOn(localData, 'postNewBoxForTitle');
expect(saveSpy).not.toHaveBeenCalled();

fireEvent.change(screen.getByRole('textbox', {name: 'Eske id'}), {target: {value: '123'}});
Expand Down
3 changes: 0 additions & 3 deletions __tests__/components/ContactAndReleaseInfo.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {beforeEach, expect, test, vi} from 'vitest';
import {updateBoxForTitle} from '@/services/local.data';
import {NextResponse} from 'next/server';
import {cleanup, render, screen} from '@testing-library/react';
import ContactAndReleaseInfo from '@/components/ContactAndReleaseInfo';
import {MockTitle} from '../mockdata';


beforeEach(() => {
vi.mocked(updateBoxForTitle).mockImplementation(() => Promise.resolve(new NextResponse(null, {status: 204})));
render(<ContactAndReleaseInfo titleFromDb={MockTitle} onSubmit={() => Promise.resolve(new Response())} />);
});

Expand Down
8 changes: 4 additions & 4 deletions __tests__/components/IssueList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {beforeEach, expect, test, vi} from 'vitest';
import {render, screen} from '@testing-library/react';
import IssueList from '@/components/IssueList';
import {MockNewspaper1, MockTitle} from '../mockdata';
import {MockBox1, MockNewspaper1, MockTitle} from '../mockdata';
import * as localData from '@/services/local.data';
import {getIssuesForTitle, postNewIssuesForTitle} from '@/services/local.data';
import {getNewspapersForBoxOnTitle, postNewIssuesForTitle} from '@/services/local.data';

// FieldArray and tables are hard to test, and might require a different approach that usual.
// Not using time to test all cases at this point, but might be smart to try that at some point.

beforeEach(() => {
vi.mocked(getIssuesForTitle).mockImplementation(() => Promise.resolve([MockNewspaper1]));
vi.mocked(getNewspapersForBoxOnTitle).mockImplementation(() => Promise.resolve([MockNewspaper1]));
vi.mocked(postNewIssuesForTitle).mockImplementation(() => Promise.resolve(new Response('', {status: 200})));
render(<IssueList title={MockTitle}/>);
render(<IssueList title={MockTitle} box={MockBox1}/>);
});

test('IssueList should render with new edition and save button', async () => {
Expand Down
17 changes: 10 additions & 7 deletions __tests__/mockdata.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {newspaper, title} from '@prisma/client';
import {box, newspaper, title} from '@prisma/client';
import {CatalogTitle} from '@/models/CatalogTitle';

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

export const MockTitle: title = {
id: 987,
last_box: 'box1',
contact_email: '[email protected]',
contact_name: 'Hugin Huginson',
contact_phone: '12345678',
vendor: 'Hugin AS',
release_pattern: [1, 0, 1, 0, 1, 0, 0],
last_box_from: new Date(2024, 1, 1),
shelf: '1-1',
notes: 'notater',
};
Expand All @@ -26,6 +24,13 @@ export const MockCatalogTitle1: CatalogTitle = {
language: 'nob'
};

export const MockBox1: box = {
id: 'box1',
date_from: new Date(2024, 0, 1),
active: true,
title_id: MockTitle.id,
};

export const MockCatalogTitle2: CatalogTitle = {
catalogueId: '123',
name: 'Huginbladet',
Expand All @@ -36,23 +41,21 @@ export const MockCatalogTitle2: CatalogTitle = {
};

export const MockNewspaper1: newspaper = {
title_id: MockTitle.id,
edition: '1',
date: new Date(2024, 1, 1),
received: true,
username: 'testuser',
box: MockTitle.last_box!,
notes: 'notater på avis',
box_id: MockBox1.id,
catalog_id: '123456'
};

export const MockNewspaper2: newspaper = {
title_id: MockTitle.id,
edition: '2',
date: new Date(2024, 1, 2),
received: true,
username: 'testuser',
box: MockTitle.last_box!,
notes: 'notater på avis',
box_id: MockBox1.id,
catalog_id: '123456'
};
10 changes: 7 additions & 3 deletions __tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ beforeAll(() => {
getLocalTitle: vi.fn(),
postLocalTitle: vi.fn(),
putLocalTitle: vi.fn(),
updateBoxForTitle: vi.fn(),
updateActiveBoxForTitle: vi.fn(),
updateNotesForTitle: vi.fn(),
updateShelfForTitle: vi.fn(),
getIssuesForTitle: vi.fn(),
postNewIssuesForTitle: vi.fn()
getBoxById: vi.fn(),
getBoxForTitle: vi.fn(),
getNewspapersForBoxOnTitle: vi.fn(),
postNewBoxForTitle: vi.fn(),
postNewIssuesForTitle: vi.fn(),
deleteIssue: vi.fn()
}));
});

Expand Down

0 comments on commit b2e7531

Please sign in to comment.