-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[i18n/audio] Add <ReadOnLoad> for reading intro text on page/route lo…
…ad (#4483)
- Loading branch information
Showing
4 changed files
with
213 additions
and
4 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
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,140 @@ | ||
import { Optional, assert } from '@votingworks/basics'; | ||
import { act } from 'react-dom/test-utils'; | ||
import React from 'react'; | ||
import { createMemoryHistory } from 'history'; | ||
import { Route, Router } from 'react-router-dom'; | ||
import { ReadOnLoad } from './read_on_load'; | ||
import { render, screen } from '../../test/react_testing_library'; | ||
import { | ||
UiStringsAudioContextInterface, | ||
UiStringsAudioContextProvider, | ||
useAudioContext, | ||
} from './audio_context'; | ||
import { | ||
UiStringsReactQueryApi, | ||
createUiStringsApi, | ||
} from '../hooks/ui_strings_api'; | ||
|
||
const mockUiStringsApi: UiStringsReactQueryApi = createUiStringsApi(() => ({ | ||
getAudioClips: jest.fn(), | ||
getAvailableLanguages: jest.fn(), | ||
getUiStringAudioIds: jest.fn(), | ||
getUiStrings: jest.fn(), | ||
})); | ||
|
||
function renderWithClickListener(ui: React.ReactNode) { | ||
const mockOnClick = jest.fn(); | ||
const result = render(<div onClickCapture={mockOnClick}>{ui}</div>); | ||
|
||
return { | ||
mockOnClick, | ||
result, | ||
}; | ||
} | ||
|
||
let audioContext: Optional<UiStringsAudioContextInterface>; | ||
function TestContextConsumer() { | ||
audioContext = useAudioContext(); | ||
|
||
return null; | ||
} | ||
|
||
afterEach(() => { | ||
audioContext = undefined; | ||
}); | ||
|
||
test('is no-op when audio context is absent', () => { | ||
const { mockOnClick } = renderWithClickListener( | ||
<ReadOnLoad>Bonjour!</ReadOnLoad> | ||
); | ||
|
||
screen.getByText('Bonjour!'); | ||
expect(mockOnClick).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('is no-op when audio playback is disabled', () => { | ||
const testHistory = createMemoryHistory(); | ||
|
||
const { mockOnClick } = renderWithClickListener( | ||
<UiStringsAudioContextProvider api={mockUiStringsApi}> | ||
<Router history={testHistory}> | ||
<TestContextConsumer /> | ||
<ReadOnLoad>Bonjour!</ReadOnLoad> | ||
</Router> | ||
</UiStringsAudioContextProvider> | ||
); | ||
|
||
screen.getByText('Bonjour!'); | ||
expect(audioContext?.isEnabled).toEqual(false); | ||
expect(mockOnClick).not.toHaveBeenCalled(); | ||
|
||
// Should still be a no-op for subsequent URL changes: | ||
act(() => testHistory.push('/new-url')); | ||
expect(mockOnClick).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers click actions on render', () => { | ||
const { mockOnClick } = renderWithClickListener( | ||
<UiStringsAudioContextProvider api={mockUiStringsApi}> | ||
<TestContextConsumer /> | ||
<ReadOnLoad>Bonjour!</ReadOnLoad> | ||
<div>Comment allez-vous?</div> | ||
</UiStringsAudioContextProvider> | ||
); | ||
|
||
screen.getByText('Bonjour!'); | ||
expect(mockOnClick).not.toHaveBeenCalled(); | ||
|
||
mockOnClick.mockImplementation((event: MouseEvent) => { | ||
assert(event.target instanceof HTMLElement); | ||
expect(event.target.textContent).toEqual('Bonjour!'); | ||
}); | ||
|
||
act(() => audioContext?.setIsEnabled(true)); | ||
|
||
expect(mockOnClick).toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers click event on URL change', () => { | ||
const testHistory = createMemoryHistory(); | ||
testHistory.push('/contests/1'); | ||
|
||
const { mockOnClick } = renderWithClickListener( | ||
<UiStringsAudioContextProvider api={mockUiStringsApi}> | ||
<Router history={testHistory}> | ||
<TestContextConsumer /> | ||
<ReadOnLoad> | ||
<div> | ||
<Route path="/contests/1">President</Route> | ||
<Route path="/contests/2">Mayor</Route> | ||
</div> | ||
<div>Vote for 1</div> | ||
</ReadOnLoad> | ||
<div>Candidate 1</div> | ||
</Router> | ||
</UiStringsAudioContextProvider> | ||
); | ||
|
||
screen.getByText('President'); | ||
expect(mockOnClick).not.toHaveBeenCalled(); | ||
|
||
mockOnClick.mockImplementation((event: MouseEvent) => { | ||
assert(event.target instanceof HTMLElement); | ||
expect(event.target.textContent).toMatch(/^President.?Vote for 1$/); | ||
}); | ||
|
||
act(() => audioContext?.setIsEnabled(true)); | ||
|
||
expect(mockOnClick).toHaveBeenCalled(); | ||
|
||
mockOnClick.mockReset(); | ||
mockOnClick.mockImplementation((event: MouseEvent) => { | ||
assert(event.target instanceof HTMLElement); | ||
expect(event.target.textContent).toMatch(/^Mayor.?Vote for 1$/); | ||
}); | ||
|
||
act(() => testHistory.push('/contests/2')); | ||
|
||
screen.getByText('Mayor'); | ||
expect(mockOnClick).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { useAudioContext } from './audio_context'; | ||
|
||
export interface ReadOnLoadProps { | ||
children: React.ReactNode; | ||
className?: string; | ||
} | ||
|
||
/** | ||
* Returns the react-router location state if a react-router context exists, | ||
* without failing if there isn't one available. | ||
* | ||
* Allows us to conditionally use router-aware logic in {@link ReadOnLoad} | ||
* without requiring a react-router context. | ||
*/ | ||
function useLocationIfAvailable() { | ||
try { | ||
return useLocation(); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* On initial render, this triggers an audio read-out of any descendant | ||
* `UiString` elements rendered within (in order of appearance in the DOM), if | ||
* audio playback is enabled. | ||
* | ||
* Re-triggers a read-out on subsequent route changes, if applicable, or whenever | ||
* audio playback is newly enabled. | ||
* | ||
* NOTE: Intended for use as a single instance on any given app screen. If | ||
* multiple instances are present on screen, audio will be played only for | ||
* content within the last `ReadOnLoad` instance. | ||
*/ | ||
export function ReadOnLoad(props: ReadOnLoadProps): JSX.Element { | ||
const { children, className } = props; | ||
|
||
const location = useLocationIfAvailable(); | ||
const currentUrl = location?.pathname; | ||
|
||
const audioContext = useAudioContext(); | ||
const isAudioEnabled = audioContext?.isEnabled; | ||
|
||
const containerRef = React.useRef<HTMLDivElement>(null); | ||
|
||
React.useEffect(() => { | ||
if (!containerRef.current || !isAudioEnabled) { | ||
return; | ||
} | ||
|
||
containerRef.current.focus(); | ||
containerRef.current.click(); | ||
}, [currentUrl, isAudioEnabled]); | ||
|
||
return ( | ||
<div className={className} ref={containerRef}> | ||
{children} | ||
</div> | ||
); | ||
} |