2021-08-25 11:11:55 -07:00
|
|
|
const utils = require('../lib/util/utils');
|
2019-09-09 10:48:09 -07:00
|
|
|
const version = require('../package.json').version;
|
2020-11-14 09:25:22 -07:00
|
|
|
const versionHerdsman = require('../node_modules/zigbee-herdsman/package.json').version;
|
2019-04-17 11:03:40 -07:00
|
|
|
|
|
|
|
describe('Utils', () => {
|
|
|
|
describe('Is xiaomi device', () => {
|
|
|
|
it('Identify xiaomi device', () => {
|
2019-09-09 10:48:09 -07:00
|
|
|
const device = {type: 'Router', manufacturerID: 4151, manufacturerName: 'Xiaomi'};
|
2019-04-17 11:03:40 -07:00
|
|
|
expect(true).toBe(utils.isXiaomiDevice(device));
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Identify xiaomi device without manufacturerName', () => {
|
|
|
|
const device = {type: 'Router', manufacturerID: 4447};
|
2019-04-17 11:03:40 -07:00
|
|
|
expect(true).toBe(utils.isXiaomiDevice(device));
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Identify xiaomi device with different manufacturerName', () => {
|
|
|
|
const device = {type: 'Router', manufacturerID: 4151, manufacturerName: 'Trust International B.V.\u0000'};
|
2019-04-17 11:03:40 -07:00
|
|
|
expect(false).toBe(utils.isXiaomiDevice(device));
|
|
|
|
});
|
|
|
|
});
|
2019-06-25 10:13:59 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Object has properties', () => {
|
|
|
|
expect(utils.objectHasProperties({a: 1, b: 2, c: 3}, ['a', 'b'])).toBeTruthy();
|
|
|
|
expect(utils.objectHasProperties({a: 1, b: 2, c: 3}, ['a', 'b', 'd'])).toBeFalsy();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('git last commit', async () => {
|
|
|
|
let mockReturnValue = [];
|
|
|
|
jest.mock('git-last-commit', () => ({
|
|
|
|
getLastCommit: (cb) => cb(mockReturnValue[0], mockReturnValue[1])
|
|
|
|
}));
|
|
|
|
|
|
|
|
mockReturnValue = [false, {shortHash: '123'}]
|
2021-08-25 11:11:55 -07:00
|
|
|
expect(await utils.getZigbee2MQTTVersion()).toStrictEqual({"commitHash": "123", "version": version});
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
mockReturnValue = [true, null]
|
2021-08-25 11:11:55 -07:00
|
|
|
expect(await utils.getZigbee2MQTTVersion()).toStrictEqual({"commitHash": expect.any(String), "version": version});
|
2019-09-09 10:48:09 -07:00
|
|
|
})
|
|
|
|
|
2020-11-14 09:25:22 -07:00
|
|
|
it('Check dependency version', async () => {
|
|
|
|
var dependency = 'zigbee-herdsman';
|
|
|
|
expect(await utils.getDependencyVersion(dependency)).toStrictEqual({"version": versionHerdsman});
|
|
|
|
})
|
2021-04-04 12:00:24 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('To local iso string', async () => {
|
|
|
|
var date = new Date('August 19, 1975 23:15:30 UTC+00:00');
|
|
|
|
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
|
|
|
|
Date.prototype.getTimezoneOffset = () => 60;
|
|
|
|
expect(utils.formatDate(date, 'ISO_8601_local').endsWith('-01:00')).toBeTruthy();
|
|
|
|
Date.prototype.getTimezoneOffset = () => -60;
|
|
|
|
expect(utils.formatDate(date, 'ISO_8601_local').endsWith('+01:00')).toBeTruthy();
|
|
|
|
Date.prototype.getTimezoneOffset = getTimezoneOffset;
|
|
|
|
})
|
2019-11-06 12:51:31 -07:00
|
|
|
|
2021-04-04 12:00:24 -07:00
|
|
|
it('Get key', async () => {
|
|
|
|
expect(utils.getKey({'1': '1'}, '1', 2, null)).toBe('1');
|
|
|
|
expect(utils.getKey({'1': '1'}, '2', 2, null)).toBe(2);
|
|
|
|
expect(utils.getKey({'1': '1'}, '1', null, () => '3')).toBe('3');
|
|
|
|
})
|
2019-04-17 11:03:40 -07:00
|
|
|
});
|