Skip to content

Commit

Permalink
minor test formatting fixes (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
cammiida authored Dec 20, 2024
1 parent c491668 commit e07c706
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 186 deletions.
5 changes: 2 additions & 3 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ describe('App', () => {
});

test('should render unknown error when hasApplicationMetadataError', async () => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementation(() =>
Promise.reject(new Error('500 Server Error')),
);
jest.mocked(fetchApplicationMetadata).mockImplementation(() => Promise.reject(new Error('500 Server Error')));

await renderWithInstanceAndLayout({
renderer: () => <App />,
});
Expand Down
7 changes: 2 additions & 5 deletions src/components/organisms/AltinnAppHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { act, screen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getLogoMock } from 'src/__mocks__/getLogoMock';
Expand Down Expand Up @@ -38,9 +37,7 @@ describe('organisms/AltinnAppHeader', () => {
logo?: ApplicationMetadata['logoOptions'];
}
const render = async ({ party, user = partyPerson, logo }: IRenderComponentProps) => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementation(() =>
Promise.resolve(getIncomingApplicationMetadataMock({ logo })),
);
jest.mocked(fetchApplicationMetadata).mockImplementation(async () => getIncomingApplicationMetadataMock({ logo }));

return await renderWithInstanceAndLayout({
renderer: () => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { OrganisationLogo } from 'src/components/presentation/OrganisationLogo/OrganisationLogo';
Expand All @@ -11,9 +10,7 @@ import { renderWithInstanceAndLayout } from 'src/test/renderWithProviders';
import type { IncomingApplicationMetadata } from 'src/features/applicationMetadata/types';

const render = async (logo: IncomingApplicationMetadata['logo']) => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementation(() =>
Promise.resolve(getIncomingApplicationMetadataMock({ logo })),
);
jest.mocked(fetchApplicationMetadata).mockImplementation(async () => getIncomingApplicationMetadataMock({ logo }));

return await renderWithInstanceAndLayout({
renderer: () => <OrganisationLogo />,
Expand Down
8 changes: 4 additions & 4 deletions src/components/presentation/Presentation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { HttpStatusCodes } from 'src/utils/network/networking';
import { returnUrlToMessagebox } from 'src/utils/urls/urlHelper';
import type { IPresentationProvidedProps } from 'src/components/presentation/Presentation';

jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
const assignMock = jest.fn();

describe('Presentation', () => {
const user = userEvent.setup();
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
let assignMock = jest.fn();
let realLocation: Location = window.location;

beforeEach(() => {
assignMock = jest.fn();
realLocation = window.location;
});

Expand Down
8 changes: 3 additions & 5 deletions src/core/texts/appTexts.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { useAppName, useAppOwner } from 'src/core/texts/appTexts';
Expand Down Expand Up @@ -32,9 +31,8 @@ interface RenderProps {

async function render({ nbTitle, textResources = [], orgs = {} }: RenderProps) {
const overrides = nbTitle ? { title: { nb: nbTitle } } : {};
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementation(() =>
Promise.resolve(getIncomingApplicationMetadataMock(overrides)),
);
jest.mocked(fetchApplicationMetadata).mockImplementation(async () => getIncomingApplicationMetadataMock(overrides));

return await renderWithoutInstanceAndLayout({
renderer: () => <AppTextsRenderer />,
queries: {
Expand Down
8 changes: 3 additions & 5 deletions src/features/expressions/shared-context.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import { jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock';
Expand Down Expand Up @@ -97,6 +97,8 @@ describe('Expressions shared context tests', () => {
: undefined;

const applicationMetadata = getApplicationMetadataMock(instance ? {} : { onEntry: { show: 'stateless' } });
jest.mocked(fetchApplicationMetadata).mockImplementation(async () => applicationMetadata);

if (instanceDataElements) {
for (const element of instanceDataElements) {
if (!applicationMetadata.dataTypes!.find((dt) => dt.id === element.dataType)) {
Expand All @@ -110,10 +112,6 @@ describe('Expressions shared context tests', () => {
}
}

(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementation(() =>
Promise.resolve(applicationMetadata),
);

await renderWithInstanceAndLayout({
renderer: () => <TestContexts />,
queries: {
Expand Down
12 changes: 3 additions & 9 deletions src/features/expressions/shared-functions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,10 @@ describe('Expressions shared function tests', () => {
// Clear localstorage, because LanguageProvider uses it to cache selected languages
localStorage.clear();

(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockResolvedValue(applicationMetadata);
jest.mocked(fetchApplicationMetadata).mockResolvedValue(applicationMetadata);
jest.mocked(useExternalApis).mockReturnValue(externalApis as ExternalApisResult);

if (roles) {
jest.mocked(useCurrentPartyRoles).mockReturnValue(roles as RoleResult);
}

(fetchProcessState as jest.Mock<typeof fetchProcessState>).mockImplementation(() =>
Promise.resolve(process ?? getProcessDataMock()),
);
jest.mocked(fetchProcessState).mockImplementation(async () => process ?? getProcessDataMock());
jest.mocked(useCurrentPartyRoles).mockReturnValue(roles as RoleResult);

const nodeId = nodeIdFromContext(context);
await renderWithNode({
Expand Down
20 changes: 9 additions & 11 deletions src/features/formData/FormData.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ const mockSchema: JSONSchema7 = {
type MinimalRenderProps = Partial<Omit<Parameters<typeof renderWithInstanceAndLayout>[0], 'renderer'>>;
type RenderProps = MinimalRenderProps & { renderer: React.ReactElement };
async function statelessRender(props: RenderProps) {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve(
getIncomingApplicationMetadataMock({
onEntry: {
show: 'stateless',
},
}),
),
jest.mocked(fetchApplicationMetadata).mockImplementationOnce(async () =>
getIncomingApplicationMetadataMock({
onEntry: {
show: 'stateless',
},
}),
);
const initialRenderRef = { current: true };
const { mocks: formDataMethods, proxies: formDataProxies } = makeFormDataMethodProxies(initialRenderRef);
Expand Down Expand Up @@ -160,9 +158,9 @@ async function statelessRender(props: RenderProps) {
}

async function statefulRender(props: RenderProps) {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve(getIncomingApplicationMetadataMock()),
);
jest
.mocked(fetchApplicationMetadata)
.mockImplementationOnce(() => Promise.resolve(getIncomingApplicationMetadataMock()));
return await renderWithInstanceAndLayout({
...props,
alwaysRouteToChildren: true,
Expand Down
12 changes: 5 additions & 7 deletions src/features/formData/FormDataReaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ function TestComponent({ ids }: TestProps) {
}

async function render(props: TestProps) {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve(
getIncomingApplicationMetadataMock((a) => {
a.dataTypes = a.dataTypes.filter((dt) => !dt.appLogic?.classRef);
a.dataTypes.push(...generateDataTypes());
}),
),
jest.mocked(fetchApplicationMetadata).mockImplementationOnce(async () =>
getIncomingApplicationMetadataMock((a) => {
a.dataTypes = a.dataTypes.filter((dt) => !dt.appLogic?.classRef);
a.dataTypes.push(...generateDataTypes());
}),
);
const dataModelNames = Object.keys(props.dataModels);
const idToNameMap: { [id: string]: string } = {};
Expand Down
16 changes: 7 additions & 9 deletions src/features/processEnd/confirm/containers/ConfirmPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

import { jest } from '@jest/globals';
import { screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import type { jest } from '@jest/globals';

import { getApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock';
Expand Down Expand Up @@ -69,14 +69,12 @@ describe('ConfirmPage', () => {
});

it('should show loading when clicking submit', async () => {
(fetchProcessState as jest.Mock<typeof fetchProcessState>).mockImplementation(() =>
Promise.resolve(
getProcessDataMock((p) => {
p.currentTask!.actions = {
confirm: true,
};
}),
),
jest.mocked(fetchProcessState).mockImplementation(async () =>
getProcessDataMock((p) => {
p.currentTask!.actions = {
confirm: true,
};
}),
);

const { mutations } = await renderWithInstanceAndLayout({
Expand Down
25 changes: 10 additions & 15 deletions src/features/receipt/ReceiptContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock';
Expand Down Expand Up @@ -78,20 +77,16 @@ const buildInstance = (hasPdf = true) =>
});

const render = async ({ autoDeleteOnProcessEnd = false, hasPdf = true }: IRender = {}) => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve(
getIncomingApplicationMetadataMock((a) => {
a.autoDeleteOnProcessEnd = autoDeleteOnProcessEnd;
}),
),
jest.mocked(fetchApplicationMetadata).mockImplementationOnce(async () =>
getIncomingApplicationMetadataMock((a) => {
a.autoDeleteOnProcessEnd = autoDeleteOnProcessEnd;
}),
);
(fetchProcessState as jest.Mock<typeof fetchProcessState>).mockImplementation(() =>
Promise.resolve(
getProcessDataMock((p) => {
p.currentTask = undefined;
p.ended = '2022-02-05T09:19:32.8858042Z';
}),
),
jest.mocked(fetchProcessState).mockImplementation(async () =>
getProcessDataMock((p) => {
p.currentTask = undefined;
p.ended = '2022-02-05T09:19:32.8858042Z';
}),
);

return await renderWithoutInstanceAndLayout({
Expand Down
25 changes: 11 additions & 14 deletions src/features/stateless/getAllowAnonymous.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getLayoutSetsMock } from 'src/__mocks__/getLayoutSetsMock';
Expand All @@ -16,18 +15,16 @@ const TestComponent = () => {
};

const render = async (stateless: boolean, allowAnonymous: boolean) => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve({
...getIncomingApplicationMetadataMock(),
...(stateless
? {
onEntry: {
show: allowAnonymous ? 'stateless-anon' : 'stateless',
},
}
: {}),
}),
);
jest.mocked(fetchApplicationMetadata).mockImplementationOnce(async () => ({
...getIncomingApplicationMetadataMock(),
...(stateless
? {
onEntry: {
show: allowAnonymous ? 'stateless-anon' : 'stateless',
},
}
: {}),
}));

return await renderWithoutInstanceAndLayout({
renderer: () => <TestComponent />,
Expand Down
28 changes: 13 additions & 15 deletions src/layout/AttachmentList/AttachmentListComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import { expect } from '@jest/globals';
import { expect, jest } from '@jest/globals';
import { screen } from '@testing-library/react';
import type { jest } from '@jest/globals';

import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock';
import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock';
Expand Down Expand Up @@ -45,20 +44,19 @@ describe('AttachmentListComponent', () => {
});

const render = async (ids?: string[]) => {
(fetchApplicationMetadata as jest.Mock<typeof fetchApplicationMetadata>).mockImplementationOnce(() =>
Promise.resolve(
getIncomingApplicationMetadataMock((a) => {
a.dataTypes.push(
generateDataType({ id: 'not-ref-data-as-pdf', dataType: 'text/plain', taskId: 'Task_1' }),
generateDataType({
id: 'different-process-task',
dataType: 'text/plain',
taskId: 'Task_2',
}),
);
}),
),
jest.mocked(fetchApplicationMetadata).mockImplementationOnce(async () =>
getIncomingApplicationMetadataMock((a) => {
a.dataTypes.push(
generateDataType({ id: 'not-ref-data-as-pdf', dataType: 'text/plain', taskId: 'Task_1' }),
generateDataType({
id: 'different-process-task',
dataType: 'text/plain',
taskId: 'Task_2',
}),
);
}),
);

return await renderGenericComponentTest({
type: 'AttachmentList',
renderer: (props) => <AttachmentListComponent {...props} />,
Expand Down
Loading

0 comments on commit e07c706

Please sign in to comment.