Skip to content

Commit

Permalink
Add unit tests for utils/common.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Oct 30, 2022
1 parent 6d3cadc commit 30fe40f
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 0 deletions.
193 changes: 193 additions & 0 deletions tests/unit_tests/commands/android/testUtilsCommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
const assert = require('assert');
const mockery = require('mockery');

describe('test getAllAvailableOptions', function() {
beforeEach(() => {
mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false});
});

afterEach(() => {
mockery.deregisterAll();
mockery.resetCache();
mockery.disable();
});

it('returns all options correctly', () => {
mockery.registerMock('../constants', {
AVAILABLE_OPTIONS: {
something: {
alias: ['s', 'some']
},
other_thing: {
alias: ['other']
},
some_other_thing: {
alias: []
}
}
});

const {getAllAvailableOptions} = require('../../../../src/commands/android/utils/common');
const allOptions = getAllAvailableOptions();

assert.deepStrictEqual(allOptions, ['something', 's', 'some', 'other_thing', 'other', 'some_other_thing']);
});
});

describe('test getBinaryNameForOS', function() {
beforeEach(() => {
mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false});
});

afterEach(() => {
mockery.deregisterAll();
mockery.resetCache();
mockery.disable();
});

it('returns all binary names correctly', () => {
const {getBinaryNameForOS} = require('../../../../src/commands/android/utils/common');

// for windows
assert.strictEqual(getBinaryNameForOS('windows', 'sdkmanager'), 'sdkmanager.bat');
assert.strictEqual(getBinaryNameForOS('windows', 'avdmanager'), 'avdmanager.bat');
assert.strictEqual(getBinaryNameForOS('windows', 'sdkmanager.bat'), 'sdkmanager.bat');
assert.strictEqual(getBinaryNameForOS('windows', 'adb'), 'adb.exe');
assert.strictEqual(getBinaryNameForOS('windows', 'emulator'), 'emulator.exe');
assert.strictEqual(getBinaryNameForOS('windows', 'adb.exe'), 'adb.exe');

// for others
assert.strictEqual(getBinaryNameForOS('mac', 'sdkmanager'), 'sdkmanager');
assert.strictEqual(getBinaryNameForOS('mac', 'avdmanager.exe'), 'avdmanager.exe');
assert.strictEqual(getBinaryNameForOS('mac', 'adb'), 'adb');
assert.strictEqual(getBinaryNameForOS('linux', 'sdkmanager'), 'sdkmanager');
assert.strictEqual(getBinaryNameForOS('linux', 'avdmanager.exe'), 'avdmanager.exe');
assert.strictEqual(getBinaryNameForOS('linux', 'adb'), 'adb');
});
});

describe('test downloadWithProgressBar', function() {
beforeEach(() => {
mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false});
});

afterEach(() => {
mockery.deregisterAll();
mockery.resetCache();
mockery.disable();
});

it('returns true when completed successfully', async () => {
let progressBarInitialized = false;
let progressBarStarted = false;
let progressBarUpdated = false;
let progressBarStopped = false;
mockery.registerMock('cli-progress', {
Bar: class {
constructor() {
progressBarInitialized = true;
}
start() {
progressBarStarted = true;
}
update() {
progressBarUpdated = true;
}
stop() {
progressBarStopped = true;
}
},
Presets: {
shade_classic: 'something'
}
});

let urlPassed;
let destPassed;
let isExtracted;
mockery.registerMock('download', (url, dest, options) => {
urlPassed = url;
destPassed = dest;
isExtracted = options.extract;

return {
on: (event, callback) => {
callback({percent: 1});
}
};
});

const url = 'some_url';
const dest = 'some/dest/path';
const extract = true;

const {downloadWithProgressBar} = require('../../../../src/commands/android/utils/common');
const result = await downloadWithProgressBar(url, dest, extract);

assert.strictEqual(progressBarInitialized, true);
assert.strictEqual(urlPassed, url);
assert.strictEqual(destPassed, dest);
assert.strictEqual(isExtracted, extract);
assert.strictEqual(progressBarStarted, true);
assert.strictEqual(progressBarUpdated, true);
assert.strictEqual(progressBarStopped, true);
assert.strictEqual(result, true);
});

it('returns false and stops progress when error occur', async () => {
let progressBarInitialized = false;
let progressBarStarted = false;
let progressBarUpdated = false;
let progressBarStopped = false;
mockery.registerMock('cli-progress', {
Bar: class {
constructor() {
progressBarInitialized = true;
}
start() {
progressBarStarted = true;
}
update() {
progressBarUpdated = true;
}
stop() {
progressBarStopped = true;
}
},
Presets: {
shade_classic: 'something'
}
});

let urlPassed;
let destPassed;
let isExtracted;
mockery.registerMock('download', (url, dest, options) => {
urlPassed = url;
destPassed = dest;
isExtracted = options.extract;

return {
on: () => {
throw Error();
}
};
});

const url = 'some_url';
const dest = 'some/dest/path';
const extract = false;

const {downloadWithProgressBar} = require('../../../../src/commands/android/utils/common');
const result = await downloadWithProgressBar(url, dest, extract);

assert.strictEqual(progressBarInitialized, true);
assert.strictEqual(urlPassed, url);
assert.strictEqual(destPassed, dest);
assert.strictEqual(isExtracted, extract);
assert.strictEqual(progressBarStarted, true);
assert.strictEqual(progressBarUpdated, false);
assert.strictEqual(progressBarStopped, true);
assert.strictEqual(result, false);
});
});
51 changes: 51 additions & 0 deletions tests/unit_tests/commands/android/testUtilsGetLatestVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const assert = require('assert');
const mockery = require('mockery');
const nock = require('nock');

describe('test getAllAvailableOptions', function() {
beforeEach(() => {
mockery.enable({useCleanCache: true, warnOnReplace: false, warnOnUnregistered: false});
if (!nock.isActive()) {
nock.activate();
}
});

afterEach(() => {
mockery.deregisterAll();
mockery.resetCache();
mockery.disable();
nock.cleanAll();
nock.restore();
});

test('when firefox version returned correctly', async () => {
nock('https://api.github.com')
.get('/repos/mozilla-mobile/fenix/releases/latest')
.reply(200, {
'tag_name': 'v105.2.0'
});

const {getLatestVersion} = require('../../../../src/commands/android/utils/common');
const version = await getLatestVersion('firefox');

assert.strictEqual(version, '105.2.0');
});

test('when firefox version not found (error)', async () => {
nock('https://api.github.com')
.get('/repos/mozilla-mobile/fenix/releases/latest')
.reply(502);

const {getLatestVersion} = require('../../../../src/commands/android/utils/common');
const version = await getLatestVersion('firefox');

assert.strictEqual(version, '105.1.0');
});

test('when asked for chrome version', async () => {
const {getLatestVersion} = require('../../../../src/commands/android/utils/common');
const version = await getLatestVersion('chrome');

assert.strictEqual(version, '83.0.4103.106');
});
});

0 comments on commit 30fe40f

Please sign in to comment.