2022-11-02 08:46:42 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-02 08:46:42 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-11-02 08:46:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { createRef, RefObject } from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { act, fireEvent, render, RenderResult } from "jest-matrix-react";
|
2022-11-02 08:46:42 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { Playback } from "../../../../../src/audio/Playback";
|
|
|
|
import { createTestPlayback } from "../../../../test-utils/audio";
|
|
|
|
import SeekBar from "../../../../../src/components/views/audio_messages/SeekBar";
|
2022-11-02 08:46:42 +00:00
|
|
|
|
|
|
|
describe("SeekBar", () => {
|
|
|
|
let playback: Playback;
|
|
|
|
let renderResult: RenderResult;
|
|
|
|
let frameRequestCallback: FrameRequestCallback;
|
|
|
|
let seekBarRef: RefObject<SeekBar>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
seekBarRef = createRef();
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(window, "requestAnimationFrame").mockImplementation((callback: FrameRequestCallback) => {
|
|
|
|
frameRequestCallback = callback;
|
|
|
|
return 0;
|
|
|
|
});
|
2022-11-02 08:46:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mocked(window.requestAnimationFrame).mockRestore();
|
|
|
|
});
|
|
|
|
|
2023-01-23 16:28:31 +00:00
|
|
|
describe("when rendering a SeekBar for an empty playback", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
playback = createTestPlayback({
|
|
|
|
durationSeconds: 0,
|
|
|
|
timeSeconds: 0,
|
|
|
|
});
|
|
|
|
renderResult = render(<SeekBar ref={seekBarRef} playback={playback} />);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render correctly", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-02 08:46:42 +00:00
|
|
|
describe("when rendering a SeekBar", () => {
|
2022-12-22 14:30:42 +00:00
|
|
|
beforeEach(() => {
|
2023-01-23 16:28:31 +00:00
|
|
|
playback = createTestPlayback();
|
2022-11-02 08:46:42 +00:00
|
|
|
renderResult = render(<SeekBar ref={seekBarRef} playback={playback} />);
|
|
|
|
});
|
|
|
|
|
2022-12-22 14:30:42 +00:00
|
|
|
it("should render the initial position", () => {
|
2022-11-02 08:46:42 +00:00
|
|
|
// expected value 3141 / 31415 ~ 0.099984084
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-22 14:30:42 +00:00
|
|
|
describe("and the playback proceeds", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
// @ts-ignore
|
|
|
|
playback.timeSeconds = 6969;
|
|
|
|
act(() => {
|
|
|
|
playback.liveData.update([playback.timeSeconds, playback.durationSeconds]);
|
|
|
|
frameRequestCallback(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render as expected", () => {
|
|
|
|
// expected value 6969 / 31415 ~ 0.221836702
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-02 08:46:42 +00:00
|
|
|
describe("and seeking position with the slider", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const rangeInput = renderResult.container.querySelector("[type='range']");
|
|
|
|
act(() => {
|
2022-12-22 14:30:42 +00:00
|
|
|
fireEvent.change(rangeInput!, { target: { value: 0.5 } });
|
2022-11-02 08:46:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the playback", () => {
|
|
|
|
expect(playback.skipTo).toHaveBeenCalledWith(0.5 * playback.durationSeconds);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and seeking left", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(playback.skipTo).mockClear();
|
|
|
|
act(() => {
|
2022-12-22 14:30:42 +00:00
|
|
|
seekBarRef.current!.left();
|
2022-11-02 08:46:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should skip to minus 5 seconds", () => {
|
|
|
|
expect(playback.skipTo).toHaveBeenCalledWith(playback.timeSeconds - 5);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and seeking right", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(playback.skipTo).mockClear();
|
|
|
|
act(() => {
|
2022-12-22 14:30:42 +00:00
|
|
|
seekBarRef.current!.right();
|
2022-11-02 08:46:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should skip to plus 5 seconds", () => {
|
|
|
|
expect(playback.skipTo).toHaveBeenCalledWith(playback.timeSeconds + 5);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when rendering a disabled SeekBar", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
renderResult = render(<SeekBar disabled={true} playback={playback} />);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render as expected", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|