Skip to content

Commit 57f364e

Browse files
committed
Fix unit tests
unit
1 parent 41b42ac commit 57f364e

File tree

4 files changed

+96
-32
lines changed

4 files changed

+96
-32
lines changed

frontend/src/stores/active-media.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { useNuxtApp } from "#imports"
2+
13
import { defineStore } from "pinia"
2-
import { useNuxtApp } from "#app"
34

45
import type { SupportedMediaType } from "#shared/constants/media"
56
import { audioErrorMessages } from "#shared/constants/audio"
@@ -74,9 +75,6 @@ export const useActiveMediaStore = defineStore(ACTIVE_MEDIA, {
7475
this.message = message
7576
},
7677
playAudio(audio: HTMLAudioElement | undefined) {
77-
const { $captureException, $captureMessage } = useNuxtApp()
78-
console.log("sentry in playAudio", $captureException)
79-
$captureMessage("test")
8078
const playPromise = audio?.play()
8179
// Check if the audio can be played successfully
8280
if (playPromise === undefined) {

frontend/test/unit/specs/components/AudioTrack/v-audio-track.spec.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ const stubs = {
2929
RouterLink: RouterLinkStub,
3030
}
3131

32-
const captureExceptionMock = vi.fn()
33-
34-
vi.mock("#app", async () => {
35-
const original = await import("#app")
36-
return {
37-
...original,
38-
useNuxtApp: vi.fn(() => ({
39-
$sentry: {
40-
captureException: captureExceptionMock,
41-
},
42-
})),
43-
}
44-
})
45-
4632
describe("AudioTrack", () => {
4733
let options = null
4834
let props = null
@@ -109,7 +95,6 @@ describe("AudioTrack", () => {
10995
${"NotAllowedError"} | ${/Reproduction not allowed./i}
11096
${"NotSupportedError"} | ${/This audio format is not supported by your browser./i}
11197
${"AbortError"} | ${/You aborted playback./i}
112-
${"UnknownError"} | ${/An unexpected error has occurred./i}
11398
`(
11499
"on play error displays a message instead of the waveform",
115100
async ({ errorType, errorText }) => {
@@ -139,15 +124,6 @@ describe("AudioTrack", () => {
139124
expect(playStub).toHaveBeenCalledTimes(1)
140125
expect(pauseStub).toHaveBeenCalledTimes(1)
141126
expect(getByText(errorText)).toBeVisible()
142-
143-
// Only the UnknownError should be sent to Sentry.
144-
if (errorType === "UnknownError") {
145-
// eslint-disable-next-line vitest/no-conditional-expect
146-
expect(captureExceptionMock).toHaveBeenCalledWith(playError)
147-
} else {
148-
// eslint-disable-next-line vitest/no-conditional-expect
149-
expect(captureExceptionMock).not.toHaveBeenCalled()
150-
}
151127
}
152128
)
153129

frontend/test/unit/specs/stores/active-media-store.spec.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
import { describe, expect, it } from "vitest"
1+
import { describe, expect, it, vi, beforeEach } from "vitest"
22
import { setActivePinia, createPinia } from "~~/test/unit/test-utils/pinia"
33

44
import { AUDIO } from "#shared/constants/media"
5+
import { audioErrorMessages } from "#shared/constants/audio"
6+
import { warn } from "~/utils/console"
57
import { useActiveMediaStore } from "~/stores/active-media"
68

9+
const mockCaptureException = vi.fn()
10+
11+
vi.mock("#app/nuxt", () => ({
12+
useNuxtApp: () => ({
13+
$captureException: mockCaptureException,
14+
}),
15+
}))
16+
17+
vi.mock("~/utils/console", () => ({
18+
warn: vi.fn(),
19+
}))
20+
721
const initialState = { type: null, id: null, status: "ejected", message: null }
822
const statuses = ["ejected", "paused", "playing"]
923

1024
describe("Active Media Store", () => {
1125
beforeEach(() => {
1226
setActivePinia(createPinia())
1327
})
28+
1429
describe("state", () => {
1530
it("sets initial filters to filterData", () => {
1631
const activeMediaStore = useActiveMediaStore()
@@ -20,6 +35,7 @@ describe("Active Media Store", () => {
2035
expect(activeMediaStore.message).toEqual(initialState.message)
2136
})
2237
})
38+
2339
describe("actions", () => {
2440
it.each(statuses)(`can set active media with status $status`, (status) => {
2541
const activeMediaStore = useActiveMediaStore()
@@ -39,6 +55,7 @@ describe("Active Media Store", () => {
3955

4056
expect(activeMediaStore.status).toBe("paused")
4157
})
58+
4259
it("can eject an item", () => {
4360
const activeMediaStore = useActiveMediaStore()
4461

@@ -53,11 +70,86 @@ describe("Active Media Store", () => {
5370
expect(activeMediaStore.type).toEqual(initialState.type)
5471
expect(activeMediaStore.status).toEqual(initialState.status)
5572
})
73+
5674
it("can set a message", () => {
5775
const activeMediaStore = useActiveMediaStore()
5876
const expectedMessage = "Cannot play this audio"
5977
activeMediaStore.setMessage({ message: expectedMessage })
6078
expect(activeMediaStore.message).toEqual(expectedMessage)
6179
})
6280
})
81+
82+
describe("playAudio", () => {
83+
it("should handle undefined audio element", () => {
84+
const activeMediaStore = useActiveMediaStore()
85+
86+
const playFn = vi.fn().mockReturnValue(undefined)
87+
const mockAudio = {
88+
play: playFn,
89+
}
90+
91+
activeMediaStore.playAudio(mockAudio)
92+
93+
expect(warn).toHaveBeenCalledWith("Play promise is undefined")
94+
})
95+
96+
it("should handle successful play", async () => {
97+
const activeMediaStore = useActiveMediaStore()
98+
const playFn = vi.fn().mockResolvedValue()
99+
const mockAudio = {
100+
play: playFn,
101+
pause: vi.fn(),
102+
}
103+
104+
activeMediaStore.playAudio(mockAudio)
105+
106+
await vi.waitFor(() => {
107+
expect(playFn).toHaveBeenCalled()
108+
expect(mockAudio.pause).not.toHaveBeenCalled()
109+
expect(activeMediaStore.message).toBeNull()
110+
})
111+
})
112+
113+
it.each(Object.keys(audioErrorMessages))(
114+
"should handle known error: %s",
115+
async (errorName) => {
116+
const activeMediaStore = useActiveMediaStore()
117+
const error = new DOMException("Msg", errorName)
118+
const playFn = vi.fn().mockRejectedValue(error)
119+
const mockAudio = {
120+
play: playFn,
121+
pause: vi.fn(),
122+
}
123+
124+
activeMediaStore.playAudio(mockAudio)
125+
126+
await vi.waitFor(() => {
127+
expect(playFn).toHaveBeenCalled()
128+
expect(mockAudio.pause).toHaveBeenCalled()
129+
expect(activeMediaStore.message).toBe(audioErrorMessages[errorName])
130+
expect(mockCaptureException).not.toHaveBeenCalled()
131+
})
132+
}
133+
)
134+
135+
it("should handle unknown error", async () => {
136+
const activeMediaStore = useActiveMediaStore()
137+
const unknownError = new Error()
138+
unknownError.name = "UnknownError"
139+
const playFn = vi.fn().mockRejectedValue(unknownError)
140+
const mockAudio = {
141+
play: playFn,
142+
pause: vi.fn(),
143+
}
144+
145+
activeMediaStore.playAudio(mockAudio)
146+
147+
await vi.waitFor(() => {
148+
expect(playFn).toHaveBeenCalled()
149+
expect(mockAudio.pause).toHaveBeenCalled()
150+
expect(activeMediaStore.message).toBe("err_unknown")
151+
expect(mockCaptureException).toHaveBeenCalledWith(unknownError)
152+
})
153+
})
154+
})
63155
})

frontend/test/unit/specs/stores/media-store-fetching.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ vi.mock("#app/nuxt", async () => {
5252
...original,
5353
useRuntimeConfig: vi.fn(() => ({ public: { deploymentEnv: "staging" } })),
5454
useNuxtApp: vi.fn(() => ({
55-
$sentry: {
56-
captureException: vi.fn(),
57-
},
55+
$captureException: vi.fn(),
5856
$sendCustomEvent: vi.fn(),
5957
$processFetchingError: vi.fn(),
6058
})),

0 commit comments

Comments
 (0)