Skip to content

Commit bca37d9

Browse files
committed
test: add more updates.checkAllUpdates tests
1 parent 545f6f1 commit bca37d9

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

src/updates/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ async function checkAllUpdates(supportedVersions?: SupportedVersions, useAppcToo
3333

3434
const updates = await Promise.all(updateChecks);
3535

36-
return updates.filter(update => update && update.hasUpdate);
36+
// Remove anything that doesn't require an update and then sort the array by running priority
37+
return updates.filter(update => update && update.hasUpdate).sort((curr: UpdateInfo, prev: UpdateInfo) => curr.priority - prev.priority);
3738
}
3839

3940
export {

tests/fixtures/network/network-mocks.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,30 @@ export function mockAppcCoreRequest (version: string): void {
1919
});
2020
}
2121

22+
// we use nock.reply and fs.readFileSync instead of nock.replyWithFile to ensure that the files are
23+
// read before setting up any fs mocks. If we used nock.replyWitFile we get errors
24+
2225
export function mockSDKRequest (): void {
2326
nock('https://appc-mobilesdk-server.s3-us-west-2.amazonaws.com')
2427
.get('/releases.json')
25-
.replyWithFile(200, path.join(__dirname, 'sdk-response.json'));
28+
.reply(200, fs.readFileSync(path.join(__dirname, 'sdk-response.json'), 'utf8'));
2629

2730
}
2831

2932
export function mockNpmRequest (): void {
3033
nock('https://registry.npmjs.org')
3134
.get('/appcelerator/latest')
32-
.replyWithFile(200, path.join(__dirname, 'appcelerator-npm-response.json'))
35+
.reply(200, fs.readFileSync(path.join(__dirname, 'appcelerator-npm-response.json'), 'utf8'))
3336
.get('/alloy/latest')
34-
.replyWithFile(200, path.join(__dirname, 'alloy-npm-response.json'))
37+
.reply(200, fs.readFileSync(path.join(__dirname, 'alloy-npm-response.json'), 'utf8'))
3538
.get('/titanium/latest')
36-
.replyWithFile(200, path.join(__dirname, 'titanium-npm-response.json'));
39+
.reply(200, fs.readFileSync(path.join(__dirname, 'titanium-npm-response.json'), 'utf8'));
3740
}
3841

3942
export function mockNodeRequest(): void {
4043
nock('https://nodejs.org')
4144
.get('/download/release/index.json')
42-
.replyWithFile(200, path.join(__dirname, 'node-response.json'))
45+
.reply(200, fs.readFileSync(path.join(__dirname, 'node-response.json'), 'utf8'))
4346
.get('/dist/v1.2.3/node-v1.2.3.pkg')
4447
.reply(200, () => {
4548
return fs.createReadStream(path.join(__dirname, 'node-installer.pkg'));

tests/updates-test.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ describe('updates', () => {
422422
});
423423

424424
describe('checkforUpdates', () => {
425-
it('useAppcTooling false', async () => {
425+
it('useAppcTooling false no updates', async () => {
426426
const stub = global.sandbox.stub(util, 'exec');
427427
mockNodeRequest();
428428
mockSDKRequest();
@@ -435,5 +435,63 @@ describe('updates', () => {
435435
const updates = await checkAllUpdates({}, false);
436436
expect(updates.length).to.equal(0);
437437
});
438+
439+
it('useAppcTooling false with updates', async () => {
440+
const stub = global.sandbox.stub(util, 'exec');
441+
mockNodeRequest();
442+
mockSDKRequest();
443+
mockNpmRequest();
444+
mockSdk('8.0.0');
445+
mockNode(stub, '12.18.1');
446+
mockNpmCli(stub, 'alloy', '1.15.3');
447+
mockNpmCli(stub, 'titanium', '5.3.0');
448+
449+
const updates = await checkAllUpdates({}, false);
450+
expect(updates.length).to.equal(2);
451+
452+
expect(updates[0].productName).to.equal('Node.js');
453+
expect(updates[0].currentVersion).to.equal('12.18.1');
454+
expect(updates[0].latestVersion).to.equal('12.18.2');
455+
456+
expect(updates[1].productName).to.equal('Alloy');
457+
expect(updates[1].currentVersion).to.equal('1.15.3');
458+
expect(updates[1].latestVersion).to.equal('1.15.4');
459+
});
460+
461+
it('useAppcTooling true no updates', async () => {
462+
const stub = global.sandbox.stub(util, 'exec');
463+
mockNodeRequest();
464+
mockSDKRequest();
465+
mockNpmRequest();
466+
mockSdk('8.0.0');
467+
mockNode(stub, '12.18.2');
468+
mockAppcCoreRequest('6.6.6');
469+
mockAppcCli(stub, '6.6.6', '4.2.13', true);
470+
471+
const updates = await checkAllUpdates({}, true);
472+
expect(updates.length).to.equal(0);
473+
});
474+
475+
it('useAppcTooling true with updates', async () => {
476+
const stub = global.sandbox.stub(util, 'exec');
477+
mockNodeRequest();
478+
mockSDKRequest();
479+
mockNpmRequest();
480+
mockSdk('8.0.0');
481+
mockNode(stub, '12.18.1');
482+
mockAppcCoreRequest('6.6.6');
483+
mockAppcCli(stub, '6.6.6', '4.2.12', true);
484+
485+
const updates = await checkAllUpdates({}, true);
486+
expect(updates.length).to.equal(2);
487+
488+
expect(updates[0].productName).to.equal('Node.js');
489+
expect(updates[0].currentVersion).to.equal('12.18.1');
490+
expect(updates[0].latestVersion).to.equal('12.18.2');
491+
492+
expect(updates[1].productName).to.equal('Appcelerator CLI (npm)');
493+
expect(updates[1].currentVersion).to.equal('4.2.12');
494+
expect(updates[1].latestVersion).to.equal('4.2.13');
495+
});
438496
});
439497
});

0 commit comments

Comments
 (0)