|
1 | 1 | import { useMediaRecorder } from '@orbisk/vue-use-media-recorder'
|
2 | 2 | import { describe, expect, it, vi } from 'vitest'
|
| 3 | +import { ref } from 'vue' |
3 | 4 |
|
4 | 5 | describe('useMediaRecorder', () => {
|
5 | 6 | it('state should be initially undefined', () => {
|
@@ -35,16 +36,16 @@ describe('useMediaRecorder', () => {
|
35 | 36 | })
|
36 | 37 | })
|
37 | 38 |
|
38 |
| - it('data should update when recording', async () => { |
| 39 | + it('data should update when recording with timeslice', async () => { |
39 | 40 | const {
|
40 | 41 | start,
|
41 | 42 | data,
|
42 | 43 | } = useMediaRecorder({ constraints: { audio: true } })
|
43 | 44 | data.value = []
|
44 | 45 | expect(data.value?.length).toBe(0)
|
45 |
| - await start(10) |
| 46 | + await start(1) |
46 | 47 | await new Promise(resolve => setTimeout(resolve, 100))
|
47 |
| - expect(data.value?.length).toBe(1) |
| 48 | + expect(data.value?.length).toBeGreaterThan(0) |
48 | 49 | })
|
49 | 50 |
|
50 | 51 | it('stream should be defined and active after start', async () => {
|
@@ -209,4 +210,89 @@ describe('useMediaRecorder', () => {
|
209 | 210 | expect(data.value.length).toBeGreaterThan(1)
|
210 | 211 | })
|
211 | 212 | })
|
| 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 | + }) |
212 | 298 | })
|
0 commit comments