From 21349790abb4692d069d4102722f9c1bb32d690e Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Fri, 20 Jan 2023 14:07:21 +0100 Subject: [PATCH] Fix seekbar position for zero length audio --- src/utils/numbers.ts | 3 ++- .../views/audio_messages/SeekBar-test.tsx | 16 +++++++++++++++- .../__snapshots__/SeekBar-test.tsx.snap | 15 +++++++++++++++ test/test-utils/audio.ts | 3 ++- test/utils/numbers-test.ts | 4 ++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/utils/numbers.ts b/src/utils/numbers.ts index 180e4f79503..e2a89833fd6 100644 --- a/src/utils/numbers.ts +++ b/src/utils/numbers.ts @@ -38,5 +38,6 @@ export function percentageWithin(pct: 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; } diff --git a/test/components/views/audio_messages/SeekBar-test.tsx b/test/components/views/audio_messages/SeekBar-test.tsx index e957daf0fe7..44f6967c5a9 100644 --- a/test/components/views/audio_messages/SeekBar-test.tsx +++ b/test/components/views/audio_messages/SeekBar-test.tsx @@ -34,15 +34,29 @@ describe("SeekBar", () => { frameRequestCallback = callback; return 0; }); - playback = createTestPlayback(); }); afterEach(() => { mocked(window.requestAnimationFrame).mockRestore(); }); + describe("when rendering a SeekBar for an empty playback", () => { + beforeEach(() => { + playback = createTestPlayback({ + durationSeconds: 0, + timeSeconds: 0, + }); + renderResult = render(); + }); + + it("should render correctly", () => { + expect(renderResult.container).toMatchSnapshot(); + }); + }); + describe("when rendering a SeekBar", () => { beforeEach(() => { + playback = createTestPlayback(); renderResult = render(); }); diff --git a/test/components/views/audio_messages/__snapshots__/SeekBar-test.tsx.snap b/test/components/views/audio_messages/__snapshots__/SeekBar-test.tsx.snap index 8c66fda4d7f..866fc657084 100644 --- a/test/components/views/audio_messages/__snapshots__/SeekBar-test.tsx.snap +++ b/test/components/views/audio_messages/__snapshots__/SeekBar-test.tsx.snap @@ -15,6 +15,21 @@ exports[`SeekBar when rendering a SeekBar and the playback proceeds should rende `; +exports[`SeekBar when rendering a SeekBar for an empty playback should render correctly 1`] = ` +
+ +
+`; + exports[`SeekBar when rendering a SeekBar should render the initial position 1`] = `
{ +export const createTestPlayback = (overrides: Partial = {}): Playback => { const eventEmitter = new EventEmitter(); return { @@ -63,6 +63,7 @@ export const createTestPlayback = (): Playback => { liveData: new SimpleObservable(), durationSeconds: 31415, timeSeconds: 3141, + ...overrides, } as PublicInterface as Playback; }; diff --git a/test/utils/numbers-test.ts b/test/utils/numbers-test.ts index 34ffaaa4eee..cfd594d5d0a 100644 --- a/test/utils/numbers-test.ts +++ b/test/utils/numbers-test.ts @@ -160,5 +160,9 @@ describe("numbers", () => { const result = percentageOf(14.28, 10.2, 20.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); + }); }); });