Skip to content

Commit

Permalink
test(design): re-enable some tests (#6003)
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha authored Feb 11, 2025
1 parent 4c9688c commit d03e078
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 97 deletions.
10 changes: 5 additions & 5 deletions apps/design/backend/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,13 @@ test('Finalize ballots', async () => {
expect(await apiClient.getBallotsFinalizedAt({ electionId })).toEqual(null);

const finalizedAt = new Date();
await apiClient.setBallotsFinalizedAt({ electionId, finalizedAt });
await apiClient.finalizeBallots({ electionId });

expect(await apiClient.getBallotsFinalizedAt({ electionId })).toEqual(
finalizedAt
);
expect(
(await apiClient.getBallotsFinalizedAt({ electionId }))!.valueOf() / 1000
).toBeCloseTo(finalizedAt.valueOf() / 1000);

await apiClient.setBallotsFinalizedAt({ electionId, finalizedAt: null });
await apiClient.unfinalizeBallots({ electionId });

expect(await apiClient.getBallotsFinalizedAt({ electionId })).toEqual(null);
});
Expand Down
17 changes: 12 additions & 5 deletions apps/design/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,18 @@ function buildApi({ auth, workspace, translator }: AppContext) {
return store.getBallotsFinalizedAt(input.electionId);
},

setBallotsFinalizedAt(input: {
electionId: ElectionId;
finalizedAt: Date | null;
}): Promise<void> {
return store.setBallotsFinalizedAt(input);
finalizeBallots(input: { electionId: ElectionId }): Promise<void> {
return store.setBallotsFinalizedAt({
electionId: input.electionId,
finalizedAt: new Date(),
});
},

unfinalizeBallots(input: { electionId: ElectionId }): Promise<void> {
return store.setBallotsFinalizedAt({
electionId: input.electionId,
finalizedAt: null,
});
},

async getBallotPreviewPdf(input: {
Expand Down
4 changes: 0 additions & 4 deletions apps/design/frontend/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
/public

src/app.test.tsx
src/ballot_order_info_screen.test.tsx
src/contests_screen.test.tsx
src/ballot_screen.test.tsx
src/tabulation_screen.test.tsx
src/election_info_screen.test.tsx
src/elections_screen.test.tsx
src/ballots_screen.test.tsx
src/geography_screen.test.tsx
src/image_input.test.tsx
src/features_context.test.tsx
Expand Down
18 changes: 16 additions & 2 deletions apps/design/frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,25 @@ export const getBallotsFinalizedAt = {
},
} as const;

export const setBallotsFinalizedAt = {
export const finalizeBallots = {
useMutation() {
const apiClient = useApiClient();
const queryClient = useQueryClient();
return useMutation(apiClient.setBallotsFinalizedAt, {
return useMutation(apiClient.finalizeBallots, {
async onSuccess(_, { electionId }) {
await queryClient.invalidateQueries(
getBallotsFinalizedAt.queryKey(electionId)
);
},
});
},
} as const;

export const unfinalizeBallots = {
useMutation() {
const apiClient = useApiClient();
const queryClient = useQueryClient();
return useMutation(apiClient.unfinalizeBallots, {
async onSuccess(_, { electionId }) {
await queryClient.invalidateQueries(
getBallotsFinalizedAt.queryKey(electionId)
Expand Down
17 changes: 6 additions & 11 deletions apps/design/frontend/src/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { suppressingConsoleOutput } from '@votingworks/test-utils';
import userEvent from '@testing-library/user-event';
import { ElectionId } from '@votingworks/types';
import { MockApiClient, createMockApiClient } from '../test/api_helpers';
import {
MockApiClient,
createMockApiClient,
nonVxUser,
vxUser,
} from '../test/api_helpers';
import { render, screen } from '../test/react_testing_library';
import { App } from './app';
import { User } from '@votingworks/design-backend';

const nonVxUser: User = {
orgId: '123',
isVotingWorksUser: false,
};

const vxUser: User = {
orgId: 'votingworks',
isVotingWorksUser: true,
};

let apiMock: MockApiClient;

beforeEach(() => {
Expand Down
34 changes: 24 additions & 10 deletions apps/design/frontend/src/ballot_order_info_screen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import userEvent from '@testing-library/user-event';
import type { BallotOrderInfo } from '@votingworks/design-backend';

import {
createMockApiClient,
MockApiClient,
nonVxUser,
provideApi,
} from '../test/api_helpers';
import { generalElectionRecord } from '../test/fixtures';
Expand All @@ -12,10 +14,13 @@ import { withRoute } from '../test/routing_helpers';
import { BallotOrderInfoScreen } from './ballot_order_info_screen';
import { routes } from './routes';

jest.useFakeTimers();

const mockDateTime = new Date('2025-01-01T00:00:00.000Z');

vi.useFakeTimers({
shouldAdvanceTime: true,
now: mockDateTime,
});

const electionRecord = generalElectionRecord;
const electionId = electionRecord.election.id;

Expand All @@ -34,16 +39,19 @@ function renderScreen() {
provideApi(
apiMock,
withRoute(<BallotOrderInfoScreen />, {
paramPath: routes.election(':electionId').tabulation.path,
path: routes.election(electionId).tabulation.path,
paramPath: routes.election(':electionId').ballotOrderInfo.path,
path: routes.election(electionId).ballotOrderInfo.path,
}),
electionId
)
);
}

test('submitting ballot order', async () => {
apiMock.getElection.expectCallWith({ electionId }).resolves(electionRecord);
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ user: nonVxUser, electionId })
.resolves(electionRecord);
apiMock.getBallotsFinalizedAt
.expectCallWith({ electionId })
.resolves(new Date());
Expand Down Expand Up @@ -103,7 +111,7 @@ test('submitting ballot order', async () => {
shouldAbsenteeBallotsBeScoredForFolding: true,
precinctBallotCount: '200',
ballotColor: 'Yellow for town, white for school',
shouldPrintCollated: true,
shouldCollateBallotPages: true,
deliveryRecipientName: 'Clerky Clerkson',
deliveryRecipientPhoneNumber: '(123) 456-7890',
deliveryAddress: '123 Main St, Town, NH, 00000',
Expand All @@ -112,12 +120,12 @@ test('submitting ballot order', async () => {
apiMock.updateBallotOrderInfo
.expectCallWith({ electionId, ballotOrderInfo: expectedBallotOrderInfo })
.resolves();
apiMock.getElection.expectCallWith({ electionId }).resolves({
apiMock.getElection.expectCallWith({ user: nonVxUser, electionId }).resolves({
...electionRecord,
ballotOrderInfo: expectedBallotOrderInfo,
});

jest.setSystemTime(mockDateTime);
vi.setSystemTime(mockDateTime);
userEvent.click(screen.getByRole('button', { name: 'Submit Order' }));
userEvent.click(screen.getByRole('button', { name: 'Submit Order' }));
await screen.findByRole('heading', { name: 'Order Submitted' });
Expand All @@ -141,7 +149,10 @@ test('submitting ballot order', async () => {
});

test('submitting ballot order with validation errors', async () => {
apiMock.getElection.expectCallWith({ electionId }).resolves(electionRecord);
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ user: nonVxUser, electionId })
.resolves(electionRecord);
apiMock.getBallotsFinalizedAt
.expectCallWith({ electionId })
.resolves(new Date());
Expand Down Expand Up @@ -176,7 +187,10 @@ test('submitting ballot order with validation errors', async () => {
});

test('ballot order submission required ballots to be proofed first', async () => {
apiMock.getElection.expectCallWith({ electionId }).resolves(electionRecord);
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ user: nonVxUser, electionId })
.resolves(electionRecord);
apiMock.getBallotsFinalizedAt.expectCallWith({ electionId }).resolves(null);
renderScreen();
await screen.findByRole('heading', { name: 'Order Ballots' });
Expand Down
23 changes: 14 additions & 9 deletions apps/design/frontend/src/ballot_screen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeEach, test, vi } from 'vitest';
import { BallotType } from '@votingworks/types';
import { DocumentProps, PageProps } from 'react-pdf';
import { useEffect } from 'react';
Expand All @@ -8,6 +9,7 @@ import userEvent from '@testing-library/user-event';
import {
MockApiClient,
createMockApiClient,
nonVxUser,
provideApi,
} from '../test/api_helpers';
import { generalElectionRecord } from '../test/fixtures';
Expand Down Expand Up @@ -45,14 +47,14 @@ function MockPage({ pageNumber }: PageProps) {
return <div>Mock Page {pageNumber}</div>;
}

jest.mock('react-pdf', (): typeof import('react-pdf') => {
const original = jest.requireActual('react-pdf');
vi.mock(import('react-pdf'), async (importActual) => {
const original = await importActual();
return {
...original,
pdfjs: { GlobalWorkerOptions: { workerSrc: 'mock-worker-src' } },
Document: MockDocument,
Page: MockPage,
};
} as unknown as typeof original;
});

let apiMock: MockApiClient;
Expand Down Expand Up @@ -83,8 +85,9 @@ function renderScreen() {
}

test('shows a PDF ballot preview', async () => {
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ electionId })
.expectCallWith({ user: nonVxUser, electionId })
.resolves(generalElectionRecord);
apiMock.getBallotPreviewPdf
.expectCallWith({
Expand All @@ -106,9 +109,9 @@ test('shows a PDF ballot preview', async () => {
screen.getByRole('button', { name: 'Close' });

const document = screen.getByText('Mock Document').parentElement!;
within(document).getByText('mock ballot pdf');
within(document).getByText('Mock Page 1');
within(document).getByText('Mock Page 2');
await within(document).findByText('mock ballot pdf');
await within(document).findByText('Mock Page 1');
await within(document).findByText('Mock Page 2');

screen.getByText('Page: 1/2');
screen.getByText('100%');
Expand Down Expand Up @@ -149,8 +152,9 @@ test('shows a PDF ballot preview', async () => {
});

test('changes ballot type', async () => {
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ electionId })
.expectCallWith({ user: nonVxUser, electionId })
.resolves(generalElectionRecord);
apiMock.getBallotPreviewPdf
.expectCallWith({
Expand Down Expand Up @@ -202,8 +206,9 @@ test('changes ballot type', async () => {
});

test('changes tabulation mode', async () => {
apiMock.getUser.expectCallWith().resolves(nonVxUser);
apiMock.getElection
.expectCallWith({ electionId })
.expectCallWith({ user: nonVxUser, electionId })
.resolves(generalElectionRecord);
apiMock.getBallotPreviewPdf
.expectCallWith({
Expand Down
Loading

0 comments on commit d03e078

Please sign in to comment.