-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.ts
More file actions
202 lines (180 loc) · 8.08 KB
/
Copy pathindex.test.ts
File metadata and controls
202 lines (180 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import type { MediaDevicesControl } from './index.js'
import {
allAccessGranted,
allAccessDenied,
anyCamera,
anyDevice,
anyMicrophone,
forgeMediaDevices,
RequestedMediaInput,
stillHaveToAskForDeviceAccess,
} from './index.js'
import './matchers/dom-exception.js'
import './matchers/to-include-video-track.js'
import './matchers/to-include-audio-track.js'
let control: MediaDevicesControl
afterEach(() => {
control.uninstall()
})
describe('MediaDevicesFake', () => {
describe('still have to ask for device access', () => {
beforeEach(() => {
control = forgeMediaDevices(stillHaveToAskForDeviceAccess())
control.installInto(window)
})
test('return a video stream from an attached camera after granting access', async () => {
control.attach(anyCamera({ label: 'The Camera' }))
const navigator = window.navigator
expect((await navigator.permissions.query({ name: 'camera' })).state).toBe('prompt')
const userMediaPromise = navigator.mediaDevices.getUserMedia({ video: true })
const permissionPrompt = await control.deviceAccessPrompt()
expect(permissionPrompt.requestedPermissions()).toEqual([RequestedMediaInput.Camera])
permissionPrompt.takeAction('allow')
expect((await userMediaPromise).getVideoTracks()[0].label).toEqual('The Camera')
expect((await navigator.permissions.query({ name: 'camera' })).state).toBe('granted')
// 2nd time should not need a permission prompt
expect(await navigator.mediaDevices.getUserMedia({ video: true })).toIncludeVideoTrack()
})
test('chain multiple permission prompts for different device types after each other', async () => {
control.attach(anyCamera(), anyMicrophone())
const navigator = window.navigator
const videoPromptPromise = control.deviceAccessPrompt()
const videoPromise = navigator.mediaDevices.getUserMedia({ video: true })
const audioPromise = navigator.mediaDevices.getUserMedia({ audio: true })
const videoPrompt = await videoPromptPromise
expect(videoPrompt.requestedPermissions()).toEqual([RequestedMediaInput.Camera])
videoPrompt.takeAction('allow')
expect(await videoPromise).toIncludeVideoTrack()
const audioPrompt = await control.deviceAccessPrompt()
expect(audioPrompt.requestedPermissions()).toEqual([RequestedMediaInput.Microphone])
await expect(audioPromise).rejects
})
test('resolve/reject pending user media request once consent is clear ', async () => {
control.attach(anyMicrophone())
const navigator = window.navigator
const willBeDismissed = navigator.mediaDevices.getUserMedia({ audio: true })
const willBeGranted = navigator.mediaDevices.getUserMedia({ audio: true })
const noMorePrompt = navigator.mediaDevices.getUserMedia({ audio: true })
const firstPrompt = await control.deviceAccessPrompt()
firstPrompt.takeAction('dismiss')
await expect(willBeDismissed).rejects.domException('Permission dismissed')
const secondPrompt = await control.deviceAccessPrompt()
secondPrompt.takeAction('allow')
expect(await willBeGranted).toIncludeAudioTrack()
expect(await noMorePrompt).toIncludeAudioTrack()
})
test('reject promise with a DomException after blocking access', async () => {
const { attach, deviceAccessPrompt } = control
attach(anyMicrophone())
const navigator = window.navigator
const userMediaPromise = navigator.mediaDevices.getUserMedia({ audio: true })
const permissionPrompt = await deviceAccessPrompt()
expect(permissionPrompt.requestedPermissions()).toEqual([RequestedMediaInput.Microphone])
permissionPrompt.takeAction('block')
await expect(userMediaPromise).rejects.domException('Permission denied', 'NotAllowedError')
expect((await navigator.permissions.query({ name: 'microphone' })).state).toBe('denied')
// 2nd time should not need a permission prompt
await expect(navigator.mediaDevices.getUserMedia({ audio: true })).rejects.domException(
'Permission denied',
'NotAllowedError',
)
})
})
describe('device access granted', () => {
beforeEach(() => {
control = forgeMediaDevices(allAccessGranted())
control.installInto(window)
})
test('return a video stream from an attached camera', async () => {
control.attach(anyCamera({ label: 'The Camera' }))
const userMedia = await window.navigator.mediaDevices.getUserMedia({ video: true })
expect(userMedia.getVideoTracks()[0].label).toEqual('The Camera')
})
test('reject media stream requests if no device is available', async () => {
const onlyMicrophone = anyMicrophone()
control.attach(onlyMicrophone)
control.remove(onlyMicrophone)
await expect(window.navigator.mediaDevices.getUserMedia({ audio: true })).rejects.domException(
'Requested device not found',
'NotFoundError',
)
})
})
describe('device access blocked', () => {
beforeEach(() => {
control = forgeMediaDevices(allAccessDenied())
control.installInto(window)
})
test('reject media stream requests', async () => {
await expect(window.navigator.mediaDevices.getUserMedia({ audio: true })).rejects.domException(
'Permission denied',
'NotAllowedError',
)
})
})
describe('attach', () => {
beforeEach(() => {
control = forgeMediaDevices(allAccessDenied())
control.installInto(window)
})
test('an array of devices', async () => {
control.attach([anyDevice()])
const devices = await window.navigator.mediaDevices.enumerateDevices()
expect(devices).toHaveLength(1)
})
})
})
describe('PermissionsFake', () => {
beforeEach(() => {
control = forgeMediaDevices(stillHaveToAskForDeviceAccess())
control.installInto(window)
})
describe('notify onchange listener on permission state change', () => {
test('for camera', async () => {
control.attach(anyCamera())
const navigator = window.navigator
const permissionStatus = await navigator.permissions.query({ name: 'camera' })
expect(permissionStatus.state).toEqual('prompt')
const onChange = jest.fn()
const onchange = jest.fn()
permissionStatus.onchange = onChange
permissionStatus.addEventListener('change', onchange)
navigator.mediaDevices.getUserMedia({ video: true })
const prompt = await control.deviceAccessPrompt()
prompt.takeAction('allow')
expect(onChange).toHaveBeenCalled()
expect(permissionStatus.state).toEqual('granted')
})
test('for microphone', async () => {
const navigator = window.navigator
control.attach(anyMicrophone())
const permissionStatus = await navigator.permissions.query({ name: 'microphone' })
expect(permissionStatus.state).toEqual('prompt')
const onChange = jest.fn()
const onchange = jest.fn()
permissionStatus.onchange = onChange
permissionStatus.addEventListener('change', onchange)
navigator.mediaDevices.getUserMedia({ audio: true }).catch(() => {})
const prompt = await control.deviceAccessPrompt()
prompt.takeAction('block')
expect(onChange).toHaveBeenCalled()
expect(onchange).toHaveBeenCalled()
expect(permissionStatus.state).toEqual('denied')
})
test('change permission after creation in one call', async () => {
control.setPermissionFor({ camera: 'granted', microphone: 'granted' })
expect(await permissionStateFor('camera')).toBe('granted')
expect(await permissionStateFor('microphone')).toBe('granted')
control.setPermissionFor({ camera: 'denied' })
expect(await permissionStateFor('camera')).toBe('denied')
expect(await permissionStateFor('microphone')).toBe('granted')
control.setPermissionFor({ microphone: 'denied' })
expect(await permissionStateFor('camera')).toBe('denied')
expect(await permissionStateFor('microphone')).toBe('denied')
})
})
})
const permissionStateFor = async (name: 'camera' | 'microphone') => {
const cameraPermission = await window.navigator.permissions.query({ name })
return cameraPermission.state
}