-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libs/ui] Shared UI Strings API react-query client (#4016)
- Loading branch information
Showing
13 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/mark-scan/frontend/src/api_machine_configuration.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { ok } from '@votingworks/basics'; | ||
import { electionGeneralDefinition } from '@votingworks/fixtures'; | ||
import { renderHook, waitFor } from '../test/react_testing_library'; | ||
import { | ||
ApiClient, | ||
ApiClientContext, | ||
configureBallotPackageFromUsb, | ||
createApiClient, | ||
uiStringsApi, | ||
unconfigureMachine, | ||
} from './api'; | ||
|
||
const queryClient = new QueryClient(); | ||
const mockBackendApi: ApiClient = { | ||
...createApiClient(), | ||
configureBallotPackageFromUsb: jest.fn(), | ||
unconfigureMachine: jest.fn(), | ||
}; | ||
|
||
function QueryWrapper(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
|
||
return ( | ||
<ApiClientContext.Provider value={mockBackendApi}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</ApiClientContext.Provider> | ||
); | ||
} | ||
|
||
const mockOnConfigurationChange = jest.spyOn( | ||
uiStringsApi, | ||
'onMachineConfigurationChange' | ||
); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('configureBallotPackageFromUsb', async () => { | ||
jest | ||
.mocked(mockBackendApi) | ||
.configureBallotPackageFromUsb.mockResolvedValueOnce( | ||
ok(electionGeneralDefinition) | ||
); | ||
|
||
const { result } = renderHook( | ||
() => configureBallotPackageFromUsb.useMutation(), | ||
{ wrapper: QueryWrapper } | ||
); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); | ||
|
||
test('unconfigureMachine', async () => { | ||
jest.mocked(mockBackendApi).unconfigureMachine.mockResolvedValueOnce(); | ||
|
||
const { result } = renderHook(() => unconfigureMachine.useMutation(), { | ||
wrapper: QueryWrapper, | ||
}); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { ok } from '@votingworks/basics'; | ||
import { electionGeneralDefinition } from '@votingworks/fixtures'; | ||
import { renderHook, waitFor } from '../test/react_testing_library'; | ||
import { | ||
ApiClient, | ||
ApiClientContext, | ||
configureBallotPackageFromUsb, | ||
createApiClient, | ||
uiStringsApi, | ||
unconfigureMachine, | ||
} from './api'; | ||
|
||
const queryClient = new QueryClient(); | ||
const mockBackendApi: ApiClient = { | ||
...createApiClient(), | ||
configureBallotPackageFromUsb: jest.fn(), | ||
unconfigureMachine: jest.fn(), | ||
}; | ||
|
||
function QueryWrapper(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
|
||
return ( | ||
<ApiClientContext.Provider value={mockBackendApi}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</ApiClientContext.Provider> | ||
); | ||
} | ||
|
||
const mockOnConfigurationChange = jest.spyOn( | ||
uiStringsApi, | ||
'onMachineConfigurationChange' | ||
); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('configureBallotPackageFromUsb', async () => { | ||
jest | ||
.mocked(mockBackendApi) | ||
.configureBallotPackageFromUsb.mockResolvedValueOnce( | ||
ok(electionGeneralDefinition) | ||
); | ||
|
||
const { result } = renderHook( | ||
() => configureBallotPackageFromUsb.useMutation(), | ||
{ wrapper: QueryWrapper } | ||
); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); | ||
|
||
test('unconfigureMachine', async () => { | ||
jest.mocked(mockBackendApi).unconfigureMachine.mockResolvedValueOnce(); | ||
|
||
const { result } = renderHook(() => unconfigureMachine.useMutation(), { | ||
wrapper: QueryWrapper, | ||
}); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { ok } from '@votingworks/basics'; | ||
import React from 'react'; | ||
import { renderHook, waitFor } from '../test/react_testing_library'; | ||
import { | ||
ApiClient, | ||
ApiClientContext, | ||
configureFromBallotPackageOnUsbDrive, | ||
createApiClient, | ||
unconfigureElection, | ||
uiStringsApi, | ||
} from './api'; | ||
|
||
const queryClient = new QueryClient(); | ||
const mockBackendApi: ApiClient = { | ||
...createApiClient(), | ||
configureFromBallotPackageOnUsbDrive: jest.fn(), | ||
unconfigureElection: jest.fn(), | ||
}; | ||
|
||
function QueryWrapper(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
|
||
return ( | ||
<ApiClientContext.Provider value={mockBackendApi}> | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
</ApiClientContext.Provider> | ||
); | ||
} | ||
|
||
const mockOnConfigurationChange = jest.spyOn( | ||
uiStringsApi, | ||
'onMachineConfigurationChange' | ||
); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('configureFromBallotPackageOnUsbDrive', async () => { | ||
jest | ||
.mocked(mockBackendApi) | ||
.configureFromBallotPackageOnUsbDrive.mockResolvedValueOnce(ok()); | ||
|
||
const { result } = renderHook( | ||
() => configureFromBallotPackageOnUsbDrive.useMutation(), | ||
{ wrapper: QueryWrapper } | ||
); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate(); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); | ||
|
||
test('unconfigureElection', async () => { | ||
jest.mocked(mockBackendApi).unconfigureElection.mockResolvedValue(); | ||
|
||
const { result } = renderHook(() => unconfigureElection.useMutation(), { | ||
wrapper: QueryWrapper, | ||
}); | ||
|
||
expect(mockOnConfigurationChange).not.toHaveBeenCalled(); | ||
|
||
result.current.mutate({}); | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
|
||
expect(mockOnConfigurationChange).toHaveBeenCalled(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { LanguageCode } from '@votingworks/types'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { renderHook, waitFor } from '../../test/react_testing_library'; | ||
import { createUiStringsApi } from './ui_strings_api'; | ||
|
||
const queryClient = new QueryClient(); | ||
function QueryWrapper(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
} | ||
|
||
const mockApiClient = { | ||
getAudioClipsBase64: jest.fn(), | ||
getAvailableLanguages: jest.fn(), | ||
getUiStringAudioIds: jest.fn(), | ||
getUiStrings: jest.fn(), | ||
} as const; | ||
|
||
const api = createUiStringsApi(() => mockApiClient); | ||
|
||
test('getAvailableLanguages', async () => { | ||
// Simulate initial machine state: | ||
mockApiClient.getAvailableLanguages.mockResolvedValueOnce([]); | ||
|
||
const { result } = renderHook(() => api.getAvailableLanguages.useQuery(), { | ||
wrapper: QueryWrapper, | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)); | ||
expect(result.current.data).toEqual([]); | ||
|
||
// Simulate configuring an election: | ||
await act(async () => { | ||
mockApiClient.getAvailableLanguages.mockResolvedValueOnce([ | ||
LanguageCode.CHINESE, | ||
LanguageCode.SPANISH, | ||
]); | ||
await api.onMachineConfigurationChange(queryClient); | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toEqual(false)); | ||
expect(result.current.data).toEqual([ | ||
LanguageCode.CHINESE, | ||
LanguageCode.SPANISH, | ||
]); | ||
|
||
// Simulate unconfiguring an election: | ||
await act(async () => { | ||
mockApiClient.getAvailableLanguages.mockResolvedValueOnce([]); | ||
await api.onMachineConfigurationChange(queryClient); | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toEqual(false)); | ||
expect(result.current.data).toEqual([]); | ||
}); |
Oops, something went wrong.