Skip to content

Commit 89a844b

Browse files
committed
test: add some ref and getter tests
1 parent e70cb45 commit 89a844b

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ coverage
5050
Network Trash Folder
5151
Temporary Items
5252
.apdisk
53+
54+
/tests/__screenshots__/

src/useMediaRecorder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ConfigurableNavigator } from '@vueuse/core'
2-
import type { MaybeRef } from 'vue'
2+
import type { MaybeRef, MaybeRefOrGetter } from 'vue'
33
import { computedWithControl, useSupported } from '@vueuse/core'
44
import { tryOnScopeDispose } from '@vueuse/shared'
55
import { defu } from 'defu'
@@ -9,11 +9,11 @@ interface UseMediaRecorderOptions extends ConfigurableNavigator {
99
/**
1010
* The constraints parameter is a MediaStreamConstraints object specifying the types of media to request, along with any requirements for each type.
1111
*/
12-
constraints?: MaybeRef<MediaStreamConstraints>
12+
constraints?: MaybeRefOrGetter<MediaStreamConstraints>
1313
/**
1414
* Options to pass to the MediaRecorder constructor.
1515
*/
16-
mediaRecorderOptions?: MaybeRef<MediaRecorderOptions>
16+
mediaRecorderOptions?: MaybeRefOrGetter<MediaRecorderOptions>
1717
/**
1818
* Callback when recording starts.
1919
*/

tests/index.spec.ts

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useMediaRecorder } from '@orbisk/vue-use-media-recorder'
22
import { describe, expect, it, vi } from 'vitest'
3+
import { ref } from 'vue'
34

45
describe('useMediaRecorder', () => {
56
it('state should be initially undefined', () => {
@@ -35,16 +36,16 @@ describe('useMediaRecorder', () => {
3536
})
3637
})
3738

38-
it('data should update when recording', async () => {
39+
it('data should update when recording with timeslice', async () => {
3940
const {
4041
start,
4142
data,
4243
} = useMediaRecorder({ constraints: { audio: true } })
4344
data.value = []
4445
expect(data.value?.length).toBe(0)
45-
await start(10)
46+
await start(1)
4647
await new Promise(resolve => setTimeout(resolve, 100))
47-
expect(data.value?.length).toBe(1)
48+
expect(data.value?.length).toBeGreaterThan(0)
4849
})
4950

5051
it('stream should be defined and active after start', async () => {
@@ -209,4 +210,89 @@ describe('useMediaRecorder', () => {
209210
expect(data.value.length).toBeGreaterThan(1)
210211
})
211212
})
213+
214+
it('should not record when paused', async () => {
215+
const {
216+
start,
217+
pause,
218+
data,
219+
} = useMediaRecorder({ constraints: {audio: true} })
220+
221+
await start(1)
222+
await vi.waitFor(() => {
223+
expect(data.value.length).toBeGreaterThan(0)
224+
})
225+
const length = data.value.length
226+
pause()
227+
await new Promise(resolve => setTimeout(resolve, 10))
228+
expect(data.value.length).toBe(length)
229+
})
230+
231+
it('should handle constraints as a getter function', async () => {
232+
const {
233+
start,
234+
stream,
235+
} = useMediaRecorder({ constraints: () => ({ audio: true }) })
236+
237+
expect(stream.value).toBeUndefined()
238+
await start()
239+
expect(stream.value).toBeDefined()
240+
})
241+
242+
it('should handle mediaRecorderOptions as a getter function', async () => {
243+
const {
244+
start,
245+
mimeType,
246+
} = useMediaRecorder({ constraints: { audio: true }, mediaRecorderOptions: () => ({ mimeType: 'audio/webm' }) })
247+
248+
expect(mimeType.value).toBeUndefined()
249+
await start()
250+
await vi.waitFor(() => {
251+
expect(mimeType.value).toBe('audio/webm')
252+
})
253+
})
254+
255+
it('should handle constraints as a ref', async () => {
256+
const constraints = ref({ audio: true })
257+
const {
258+
start,
259+
stream,
260+
} = useMediaRecorder({ constraints })
261+
262+
expect(stream.value).toBeUndefined()
263+
await start()
264+
expect(stream.value).toBeDefined()
265+
})
266+
267+
it('should handle mediaRecorderOptions as a ref', async () => {
268+
const mediaRecorderOptions = ref({ mimeType: 'audio/webm' })
269+
const {
270+
start,
271+
mimeType,
272+
} = useMediaRecorder({ constraints: { audio: true }, mediaRecorderOptions })
273+
274+
expect(mimeType.value).toBeUndefined()
275+
await start()
276+
await vi.waitFor(() => {
277+
expect(mimeType.value).toBe('audio/webm')
278+
})
279+
})
280+
it('should not change when constraints change', async () => {
281+
const constraints = ref({ audio: true })
282+
283+
const {
284+
start,
285+
stream,
286+
} = useMediaRecorder({ constraints })
287+
288+
expect(stream.value).toBeUndefined()
289+
await start()
290+
constraints.value = { audio: false }
291+
stop()
292+
await start()
293+
await vi.waitFor(() => {
294+
expect(stream.value).toBeDefined()
295+
})
296+
297+
})
212298
})

0 commit comments

Comments
 (0)