-
Notifications
You must be signed in to change notification settings - Fork 924
/
Copy pathutil.test.ts
204 lines (178 loc) · 6.04 KB
/
util.test.ts
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
203
204
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from 'chai';
import * as sinon from 'sinon';
import {
assertFails,
assertSucceeds,
withFunctionTriggersDisabled
} from '../src/util';
import { restoreEnvVars, stashEnvVars } from './test_utils';
describe('assertSucceeds()', () => {
it('returns a fulfilled promise iff success', async function () {
const success = Promise.resolve('success');
const failure = Promise.reject('failure');
await assertSucceeds(success).catch(() => {
throw new Error('Expected success to succeed.');
});
await assertSucceeds(failure)
.then(() => {
throw new Error('Expected failure to fail.');
})
.catch(() => {});
});
});
describe('assertFails()', () => {
it('returns a rejected promise if argument promise fulfills', async function () {
const success = Promise.resolve('success');
await assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});
});
it('returns a fulfilled promise if PERMISSION_DENIED', async function () {
const permissionDenied = Promise.reject({
message: 'PERMISSION_DENIED'
});
await assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});
});
it('returns a fulfilled promise if code is permission-denied', async function () {
const success = Promise.resolve('success');
const permissionDenied = Promise.reject({
code: 'permission-denied'
});
const otherFailure = Promise.reject('failure');
await assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});
await assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});
await assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});
it('resolve if code is PERMISSION_DENIED', async function () {
const success = Promise.resolve('success');
const permissionDenied = Promise.reject({
code: 'PERMISSION_DENIED'
});
const otherFailure = Promise.reject('failure');
await assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});
await assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});
await assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});
it('returns a fulfilled promise if message is Permission denied', async function () {
const success = Promise.resolve('success');
const permissionDenied = Promise.reject({
message: 'Permission denied'
});
const otherFailure = Promise.reject('failure');
await assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});
await assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});
await assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});
it('returns a fulfilled promise if message contains unauthorized', async function () {
const success = Promise.resolve('success');
const permissionDenied = Promise.reject({
message:
"User does not have permission to access 'file'. (storage/unauthorized)"
});
const otherFailure = Promise.reject('failure');
await assertFails(success)
.then(() => {
throw new Error('Expected success to fail.');
})
.catch(() => {});
await assertFails(permissionDenied).catch(() => {
throw new Error('Expected permissionDenied to succeed.');
});
await assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});
it('returns a rejected promise if argument promise rejects with other errors', async function () {
const otherFailure = Promise.reject('failure');
await assertFails(otherFailure)
.then(() => {
throw new Error('Expected otherFailure to fail.');
})
.catch(() => {});
});
});
describe('withFunctionTriggersDisabled()', () => {
it('disabling function triggers does not throw, returns value', async function () {
const fetchSpy = sinon.spy(globalThis, 'fetch');
const res = await withFunctionTriggersDisabled(() => {
return Promise.resolve(1234);
});
expect(res).to.eq(1234);
expect(fetchSpy.callCount).to.equal(2);
});
it('disabling function triggers always re-enables, event when the function throws', async function () {
const fetchSpy = sinon.spy(globalThis, 'fetch');
const res = withFunctionTriggersDisabled(() => {
throw new Error('I throw!');
});
await expect(res).to.eventually.be.rejectedWith('I throw!');
expect(fetchSpy.callCount).to.equal(2);
});
context('without env vars', () => {
beforeEach(() => {
stashEnvVars();
});
afterEach(() => {
restoreEnvVars();
});
it('throws if hub is not specified', async function () {
await expect(
withFunctionTriggersDisabled(() => {
return Promise.resolve(1234);
})
).to.rejectedWith(/specify the Emulator Hub host and port/);
});
});
});