Skip to content

Commit

Permalink
Update/add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arsalansufi committed Oct 4, 2023
1 parent 9615ad3 commit 4e2090f
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 20 deletions.
8 changes: 4 additions & 4 deletions apps/scan/frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ module.exports = {
],
coverageThreshold: {
global: {
statements: 94,
branches: 86,
functions: 86,
lines: 94,
statements: 95,
branches: 87,
functions: 88,
lines: 95,
},
},
resetMocks: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { screen, within } from '@testing-library/react';
import { screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { err } from '@votingworks/basics';

Expand Down Expand Up @@ -41,12 +41,16 @@ test('CVR sync modal success case', async () => {

apiMock.expectExportCastVoteRecordsToUsbDrive({ mode: 'full_export' });
userEvent.click(within(modal).getByRole('button', { name: 'Sync CVRs' }));
within(modal).getByText('Syncing CVRs');
apiMock.expectGetUsbDriveStatus('mounted');
await within(modal).findByText('Syncing CVRs');
await within(modal).findByText('Voters may continue casting ballots.');
apiMock.expectGetUsbDriveStatus('mounted');

userEvent.click(within(modal).getByRole('button', { name: 'Close' }));
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
await waitFor(() =>
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
);

await waitFor(() => apiMock.mockApiClient.assertComplete());
});

test('CVR sync modal error case', async () => {
Expand All @@ -67,6 +71,6 @@ test('CVR sync modal error case', async () => {
.expectCallWith({ mode: 'full_export' })
.resolves(err({ type: 'file-system-error', message: '' }));
userEvent.click(within(modal).getByRole('button', { name: 'Sync CVRs' }));
within(modal).getByText('Syncing CVRs');
await within(modal).findByText('Syncing CVRs');
await within(modal).findByText('Try inserting a different USB drive.');
});
206 changes: 196 additions & 10 deletions apps/scan/frontend/src/screens/election_manager_screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ beforeEach(() => {
window.location.href = '/';
window.kiosk = fakeKiosk();
apiMock = createApiMock();
apiMock.expectCheckUltrasonicSupported(true);
apiMock.expectGetMachineConfig();
apiMock.expectGetScannerStatus(statusNoPaper);
apiMock.expectGetUsbDriveStatus('mounted');
});

afterEach(() => {
Expand All @@ -62,7 +64,6 @@ function renderScreen(

test('renders date and time settings modal', async () => {
jest.setSystemTime(new Date('2020-10-31T00:00:00.000Z'));
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig();
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand Down Expand Up @@ -100,7 +101,6 @@ test('renders date and time settings modal', async () => {
});

test('option to set precinct if more than one', async () => {
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig();
const precinct = electionGeneralDefinition.election.precincts[0];
const precinctSelection = singlePrecinctSelectionFor(precinct.id);
Expand All @@ -114,7 +114,6 @@ test('option to set precinct if more than one', async () => {
});

test('no option to change precinct if there is only one precinct', async () => {
apiMock.expectCheckUltrasonicSupported(false);
const electionDefinition =
electionTwoPartyPrimaryFixtures.singlePrecinctElectionDefinition;
apiMock.expectGetConfig({
Expand All @@ -128,7 +127,6 @@ test('no option to change precinct if there is only one precinct', async () => {
});

test('unconfigure does not eject a usb drive that is not mounted', async () => {
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig();
renderScreen({
scannerStatus: statusNoPaper,
Expand All @@ -150,7 +148,6 @@ test('unconfigure does not eject a usb drive that is not mounted', async () => {
});

test('unconfigure ejects a usb drive when it is mounted', async () => {
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig();
renderScreen({
scannerStatus: statusNoPaper,
Expand All @@ -171,7 +168,6 @@ test('unconfigure ejects a usb drive when it is mounted', async () => {
});

test('when sounds are not muted, shows a button to mute sounds', async () => {
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig({ isSoundMuted: false });
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand All @@ -188,7 +184,6 @@ test('when sounds are not muted, shows a button to mute sounds', async () => {
});

test('when sounds are muted, shows a button to unmute sounds', async () => {
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig({ isSoundMuted: true });
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand All @@ -205,6 +200,7 @@ test('when sounds are muted, shows a button to unmute sounds', async () => {
});

test('does not show ultrasonic button if not supported', async () => {
apiMock.mockApiClient.supportsUltrasonic.reset();
apiMock.expectCheckUltrasonicSupported(false);
apiMock.expectGetConfig();
renderScreen();
Expand All @@ -216,7 +212,6 @@ test('does not show ultrasonic button if not supported', async () => {
});

test('shows ultrasonic toggle when supported', async () => {
apiMock.expectCheckUltrasonicSupported(true);
apiMock.expectGetConfig();
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand All @@ -227,7 +222,6 @@ test('shows ultrasonic toggle when supported', async () => {
});

test('prompts to enable ultrasonic when disabled ', async () => {
apiMock.expectCheckUltrasonicSupported(true);
apiMock.expectGetConfig({ isUltrasonicDisabled: true });
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand All @@ -238,7 +232,6 @@ test('prompts to enable ultrasonic when disabled ', async () => {
});

test('disables ultrasonic properly', async () => {
apiMock.expectCheckUltrasonicSupported(true);
apiMock.expectGetConfig();
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });
Expand All @@ -253,3 +246,196 @@ test('disables ultrasonic properly', async () => {
await screen.findButton('Enable Double Sheet Detection');
await screen.findByText('Enable Double Sheet Detection');
});

test('switching mode when no ballots have been counted', async () => {
apiMock.expectGetConfig({ isTestMode: true });
renderScreen({ scannerStatus: { ...statusNoPaper, ballotsCounted: 0 } });
await screen.findByRole('heading', { name: 'Election Manager Settings' });

userEvent.click(screen.getByRole('tab', { name: 'Configuration' }));

await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: true,
});
const officialBallotModeButton = screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: false,
});

// Switch from test mode to official mode
apiMock.expectSetTestMode(false);
apiMock.expectGetConfig({ isTestMode: false });
userEvent.click(officialBallotModeButton);
const testBallotModeButton = await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: false,
});
screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: true,
});

// Switch from official mode to test mode
apiMock.expectSetTestMode(true);
apiMock.expectGetConfig({ isTestMode: true });
userEvent.click(testBallotModeButton);
await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: true,
});
screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: false,
});
});

test('switching to official mode when ballots have been counted', async () => {
apiMock.expectGetConfig({ isTestMode: true });
renderScreen({ scannerStatus: { ...statusNoPaper, ballotsCounted: 1 } });
await screen.findByRole('heading', { name: 'Election Manager Settings' });

userEvent.click(screen.getByRole('tab', { name: 'Configuration' }));

await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: true,
});
const officialBallotModeButton = screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: false,
});

apiMock.expectSetTestMode(false);
apiMock.expectGetConfig({ isTestMode: false });
userEvent.click(officialBallotModeButton);
await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: false,
});
screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: true,
});
});

test('switching to test mode when ballots have been counted', async () => {
apiMock.expectGetConfig({ isTestMode: false });
renderScreen({ scannerStatus: { ...statusNoPaper, ballotsCounted: 1 } });
await screen.findByRole('heading', { name: 'Election Manager Settings' });

userEvent.click(screen.getByRole('tab', { name: 'Configuration' }));

await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: false,
});
let officialBallotModeButton = screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: true,
});

// Cancel the first time
userEvent.click(officialBallotModeButton);
let modal = await screen.findByRole('alertdialog');
within(modal).getByText(
'Do you want to switch to test mode and clear the ballots scanned at this scanner?'
);
userEvent.click(within(modal).getByRole('button', { name: 'Cancel' }));
await waitFor(() =>
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
);
await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: false,
});
officialBallotModeButton = screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: true,
});

// Proceed the second time
userEvent.click(officialBallotModeButton);
modal = await screen.findByRole('alertdialog');
within(modal).getByText(
'Do you want to switch to test mode and clear the ballots scanned at this scanner?'
);
apiMock.expectSetTestMode(true);
apiMock.expectGetConfig({ isTestMode: true });
userEvent.click(within(modal).getByRole('button', { name: 'Yes, Switch' }));
await waitFor(() =>
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
);
// For some reason, getting by role doesn't work here
expect(
screen
.getByText('Test Ballot Mode')
.closest('button')
?.getAttribute('aria-selected')
).toEqual('true');
expect(
screen
.getByText('Official Ballot Mode')
.closest('button')
?.getAttribute('aria-selected')
).toEqual('false');
});

test('machine cannot be switched to test mode if CVR sync is required and ballots have been counted', async () => {
apiMock.mockApiClient.getUsbDriveStatus.reset();
apiMock.expectGetUsbDriveStatus('mounted', {
doesUsbDriveRequireCastVoteRecordSync: true,
});
apiMock.expectGetConfig({ isTestMode: false });
renderScreen({ scannerStatus: { ...statusNoPaper, ballotsCounted: 1 } });
await screen.findByRole('heading', { name: 'Election Manager Settings' });

userEvent.click(screen.getByRole('tab', { name: 'Configuration' }));

await screen.findByRole('option', {
name: 'Test Ballot Mode',
selected: false,
});
const officialBallotModeButton = screen.getByRole('option', {
name: 'Official Ballot Mode',
selected: true,
});

userEvent.click(officialBallotModeButton);
const modal = await screen.findByRole('alertdialog');
within(modal).getByText(
'Cast vote records (CVRs) need to be synced to the inserted USB drive before you can switch to test mode. ' +
'Remove your election manager card to sync.'
);
userEvent.click(within(modal).getByRole('button', { name: 'Cancel' }));
await waitFor(() =>
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
);
});

test('machine cannot be unconfigured if CVR sync is required and not in test mode', async () => {
apiMock.mockApiClient.getUsbDriveStatus.reset();
apiMock.expectGetUsbDriveStatus('mounted', {
doesUsbDriveRequireCastVoteRecordSync: true,
});
apiMock.expectGetConfig({ isTestMode: false });
renderScreen();
await screen.findByRole('heading', { name: 'Election Manager Settings' });

userEvent.click(screen.getByRole('tab', { name: 'Election Data' }));

userEvent.click(
await screen.findByRole('button', {
name: 'Delete All Election Data from VxScan',
})
);
const modal = await screen.findByRole('alertdialog');
within(modal).getByText(
'Cast vote records (CVRs) need to be synced to the inserted USB drive before you can delete election data. ' +
'Remove your election manager card to sync.'
);
userEvent.click(within(modal).getByRole('button', { name: 'Cancel' }));
await waitFor(() =>
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
);
});
Loading

0 comments on commit 4e2090f

Please sign in to comment.