Skip to content

Commit

Permalink
Merge pull request #238 from newscorp-ghfb/feat/PPDSC-1961-initial-st…
Browse files Browse the repository at this point in the history
…art-point-audioPlayer

feat(PPDSC-2182): added initialTime prop and test
  • Loading branch information
agagotowiec authored Jun 30, 2022
2 parents 4407a7f + f6cfed9 commit 6518393
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ exports[`Audio Player Composable renders with TimeDisplay label overrides 1`] =
</button>
<span
class="emotion-4"
data-testid="audio-player-time-display"
>
00:00/00:00
</span>
Expand Down Expand Up @@ -2357,6 +2358,7 @@ exports[`Audio Player Composable seekBar should renders with overrides 1`] = `
</button>
<span
class="emotion-9"
data-testid="audio-player-time-display"
>
00:00/00:00
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,58 @@ export const AudioSubComponents = () => (
);
AudioSubComponents.storyName = 'audio-sub-components';

export const AudioPlayerWithInitialProps = () => {
const breakpointKey = useBreakpointKey();
return (
<AudioPlayerComposable src={AUDIO_SRC} initialTime={50} initialVolume={0.2}>
<GridLayout
columns={{
xs: '1fr auto auto auto 1fr',
md: '50px 1fr auto auto auto 1fr 50px',
}}
rowGap="space040"
columnGap="space040"
areas={{
xs: fullAudioPlayerAreasMobile,
md: fullAudioPlayerAreasDesktop,
}}
>
{Areas => (
<>
<Areas.Play alignSelf="center">
<AudioPlayerPlayPauseButton />
</Areas.Play>

<Areas.Volume alignSelf="center" justifySelf="start">
<AudioPlayerVolumeControl
collapsed={breakpointKey === 'xs' || breakpointKey === 'sm'}
/>
</Areas.Volume>

<Areas.SeekBar>
<AudioPlayerSeekBar />
</Areas.SeekBar>

<Areas.CurrentTime>
<AudioPlayerTimeDisplay
format={({currentTime}) => calculateTime(currentTime)}
/>
</Areas.CurrentTime>

<Areas.TotalTime justifySelf="end">
<AudioPlayerTimeDisplay
format={({duration}) => calculateTime(duration)}
/>
</Areas.TotalTime>
</>
)}
</GridLayout>
</AudioPlayerComposable>
);
};

AudioPlayerWithInitialProps.storyName = 'audio-player-with-initial-props';

export const AudioPlayerOverrides = () => (
<ThemeProvider theme={myCustomTheme}>
<StorybookSubHeading>Audio player with overrides</StorybookSubHeading>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ const AudioPropsAndVolumeControlWithInitialVolumeCollapsed: AudioPlayerComposabl
),
};

const AudioPropsWithInitialTime: AudioPlayerComposableProps = {
initialTime: 50,
src: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
children: (
<>
<AudioPlayerTimeDisplay />
<AudioPlayerSeekBar />
</>
),
};

const AudioPropsAndVolumeControlVertical: AudioPlayerComposableProps = {
src: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
initialVolume: 0.2,
Expand Down Expand Up @@ -902,6 +913,25 @@ describe('Audio Player Composable', () => {
expect(asFragment()).toMatchSnapshot();
});
});
describe('initialTime prop', () => {
it('should render correctly with initial time', () => {
const {getByTestId} = renderWithTheme(
AudioPlayerComposable,
AudioPropsWithInitialTime,
);

const audioElement = getByTestId('audio-element') as HTMLAudioElement;
const audioTimeLabel = getByTestId(
'audio-player-time-display',
) as HTMLParagraphElement;

const seekBar = getByTestId('audio-slider-track') as HTMLDivElement;
fireEvent.timeUpdate(audioElement);
expect(audioElement.currentTime).toEqual(50);
expect(audioTimeLabel.innerHTML).toEqual('00:50/00:00 ');
expect(seekBar.getAttribute('values')).toEqual('50');
});
});

describe('Instrumentation tests', () => {
test('should raise "end" event when the track has ended', () => {
Expand Down
1 change: 1 addition & 0 deletions src/audio-player-composable/audio-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export const useAudioFunctions = ({
const onTimeUpdate = useCallback(
({target}: SyntheticEvent<HTMLAudioElement, Event>) => {
const eventTime = Math.floor((target as HTMLAudioElement).currentTime);

if (currentTimeRef.current !== eventTime) {
setCurrentTime(eventTime);

Expand Down
11 changes: 9 additions & 2 deletions src/audio-player-composable/audio-player-composable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const AudioPlayerComposable = ({
ariaLandmark,
keyboardShortcuts: keyboardShortcutsProp,
initialVolume = 0.7,
initialTime = 0,
...props
}: AudioPlayerComposableProps) => {
const currentTimeRef = useRef(0);
Expand All @@ -71,9 +72,15 @@ export const AudioPlayerComposable = ({
});

useEffect(() => {
// On render onTimeUpdate will be fired and initialTime will be set as a value for currentTime state.
// I can't set this one to the setCurrentTime state directly as the audioElement time
// will still be 0, currentTime will be overridden to 0 and the audio will start from 0
if (audioRef && audioRef.current) {
audioRef.current.currentTime = initialTime;
}
setCurrentTime(0);
setDisplayDuration(0);
}, [src]);
}, [src, initialTime]);

const {
audioEvents,
Expand Down Expand Up @@ -224,7 +231,7 @@ export const AudioPlayerComposable = ({
initialVolume,
muteButtonSize,
}),
[volume, onChangeVolumeSlider],
[volume, initialVolume, onChangeVolumeSlider],
);

const getSkipPreviousButtonProps = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const ThemelessTimeDisplay = ({
const {format: defaultFormat, currentTime, duration} = getTimeDisplayProps!();
const formatFn = typeof format === 'function' ? format : defaultFormat;
return (
<StyledTextBlock as="span" overrides={overrides} {...restProps}>
<StyledTextBlock
as="span"
overrides={overrides}
data-testid="audio-player-time-display"
{...restProps}
>
{formatFn({currentTime, duration})}
</StyledTextBlock>
);
Expand Down
1 change: 1 addition & 0 deletions src/audio-player-composable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface AudioPlayerComposableProps
jumpToEnd: string | string[];
};
initialVolume?: 0 | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1;
initialTime?: number;
}

export enum AudioEvents {
Expand Down

0 comments on commit 6518393

Please sign in to comment.