Skip to content

Commit

Permalink
feat: AudioPlayer tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
mythter committed Aug 9, 2024
1 parent 85a9a23 commit a22c9b8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
7 changes: 7 additions & 0 deletions __mocks__/@stores/root-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export const useModalContext = () => ({
},
});

export const useAudioContext = () => ({
audio: {
base64: 'base64Mock',
mimeType: 'audio/mpeg',
},
});

export const useMobx = () => ({
newsStore: {
updateNews: mockUpdateNews,
Expand Down
121 changes: 121 additions & 0 deletions src/app/common/components/AudioPlayer/AudioPlayer.component.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { act } from 'react-dom/test-utils';
import {
fireEvent,
render,
screen,
} from '@testing-library/react';
import user from '@testing-library/user-event';

import '@testing-library/jest-dom';

import { AudioPlayer } from './AudioPlayer.component';

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: unknown) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => {},
}),
});

jest.mock('@images/audio-player/PauseBtn.webp', () => 'test-file-stub');
jest.mock('@images/audio-player/PlayBtn.webp', () => 'test-file-stub');

global.HTMLMediaElement.prototype.play = jest.fn();
global.HTMLMediaElement.prototype.pause = jest.fn();
global.HTMLMediaElement.prototype.addEventListener = jest.fn((event, callback) => {
if (event === 'ended') {
setTimeout(() => {
if (typeof callback === 'function') {
callback(new Event('ended'));
}
}, 0);
}
});

describe('AudioPlayer test', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should be rendered', async () => {
const { container } = await render(<AudioPlayer />);

const audio = container.getElementsByTagName('audio');
expect(audio).toHaveLength(1);

const audioSlider = screen.getByRole('slider');
expect(audioSlider).toBeInTheDocument();

const buttonPlayPause = container.getElementsByClassName('buttonContainer');
expect(buttonPlayPause).toHaveLength(1);

const icon = buttonPlayPause[0].getElementsByTagName('img');
expect(icon).toHaveLength(1);
});

it('should immediately play', async () => {
const { container } = await render(<AudioPlayer immediatelyPlay />);

const audio = container.getElementsByTagName('audio')[0];
const icon = container.getElementsByTagName('img')[0];

expect(audio.play).toHaveBeenCalled();
expect(icon.alt).toBe('Пауза');
});

it('should not immediately play', async () => {
const { container } = await render(<AudioPlayer />);

const audio = container.getElementsByTagName('audio')[0];
const icon = container.getElementsByTagName('img')[0];

expect(audio.play).not.toHaveBeenCalled();
expect(icon.alt).toBe('Програти');
});

it('should play/pause audio and toggle button icon', async () => {
const { container } = await render(<AudioPlayer />);

const audio = container.getElementsByTagName('audio')[0];
const buttonPlayPause = container.getElementsByClassName('buttonContainer')[0];
const icon = buttonPlayPause.getElementsByTagName('img')[0];

act(() => {
user.click(icon);
});

expect(audio.play).toHaveBeenCalled();
expect(icon.alt).toBe('Пауза');

act(() => {
user.click(icon);
});

expect(audio.pause).toHaveBeenCalled();
expect(icon.alt).toBe('Програти');
});

it('should change slider and audio position', async () => {
const { container } = await render(<AudioPlayer />);

const audio = container.getElementsByTagName('audio')[0];
const audioSlider = screen.getByRole('slider') as HTMLInputElement;

expect(audio.currentTime).toBe(0);
expect(audioSlider.value).toBe('0');

act(() => {
fireEvent.change(audioSlider, { target: { value: '50' } });
});

expect(audio.currentTime).toBe(50);
expect(audioSlider.value).toBe('50');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const AudioPlayer: React.FC<{ immediatelyPlay?: boolean }> = ({ immediatelyPlay

useEffect(() => {
resetAudio();
if (!isPlaying) {
if (!isPlaying && immediatelyPlay) {
play();
}
}, [audioState]);
Expand Down

0 comments on commit a22c9b8

Please sign in to comment.