This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
forked from opencomponents/oc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry-domain-components-cache.js
266 lines (224 loc) · 9.6 KB
/
registry-domain-components-cache.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'use strict';
/* eslint no-unused-vars: 'off' */
var expect = require('chai').expect;
var injctr = require('injectr');
var sinon = require('sinon');
describe('registry : domain : components-cache', function(){
var mockedCdn = {
getFile: sinon.stub(),
listSubDirectories: sinon.stub(),
putFileContent: sinon.stub()
};
var setTimeoutStub,
clearTimeoutStub,
componentsCache,
eventsHandlerStub,
baseOptions = { pollingInterval: 5, s3: { componentsDir: 'component'}},
response,
baseResponse = JSON.stringify({
lastEdit: 12345678,
components: {
'hello-world': ['1.0.0', '1.0.2']
}
});
var initialise = function(){
clearTimeoutStub = sinon.stub();
setTimeoutStub = sinon.stub();
eventsHandlerStub = { fire: sinon.stub() };
var ComponentsCache = injctr('../../src/registry/domain/components-cache.js', {
'../../utils/get-unix-utc-timestamp': function(){
return 12345678;
},
'./events-handler': eventsHandlerStub
}, {
setTimeout: setTimeoutStub,
clearTimeout: clearTimeoutStub
});
componentsCache = new ComponentsCache(baseOptions, mockedCdn);
};
var saveCallbackResult = function(callback){
return function(err, res){
response = { error: err, result: res };
callback();
};
};
describe('when library does not contain components.json', function(){
describe('when initialising the cache', function(){
before(function(done){
mockedCdn.getFile = sinon.stub();
mockedCdn.getFile.yields('not_found');
mockedCdn.listSubDirectories = sinon.stub();
mockedCdn.listSubDirectories.onCall(0).yields(null, ['hello-world']);
mockedCdn.listSubDirectories.onCall(1).yields(null, ['1.0.0', '1.0.2']);
mockedCdn.putFileContent = sinon.stub();
mockedCdn.putFileContent.yields(null, 'ok');
initialise();
componentsCache.load(saveCallbackResult(done));
});
it('should try fetching the components.json', function(){
expect(mockedCdn.getFile.calledOnce).to.be.true;
expect(mockedCdn.getFile.args[0][0]).to.be.equal('component/components.json');
});
it('should scan for directories to fetch components and versions', function(){
expect(mockedCdn.listSubDirectories.calledTwice).to.be.true;
});
it('should then save the directories\' data to components.json file in cdn', function(){
expect(mockedCdn.putFileContent.called).to.be.true;
expect(mockedCdn.putFileContent.args[0][2]).to.be.true;
expect(JSON.parse(mockedCdn.putFileContent.args[0][0])).to.eql({
lastEdit: 12345678,
components: {
'hello-world': ['1.0.0', '1.0.2']
}
});
});
it('should start the refresh loop', function(){
expect(setTimeoutStub.called).to.be.true;
expect(setTimeoutStub.args[0][1]).to.equal(5000);
});
});
});
describe('when library contains outdated components.json', function(){
describe('when initialising the cache', function(){
before(function(done){
mockedCdn.getFile = sinon.stub();
mockedCdn.getFile.yields(null, baseResponse);
mockedCdn.listSubDirectories = sinon.stub();
mockedCdn.listSubDirectories.onCall(0).yields(null, ['hello-world']);
mockedCdn.listSubDirectories.onCall(1).yields(null, ['1.0.0', '1.0.2', '2.0.0']);
mockedCdn.putFileContent = sinon.stub();
mockedCdn.putFileContent.yields(null, 'ok');
initialise();
componentsCache.load(saveCallbackResult(done));
});
it('should fetch the components.json', function(){
expect(mockedCdn.getFile.calledOnce).to.be.true;
expect(mockedCdn.getFile.args[0][0]).to.be.equal('component/components.json');
});
it('should scan for directories to fetch components and versions', function(){
expect(mockedCdn.listSubDirectories.calledTwice).to.be.true;
});
it('should then save the directories\' data to components.json file in cdn', function(){
expect(mockedCdn.putFileContent.called).to.be.true;
expect(mockedCdn.putFileContent.args[0][2]).to.be.true;
expect(JSON.parse(mockedCdn.putFileContent.args[0][0])).to.eql({
lastEdit: 12345678,
components: {
'hello-world': ['1.0.0', '1.0.2', '2.0.0']
}
});
});
it('should start the refresh loop', function(){
expect(setTimeoutStub.called).to.be.true;
expect(setTimeoutStub.args[0][1]).to.equal(5000);
});
});
});
describe('when library contains updated components.json', function(){
describe('when initialising the cache', function(){
before(function(done){
mockedCdn.getFile = sinon.stub();
mockedCdn.getFile.yields(null, baseResponse);
mockedCdn.listSubDirectories = sinon.stub();
mockedCdn.listSubDirectories.onCall(0).yields(null, ['hello-world']);
mockedCdn.listSubDirectories.onCall(1).yields(null, ['1.0.0', '1.0.2']);
mockedCdn.putFileContent = sinon.stub();
initialise();
componentsCache.load(saveCallbackResult(done));
});
it('should fetch the components.json', function(){
expect(mockedCdn.getFile.calledOnce).to.be.true;
expect(mockedCdn.getFile.args[0][0]).to.be.equal('component/components.json');
});
it('should scan for directories to fetch components and versions', function(){
expect(mockedCdn.listSubDirectories.calledTwice).to.be.true;
});
it('should not modify components.json', function(){
expect(mockedCdn.putFileContent.called).to.be.false;
});
it('should use it as a source of truth', function(done){
componentsCache.get(function(err, res){
expect(res).to.eql({
lastEdit: 12345678,
components: {
'hello-world': ['1.0.0', '1.0.2']
}
});
done();
});
});
it('should start the refresh loop', function(){
expect(setTimeoutStub.called).to.be.true;
expect(setTimeoutStub.args[0][1]).to.equal(5000);
});
});
describe('when refreshing the cache', function(){
var baseResponseUpdated = JSON.parse(baseResponse);
baseResponseUpdated.components['hello-world'].push('2.0.0');
baseResponseUpdated.components['new-component'] = ['1.0.0'];
baseResponseUpdated.lastEdit++;
baseResponseUpdated = JSON.stringify(baseResponseUpdated);
describe('when refresh errors', function(){
before(function(done){
mockedCdn.getFile = sinon.stub();
mockedCdn.getFile.yields(null, baseResponse);
mockedCdn.putFileContent = sinon.stub();
mockedCdn.putFileContent.yields(null, 'ok');
mockedCdn.listSubDirectories = sinon.stub();
mockedCdn.listSubDirectories.onCall(0).yields(null, ['hello-world']);
mockedCdn.listSubDirectories.onCall(1).yields(null, ['1.0.0', '1.0.2']);
mockedCdn.listSubDirectories.onCall(2).yields(null, ['hello-world', 'new-component']);
mockedCdn.listSubDirectories.onCall(3).yields('an error!');
mockedCdn.listSubDirectories.onCall(4).yields(null, ['1.0.0']);
initialise();
componentsCache.load(function(){
componentsCache.refresh(saveCallbackResult(done));
});
});
it('should generate an error event', function(){
expect(eventsHandlerStub.fire.called).to.be.true;
expect(eventsHandlerStub.fire.args[0][0]).to.equal('cache-poll');
expect(eventsHandlerStub.fire.args[1][0]).to.equal('error');
expect(eventsHandlerStub.fire.args[1][1].code).to.equal('components_cache_refresh');
expect(eventsHandlerStub.fire.args[1][1].message).to.contain('an error!');
});
});
describe('when refresh does not generate errors', function(){
before(function(done){
mockedCdn.getFile = sinon.stub();
mockedCdn.getFile.yields(null, baseResponse);
mockedCdn.putFileContent = sinon.stub();
mockedCdn.putFileContent.yields(null, 'ok');
mockedCdn.listSubDirectories = sinon.stub();
mockedCdn.listSubDirectories.onCall(0).yields(null, ['hello-world']);
mockedCdn.listSubDirectories.onCall(1).yields(null, ['1.0.0', '1.0.2']);
mockedCdn.listSubDirectories.onCall(2).yields(null, ['hello-world', 'new-component']);
mockedCdn.listSubDirectories.onCall(3).yields(null, ['1.0.0', '1.0.2', '2.0.0']);
mockedCdn.listSubDirectories.onCall(4).yields(null, ['1.0.0']);
initialise();
componentsCache.load(function(){
componentsCache.refresh(saveCallbackResult(done));
});
});
it('should have started, stopped and restarted the refresh loop', function(){
expect(setTimeoutStub.calledTwice).to.be.true;
expect(clearTimeoutStub.calledOnce).to.be.true;
});
it('should do list requests to cdn', function(){
expect(mockedCdn.listSubDirectories.args.length).to.equal(5);
});
it('should do write request to cdn', function(){
expect(mockedCdn.putFileContent.calledOnce).to.be.true;
});
it('should refresh the values', function(done){
componentsCache.get(function(err, data){
expect(data.lastEdit).to.equal(12345678);
expect(data.components['new-component']).to.eql(['1.0.0']);
expect(data.components['hello-world'].length).to.equal(3);
done();
});
});
});
});
});
});