-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
210 lines (194 loc) · 5.53 KB
/
index.js
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
205
206
207
208
209
210
import assert from "assert"
import { cdk, CocodaSDK, BaseProvider, errors, utils } from "../src/index.js"
// axios mock
import MockAdapter from "axios-mock-adapter"
const mock = new MockAdapter(cdk.axios)
describe("index", () => {
beforeEach(() => {
mock.resetHandlers()
})
it("should export the default instance", () => {
assert.equal(cdk instanceof CocodaSDK, true, "cdk should be instance of CocodaSDK")
})
it("should export the errors object", () => {
assert.ok(errors)
})
it("should export the utils object", () => {
assert.ok(utils)
})
it("should set config when setConfig is called", () => {
cdk.setConfig({ test: "test" })
assert.equal(cdk.config.test, "test")
})
it("should have expected properties", () => {
const props = [
"config",
"createInstance",
"loadConfig",
"setConfig",
"loadBuildInfo",
"getRegistryForUri",
"initializeRegistry",
"addProvider",
]
for (let prop of props) {
assert.ok(!!cdk[prop], `cdk should have property "${prop}"`)
}
})
it("should be able to create a new instance with createInstance", () => {
// Set custom property on current instance
cdk.someProp = "test"
// Create a new instance
const cdk2 = cdk.createInstance()
// Check that custom prop doesn't exist on new instance
assert.ok(!cdk2.someProp)
})
// TODO: Test additional functionality of repeat.
it("should properly repeat a method", (done) => {
let callbackCalled = 0
let valueToBeIncremented = 0
let method = () => {
return ++valueToBeIncremented
}
let repeat = cdk.repeat({
function: method,
interval: 10,
callback: (error, result, previousResult) => {
assert(previousResult == null || result == previousResult + 1)
assert.equal(result, valueToBeIncremented)
callbackCalled += 1
// Stop test after three calls
if (callbackCalled == 3) {
repeat.stop()
done()
}
},
})
})
it("should properly behave when loading buildInfo without previous value", (done) => {
let mockCalled = 0
mock.onGet("buildInfo").reply(() => {
mockCalled += 1
return [200, { mockCalled }]
})
let repeat = cdk.loadBuildInfo({
url: "buildInfo",
interval: 10,
callback: (error, buildInfo, previousBuildInfo) => {
assert(!error)
assert.notEqual(previousBuildInfo, null)
assert.equal(buildInfo.mockCalled, mockCalled)
repeat.stop()
done()
},
})
})
it("should properly behave when loading buildInfo with previous value", (done) => {
let mockCalled = 0
mock.onGet("buildInfo").reply(() => {
mockCalled += 1
return [200, { mockCalled }]
})
let repeat = cdk.loadBuildInfo({
url: "buildInfo",
buildInfo: {
mockCalled,
},
interval: 10,
callback: (error, buildInfo, previousBuildInfo) => {
assert(!error)
if (mockCalled == 1) {
assert.notEqual(previousBuildInfo, null)
assert.equal(previousBuildInfo.mockCalled, 0)
assert.equal(buildInfo.mockCalled, 1)
} else if (mockCalled == 2) {
assert.notEqual(previousBuildInfo, null)
assert.equal(previousBuildInfo.mockCalled, 1)
assert.equal(buildInfo.mockCalled, 2)
repeat.stop()
done()
}
},
})
})
it("should properly behave when loading buildInfo with an error", (done) => {
let mockCalled = 0
mock.onGet("buildInfo").reply(() => {
mockCalled += 1
return [mockCalled == 1 ? 404 : 200, { mockCalled }]
})
let repeat = cdk.loadBuildInfo({
url: "buildInfo",
interval: 10,
callback: (error, result) => {
if (mockCalled == 1) {
assert.notEqual(error, null)
} else if (mockCalled == 2) {
// Should not call callback here!
assert(false)
} else if (mockCalled == 3) {
assert.equal(result.mockCalled, mockCalled)
repeat.stop()
done()
}
},
})
})
it("should load schemes properly", async () => {
const scheme1 = {
uri: "scheme1",
}
const scheme2 = {
uri: "scheme2",
}
const scheme1alt = {
uri: "scheme1",
test: 1,
}
class SchemeProvider1 extends BaseProvider {
constructor(registry) {
super(registry)
this.has.schemes = true
}
async getSchemes() {
return [scheme1, scheme2]
}
}
SchemeProvider1.providerName = "Scheme1"
class SchemeProvider2 extends BaseProvider {
constructor(registry) {
super(registry)
this.has.schemes = true
}
async getSchemes() {
return [scheme1alt]
}
}
SchemeProvider2.providerName = "Scheme2"
const cdk2 = cdk.createInstance()
cdk2.addProvider(SchemeProvider1)
cdk2.addProvider(SchemeProvider2)
cdk2.setConfig({
registries: [
{
provider: SchemeProvider2.providerName,
},
{
provider: SchemeProvider1.providerName,
},
],
})
const schemes = await cdk2.getSchemes()
assert.equal(schemes.length, 2)
schemes.forEach(scheme => {
if (scheme.uri == "scheme1") {
// Expect scheme1 to be from SchemeProvider2
assert(scheme._registry instanceof SchemeProvider2)
}
if (scheme.uri == "scheme2") {
// Expect scheme2 to be from SchemeProvider1
assert(scheme._registry instanceof SchemeProvider1)
}
})
})
})