-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaio-lib-runtime.js
More file actions
115 lines (110 loc) · 3.41 KB
/
aio-lib-runtime.js
File metadata and controls
115 lines (110 loc) · 3.41 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
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const originalRuntimeLib = jest.requireActual('@adobe/aio-lib-runtime')
const cleanRtLibInstance = {
actions: {},
activations: {},
namespaces: {},
packages: {
update: jest.fn(),
list: jest.fn(() => '')
},
rules: {
list: jest.fn(() => '')
},
triggers: {
list: jest.fn(() => '')
},
logForwarding: {
get: jest.fn(),
setAdobeIoRuntime: jest.fn(),
setAzureLogAnalytics: jest.fn(),
setSplunkHec: jest.fn(),
setNewRelic: jest.fn()
},
feeds: {},
routes: {}
}
const RtLibInstanceMethods = {
mockFn: function (methodName) {
const cmd = methodName.split('.')
let method = this
while (cmd.length > 1) {
const word = cmd.shift()
method = method[word] = method[word] || {}
}
method = method[cmd.shift()] = jest.fn()
return method
},
mockResolvedFixtureMulitValue: function (methodName, returnValues) {
return this.mockResolvedMulitValue(methodName, returnValues, true)
},
mockResolvedFixture: function (methodName, returnValue) {
return this.mockResolved(methodName, returnValue, true)
},
mockRejectedFixture: function (methodName, returnValue) {
return this.mockRejected(methodName, returnValue, true)
},
mockResolvedMulitValue: function (methodName, returnValues, isFile) {
let vals = (isFile) ? fixtureFile(returnValues) : returnValues
try {
vals = JSON.parse(vals)
} catch (e) { }
const mockFn = this.mockFn(methodName)
for (const i in vals) {
mockFn.mockResolvedValueOnce(vals[i], isFile)
}
mockFn.mockResolvedValue(vals[vals.length - 1], isFile)
return mockFn
},
mockResolved: function (methodName, returnValue, isFile) {
let val = (isFile) ? fixtureFile(returnValue) : returnValue
try {
val = JSON.parse(val)
} catch (e) { }
return this.mockFn(methodName).mockResolvedValue(val, isFile)
},
mockRejected: function (methodName, err) {
return this.mockFn(methodName).mockRejectedValue(err)
}
}
const mockRtLibInstance = {
...cleanRtLibInstance,
...RtLibInstanceMethods
}
const mockRtUtils = {
getActionUrls: jest.fn(),
checkOpenWhiskCredentials: jest.fn(),
getIncludeIMSCredentialsAnnotationInputs: jest.fn()
}
const init = jest.fn().mockReturnValue(mockRtLibInstance)
const mockActionMethods = {
buildActions: jest.fn(),
deployActions: jest.fn(),
undeployActions: jest.fn(),
printActionLogs: jest.fn()
}
module.exports = {
utils: {
...mockRtUtils,
_absApp: originalRuntimeLib.utils._absApp,
_relApp: originalRuntimeLib.utils._relApp,
getActionEntryFile: originalRuntimeLib.utils.getActionEntryFile
},
init,
...mockActionMethods,
mockReset: () => {
Object.values(mockRtUtils).forEach(v => v.mockReset())
init.mockClear()
Object.values(mockActionMethods).forEach(v => v.mockReset())
Object.assign(mockRtLibInstance, cleanRtLibInstance)
}
}