1
- import { describe , expect , it } from "vitest"
1
+ import { describe , expect , it , vi , beforeEach } from "vitest"
2
2
import { setActivePinia , createPinia } from "~~/test/unit/test-utils/pinia"
3
3
4
4
import { AUDIO } from "#shared/constants/media"
5
+ import { audioErrorMessages } from "#shared/constants/audio"
6
+ import { warn } from "~/utils/console"
5
7
import { useActiveMediaStore } from "~/stores/active-media"
6
8
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
+
7
21
const initialState = { type : null , id : null , status : "ejected" , message : null }
8
22
const statuses = [ "ejected" , "paused" , "playing" ]
9
23
10
24
describe ( "Active Media Store" , ( ) => {
11
25
beforeEach ( ( ) => {
12
26
setActivePinia ( createPinia ( ) )
13
27
} )
28
+
14
29
describe ( "state" , ( ) => {
15
30
it ( "sets initial filters to filterData" , ( ) => {
16
31
const activeMediaStore = useActiveMediaStore ( )
@@ -20,6 +35,7 @@ describe("Active Media Store", () => {
20
35
expect ( activeMediaStore . message ) . toEqual ( initialState . message )
21
36
} )
22
37
} )
38
+
23
39
describe ( "actions" , ( ) => {
24
40
it . each ( statuses ) ( `can set active media with status $status` , ( status ) => {
25
41
const activeMediaStore = useActiveMediaStore ( )
@@ -39,6 +55,7 @@ describe("Active Media Store", () => {
39
55
40
56
expect ( activeMediaStore . status ) . toBe ( "paused" )
41
57
} )
58
+
42
59
it ( "can eject an item" , ( ) => {
43
60
const activeMediaStore = useActiveMediaStore ( )
44
61
@@ -53,11 +70,86 @@ describe("Active Media Store", () => {
53
70
expect ( activeMediaStore . type ) . toEqual ( initialState . type )
54
71
expect ( activeMediaStore . status ) . toEqual ( initialState . status )
55
72
} )
73
+
56
74
it ( "can set a message" , ( ) => {
57
75
const activeMediaStore = useActiveMediaStore ( )
58
76
const expectedMessage = "Cannot play this audio"
59
77
activeMediaStore . setMessage ( { message : expectedMessage } )
60
78
expect ( activeMediaStore . message ) . toEqual ( expectedMessage )
61
79
} )
62
80
} )
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
+ } )
63
155
} )
0 commit comments