Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

refactor: use timeFmt for aria-valuetext in useSeekable (#5671)

Ashish Vermac6fbe221c143
diff --git a/frontend/src/composables/use-seekable.ts b/frontend/src/composables/use-seekable.ts
index c17cad8d3..e6e158abb 100644
--- a/frontend/src/composables/use-seekable.ts
+++ b/frontend/src/composables/use-seekable.ts
@@ -1,7 +1,7 @@
-import { useI18n } from "#imports"
import { computed, ToRefs, ref } from "vue"
import { keycodes } from "#shared/constants/key-codes"
+import { timeFmt } from "#shared/utils/time-fmt"
export interface UseSeekableOptions
extends ToRefs<{
@@ -22,15 +22,11 @@ export const useSeekable = ({
onSeek,
onTogglePlayback,
}: UseSeekableOptions) => {
- const { t } = useI18n({ useScope: "global" })
-
const attributes = computed(() => ({
"aria-role": "slider",
"aria-valuemax": duration.value,
"aria-valuenow": currentTime.value,
- "aria-valuetext": t("waveform.currentTime", {
- time: currentTime.value,
- }),
+ "aria-valuetext": timeFmt(currentTime.value),
"aria-orientation": "horizontal" as const,
"aria-valuemin": "0",
}))
diff --git a/frontend/test/unit/specs/composables/use-seekable.spec.ts b/frontend/test/unit/specs/composables/use-seekable.spec.ts
new file mode 100644
index 000000000..b1aba6918
--- /dev/null
+++ b/frontend/test/unit/specs/composables/use-seekable.spec.ts
@@ -0,0 +1,75 @@
+import { ref } from "vue"
+
+import { describe, expect, it, vi } from "vitest"
+
+import { useSeekable } from "~/composables/use-seekable"
+
+describe("useSeekable", () => {
+ const setup = (time = 0) => {
+ const currentTime = ref(time)
+ const duration = ref(60.116463)
+ const onSeek = vi.fn()
+ const seekable = useSeekable({
+ currentTime,
+ duration,
+ isReady: ref(true),
+ isSeekable: ref(true),
+ onSeek,
+ onTogglePlayback: vi.fn(),
+ })
+
+ return { ...seekable, currentTime, duration, onSeek }
+ }
+
+ it.each<[number, string]>([
+ [0, "0:00"],
+ [1, "0:01"],
+ [1.999999, "0:01"],
+ [60.116463, "1:00"],
+ [59.116463, "0:59"],
+ [3600, "1:00:00"],
+ ])("formats %s seconds as %s for the announced value", (time, expected) => {
+ const { attributes } = setup(time)
+
+ expect(attributes.value["aria-valuetext"]).toBe(expected)
+ })
+
+ it("updates the announced value when the playback position changes", () => {
+ const { attributes, currentTime } = setup()
+
+ expect(attributes.value["aria-valuetext"]).toBe("0:00")
+
+ currentTime.value = 60.116463
+ expect(attributes.value["aria-valuetext"]).toBe("1:00")
+
+ currentTime.value = 59.116463
+ expect(attributes.value["aria-valuetext"]).toBe("0:59")
+ })
+
+ it("preserves precision in numeric ARIA values and seek fractions", () => {
+ const { attributes, meta, currentTime, duration } = setup(59.116463)
+
+ expect(attributes.value["aria-valuenow"]).toBe(currentTime.value)
+ expect(attributes.value["aria-valuemax"]).toBe(duration.value)
+ expect(meta.currentFrac.value).toBe(currentTime.value / duration.value)
+ })
+
+ it.each<[string, number]>([
+ ["Home", 0],
+ ["End", 1],
+ ["ArrowLeft", 58.116463 / 60.116463],
+ ["ArrowRight", 60.116463 / 60.116463],
+ ])("preserves seeking with %s", (key, expectedFraction) => {
+ const { listeners, onSeek } = setup(59.116463)
+ const event = new KeyboardEvent("keydown", {
+ key,
+ cancelable: true,
+ })
+
+ listeners.keydown(event)
+
+ expect(event.defaultPrevented).toBe(true)
+ expect(onSeek).toHaveBeenCalledOnce()
+ expect(onSeek.mock.calls[0][0]).toBeCloseTo(expectedFraction)
+ })
+})