-
Notifications
You must be signed in to change notification settings - Fork 798
/
test-normalizeHandler.mjs
79 lines (63 loc) 路 3.01 KB
/
test-normalizeHandler.mjs
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
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {expect} from 'chai';
import expectError from '../../../../infra/testing/expectError.js';
import {normalizeHandler} from '../../../../packages/workbox-routing/utils/normalizeHandler.mjs';
const handler = {
handle: () => {},
};
const functionHandler = () => {};
const invalidHandlerObject = {};
const invalidHandlerString = 'INVALID';
describe(`workbox-routing: utils/normalizeHandler`, function() {
it(`should properly normalize an object that exposes a handle method in dev`, async function() {
if (process.env.NODE_ENV == 'production') return this.skip();
const normalizedHandler = normalizeHandler(handler);
expect(normalizedHandler).to.have.property('handle');
});
it(`should properly normalize a function in dev`, async function() {
if (process.env.NODE_ENV == 'production') return this.skip();
const normalizedHandler = normalizeHandler(functionHandler);
expect(normalizedHandler).to.have.property('handle');
});
it(`should throw when called with an object that doesn't expose a handle method in dev`, async function() {
if (process.env.NODE_ENV == 'production') return this.skip();
await expectError(
() => normalizeHandler(invalidHandlerObject),
'missing-a-method',
(error) => {
expect(error.details).to.have.property('moduleName').that.equals('workbox-routing');
expect(error.details).to.have.property('className').that.equals('Route');
expect(error.details).to.have.property('funcName').that.equals('constructor');
expect(error.details).to.have.property('paramName').that.equals('handler');
}
);
});
it(`should throw when called with something other than a function or an object in dev`, async function() {
if (process.env.NODE_ENV == 'production') return this.skip();
await expectError(
() => normalizeHandler(invalidHandlerString),
'incorrect-type',
(error) => {
expect(error.details).to.have.property('moduleName').that.equals('workbox-routing');
expect(error.details).to.have.property('className').that.equals('Route');
expect(error.details).to.have.property('funcName').that.equals('constructor');
expect(error.details).to.have.property('paramName').that.equals('handler');
}
);
});
it(`should properly normalize an object that exposes a handle method in production`, async function() {
if (process.env.NODE_ENV != 'production') return this.skip();
const normalizedHandler = normalizeHandler(handler);
expect(normalizedHandler).to.have.property('handle');
});
it(`should properly normalize a function in production`, async function() {
if (process.env.NODE_ENV != 'production') return this.skip();
const normalizedHandler = normalizeHandler(functionHandler);
expect(normalizedHandler).to.have.property('handle');
});
});