Skip to content

Commit b13e59e

Browse files
add more tests
1 parent c0b7056 commit b13e59e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/registry/domain/components-cache/components-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function componentsList(conf: Config, cdn: StorageAdapter) {
5858

5959
if (invalidVersions.length > 0) {
6060
eventsHandler.fire('error', {
61-
code: 'CORRUPTED_VERSION',
61+
code: 'corrupted_version',
6262
message: `Couldn't validate the integrity of the component ${componentName} on the following versions: ${invalidVersions.join(
6363
', '
6464
)}.`

test/unit/registry-domain-components-cache.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe('registry : domain : components-cache', () => {
4545
'./components-list': injectr(
4646
'../../dist/registry/domain/components-cache/components-list.js',
4747
{
48-
'oc-get-unix-utc-timestamp': getTimestamp
48+
'oc-get-unix-utc-timestamp': getTimestamp,
49+
'../events-handler': eventsHandlerStub
4950
}
5051
).default
5152
},
@@ -63,7 +64,7 @@ describe('registry : domain : components-cache', () => {
6364
let error;
6465
before(done => {
6566
mockedCdn.getJson = sinon.stub();
66-
mockedCdn.getJson.rejects('FILE_ERROR');
67+
mockedCdn.getJson.rejects(new Error('FILE_ERROR'));
6768
initialise();
6869
componentsCache
6970
.load()
@@ -72,7 +73,7 @@ describe('registry : domain : components-cache', () => {
7273
});
7374

7475
it('should throw with the error message', () => {
75-
expect(error).to.equal('FILE_ERROR');
76+
expect(error.message).to.equal('FILE_ERROR');
7677
});
7778
});
7879
describe('when initialising the cache', () => {
@@ -129,25 +130,46 @@ describe('registry : domain : components-cache', () => {
129130
before(done => {
130131
mockedCdn.getJson = sinon.stub();
131132
mockedCdn.getJson.resolves(baseResponse());
133+
mockedCdn.getJson
134+
.withArgs('component/hello-world/3.0.0/package.json')
135+
.rejects('ERROR');
132136
mockedCdn.listSubDirectories = sinon.stub();
133137
mockedCdn.listSubDirectories.onCall(0).resolves(['hello-world']);
134138
mockedCdn.listSubDirectories
135139
.onCall(1)
136-
.resolves(['1.0.0', '1.0.2', '2.0.0']);
140+
.resolves(['1.0.0', '1.0.2', '2.0.0', '3.0.0']);
137141
mockedCdn.putFileContent = sinon.stub();
138142
mockedCdn.putFileContent.resolves('ok');
139143
initialise();
140144
componentsCache.load().finally(done);
141145
});
142146

143147
it('should fetch the components.json', () => {
144-
expect(mockedCdn.getJson.calledTwice).to.be.true;
145148
expect(mockedCdn.getJson.args[0][0]).to.be.equal(
146149
'component/components.json'
147150
);
151+
});
152+
153+
it('should verify new versions', () => {
154+
expect(mockedCdn.getJson.calledThrice).to.be.true;
148155
expect(mockedCdn.getJson.args[1][0]).to.be.equal(
149156
'component/hello-world/2.0.0/package.json'
150157
);
158+
expect(mockedCdn.getJson.args[2][0]).to.be.equal(
159+
'component/hello-world/3.0.0/package.json'
160+
);
161+
});
162+
163+
it('should ignore corrupted versions and generate an error event', () => {
164+
expect(eventsHandlerStub.fire.called).to.be.true;
165+
expect(eventsHandlerStub.fire.args[0][0]).to.equal('error');
166+
expect(eventsHandlerStub.fire.args[0][1].code).to.equal(
167+
'corrupted_version'
168+
);
169+
expect(eventsHandlerStub.fire.args[0][1].message).to.contain(
170+
'hello-world'
171+
);
172+
expect(eventsHandlerStub.fire.args[0][1].message).to.contain('3.0.0');
151173
});
152174

153175
it('should scan for directories to fetch components and versions', () => {

0 commit comments

Comments
 (0)