Fix seekbar position for zero length audio (#9949)

This commit is contained in:
Michael Weimann 2023-01-23 17:28:31 +01:00 committed by GitHub
parent 51b4555106
commit ed06ed0185
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 3 deletions

View file

@ -38,5 +38,6 @@ export function percentageWithin(pct: number, min: number, max: number): number
} }
export function percentageOf(val: number, min: number, max: number): number { export function percentageOf(val: number, min: number, max: number): number {
return (val - min) / (max - min); const percentage = (val - min) / (max - min);
return Number.isNaN(percentage) ? 0 : percentage;
} }

View file

@ -34,15 +34,29 @@ describe("SeekBar", () => {
frameRequestCallback = callback; frameRequestCallback = callback;
return 0; return 0;
}); });
playback = createTestPlayback();
}); });
afterEach(() => { afterEach(() => {
mocked(window.requestAnimationFrame).mockRestore(); mocked(window.requestAnimationFrame).mockRestore();
}); });
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();
});
});
describe("when rendering a SeekBar", () => { describe("when rendering a SeekBar", () => {
beforeEach(() => { beforeEach(() => {
playback = createTestPlayback();
renderResult = render(<SeekBar ref={seekBarRef} playback={playback} />); renderResult = render(<SeekBar ref={seekBarRef} playback={playback} />);
}); });

View file

@ -15,6 +15,21 @@ exports[`SeekBar when rendering a SeekBar and the playback proceeds should rende
</div> </div>
`; `;
exports[`SeekBar when rendering a SeekBar for an empty playback should render correctly 1`] = `
<div>
<input
class="mx_SeekBar"
max="1"
min="0"
step="0.001"
style="--fillTo: 0;"
tabindex="0"
type="range"
value="0"
/>
</div>
`;
exports[`SeekBar when rendering a SeekBar should render the initial position 1`] = ` exports[`SeekBar when rendering a SeekBar should render the initial position 1`] = `
<div> <div>
<input <input

View file

@ -22,7 +22,7 @@ import { PlaybackClock } from "../../src/audio/PlaybackClock";
import { UPDATE_EVENT } from "../../src/stores/AsyncStore"; import { UPDATE_EVENT } from "../../src/stores/AsyncStore";
import { PublicInterface } from "../@types/common"; import { PublicInterface } from "../@types/common";
export const createTestPlayback = (): Playback => { export const createTestPlayback = (overrides: Partial<Playback> = {}): Playback => {
const eventEmitter = new EventEmitter(); const eventEmitter = new EventEmitter();
return { return {
@ -63,6 +63,7 @@ export const createTestPlayback = (): Playback => {
liveData: new SimpleObservable<number[]>(), liveData: new SimpleObservable<number[]>(),
durationSeconds: 31415, durationSeconds: 31415,
timeSeconds: 3141, timeSeconds: 3141,
...overrides,
} as PublicInterface<Playback> as Playback; } as PublicInterface<Playback> as Playback;
}; };

View file

@ -160,5 +160,9 @@ describe("numbers", () => {
const result = percentageOf(14.28, 10.2, 20.4); const result = percentageOf(14.28, 10.2, 20.4);
expect(result).toBe(0.4); expect(result).toBe(0.4);
}); });
it("should return 0 for values that cause a division by zero", () => {
expect(percentageOf(0, 0, 0)).toBe(0);
});
}); });
}); });