Skip to content

Commit

Permalink
Add unit test for device utils (#432)
Browse files Browse the repository at this point in the history
* Add unit test for device utils

* Update
  • Loading branch information
EzioLi01 authored Sep 4, 2023
1 parent 9a51eac commit c09cef1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/server/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,8 @@ function saveDefaultDeviceIdForPlatform(platform, deviceId) {

module.exports = {
getDeviceInfo: getDeviceInfo,
updateDeviceInfo: updateDeviceInfo
updateDeviceInfo: updateDeviceInfo,
getDefaultDevice: getDefaultDevice,
getDevice: getDevice,
getUserAgent: getUserAgent
};
46 changes: 46 additions & 0 deletions test/device.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

const assert = require('assert');
var Device = require('../src/server/device');

suite('deviceUtils', function () {
test('Should get default device correctly for each platform', (done) => {
const defaultAndroidDevice = Device.getDefaultDevice('android');
assert.strictEqual('Nexus6', defaultAndroidDevice.id);
const defaultIosDevice = Device.getDefaultDevice('ios');
assert.strictEqual('iPhone12', defaultIosDevice.id);
const defaultWindowsDevice = Device.getDefaultDevice('windows');
assert.strictEqual('Lumia950', defaultWindowsDevice.id);
done();
});

test('Should get default device if the target device id is not existing in specific platform', (done) => {
const device = Device.getDevice('android', 'iPhone11');
assert.strictEqual('Nexus6', device.id);
done();
});

test('Should throw error message if no user agent found', (done) => {
const device = Device.getDevice('ios', 'iPhone12');
device['os-version'] = '1';
let error;
try {
Device.getUserAgent(device);
} catch (err) {
error = err.message;
}
assert.strictEqual('Cannot find user agent for device: iPhone 12, os-version: 1.', error);
done();
});

test('Should get device info correctly', (done) => {
const deviceInfo = Device.getDeviceInfo('ios', 'iPhone13');
assert.strictEqual(deviceInfo.deviceId, 'iPhone13');
assert.strictEqual(deviceInfo.platform, 'ios');
assert.strictEqual(Object.keys(deviceInfo.platformDevices).length != 0, true);
assert.strictEqual(deviceInfo.userAgent.includes('Mozilla') && deviceInfo.userAgent.includes('AppleWebKit'), true);
done();
});
});

0 comments on commit c09cef1

Please sign in to comment.