[MM-39885] Migrate unit tests to Jest, fleshed out tests for common/util, a bunch of cleanup (#1852)
* [MM-39885] Migrate unit tests to Jest, fleshed out tests for common/util/url * Typo fix * Oops * I found more tests! * Fixed a bug * Oops again * Tests for common/utils/util * A bunch of cleanup * Oops
This commit is contained in:
68
src/main/UserActivityMonitor.test.js
Normal file
68
src/main/UserActivityMonitor.test.js
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import UserActivityMonitor from './UserActivityMonitor';
|
||||
|
||||
describe('UserActivityMonitor', () => {
|
||||
describe('updateIdleTime', () => {
|
||||
it('should set idle time to provided value', () => {
|
||||
const userActivityMonitor = new UserActivityMonitor();
|
||||
const idleTime = Math.round(Date.now() / 1000);
|
||||
userActivityMonitor.updateIdleTime(idleTime);
|
||||
expect(userActivityMonitor.userIdleTime).toBe(idleTime);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateUserActivityStatus', () => {
|
||||
let userActivityMonitor;
|
||||
|
||||
beforeEach(() => {
|
||||
userActivityMonitor = new UserActivityMonitor();
|
||||
});
|
||||
|
||||
it('should set user status to active', () => {
|
||||
userActivityMonitor.setActivityState(true);
|
||||
expect(userActivityMonitor.userIsActive).toBe(true);
|
||||
});
|
||||
it('should set user status to inactive', () => {
|
||||
userActivityMonitor.setActivityState(false);
|
||||
expect(userActivityMonitor.userIsActive).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sendStatusUpdate', () => {
|
||||
let userActivityMonitor;
|
||||
|
||||
beforeEach(() => {
|
||||
userActivityMonitor = new UserActivityMonitor();
|
||||
});
|
||||
|
||||
it('should emit a non-system triggered status event indicating a user is active', () => {
|
||||
userActivityMonitor.on('status', ({userIsActive, isSystemEvent}) => {
|
||||
expect(userIsActive && !isSystemEvent).toBe(true);
|
||||
});
|
||||
userActivityMonitor.setActivityState(true, false);
|
||||
});
|
||||
|
||||
it('should emit a non-system triggered status event indicating a user is inactive', () => {
|
||||
userActivityMonitor.on('status', ({userIsActive, isSystemEvent}) => {
|
||||
expect(!userIsActive && !isSystemEvent).toBe(true);
|
||||
});
|
||||
userActivityMonitor.setActivityState(false, false);
|
||||
});
|
||||
|
||||
it('should emit a system triggered status event indicating a user is active', () => {
|
||||
userActivityMonitor.on('status', ({userIsActive, isSystemEvent}) => {
|
||||
expect(userIsActive && isSystemEvent).toBe(true);
|
||||
});
|
||||
userActivityMonitor.setActivityState(true, true);
|
||||
});
|
||||
|
||||
it('should emit a system triggered status event indicating a user is inactive', () => {
|
||||
userActivityMonitor.on('status', ({userIsActive, isSystemEvent}) => {
|
||||
expect(!userIsActive && isSystemEvent).toBe(true);
|
||||
});
|
||||
userActivityMonitor.setActivityState(false, true);
|
||||
});
|
||||
});
|
||||
});
|
@@ -1,12 +1,14 @@
|
||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
import {app, Notification} from 'electron';
|
||||
|
||||
import {MentionOptions} from 'types/notification';
|
||||
|
||||
import osVersion from 'common/osVersion';
|
||||
import Utils from 'common/utils/util';
|
||||
|
||||
const assetsDir = path.resolve(app.getAppPath(), 'assets');
|
||||
const appIconURL = path.resolve(assetsDir, 'appicon_48.png');
|
||||
@@ -30,7 +32,7 @@ export class Mention extends Notification {
|
||||
// Notification Center shows app's icon, so there were two icons on the notification.
|
||||
Reflect.deleteProperty(options, 'icon');
|
||||
}
|
||||
const isWin7 = (process.platform === 'win32' && osVersion.isLowerThanOrEqualWindows8_1() && DEFAULT_WIN7);
|
||||
const isWin7 = (process.platform === 'win32' && !Utils.isVersionGreaterThanOrEqualTo(os.version(), '6.3') && DEFAULT_WIN7);
|
||||
const customSound = String(!options.silent && ((options.data && options.data.soundName !== 'None' && options.data.soundName) || isWin7));
|
||||
if (customSound) {
|
||||
options.silent = true;
|
||||
|
@@ -2,10 +2,12 @@
|
||||
// See LICENSE.txt for license information.
|
||||
'use strict';
|
||||
|
||||
import assert from 'assert';
|
||||
import TrustedOriginsStore from 'main/trustedOrigins';
|
||||
import {BASIC_AUTH_PERMISSION} from 'common/permissions';
|
||||
|
||||
import TrustedOriginsStore from 'main/trustedOrigins.ts';
|
||||
import {BASIC_AUTH_PERMISSION} from 'common/permissions.ts';
|
||||
jest.mock('electron-log', () => ({
|
||||
error: jest.fn(),
|
||||
}));
|
||||
|
||||
function mockTOS(fileName, returnvalue) {
|
||||
const tos = new TrustedOriginsStore(fileName);
|
||||
@@ -20,24 +22,28 @@ describe('Trusted Origins', () => {
|
||||
it('should be empty if there is no file', () => {
|
||||
const tos = mockTOS('emptyfile', null);
|
||||
tos.load();
|
||||
assert.deepEqual(tos.data.size, 0);
|
||||
expect(tos.data.size).toStrictEqual(0);
|
||||
});
|
||||
|
||||
it('should throw an error if data isn\'t an object', () => {
|
||||
const tos = mockTOS('notobject', 'this is not my object!');
|
||||
|
||||
assert.throws(tos.load, SyntaxError);
|
||||
expect(() => {
|
||||
tos.load();
|
||||
}).toThrow(SyntaxError);
|
||||
});
|
||||
|
||||
it('should throw an error if data isn\'t in the expected format', () => {
|
||||
const tos = mockTOS('badobject', '{"https://mattermost.com": "this is not my object!"}');
|
||||
assert.throws(tos.load, /^Error: Provided TrustedOrigins file does not validate, using defaults instead\.$/);
|
||||
expect(() => {
|
||||
tos.load();
|
||||
}).toThrow(/^Provided TrustedOrigins file does not validate, using defaults instead\.$/);
|
||||
});
|
||||
|
||||
it('should drop keys that aren\'t urls', () => {
|
||||
const tos = mockTOS('badobject2', `{"this is not an uri": {"${BASIC_AUTH_PERMISSION}": true}}`);
|
||||
tos.load();
|
||||
assert.equal(typeof tos.data['this is not an uri'], 'undefined');
|
||||
expect(typeof tos.data['this is not an uri']).toBe('undefined');
|
||||
});
|
||||
|
||||
it('should contain valid data if everything goes right', () => {
|
||||
@@ -47,7 +53,7 @@ describe('Trusted Origins', () => {
|
||||
}};
|
||||
const tos = mockTOS('okfile', JSON.stringify(value));
|
||||
tos.load();
|
||||
assert.deepEqual(Object.fromEntries(tos.data.entries()), value);
|
||||
expect(Object.fromEntries(tos.data.entries())).toStrictEqual(value);
|
||||
});
|
||||
});
|
||||
describe('validate testing permissions', () => {
|
||||
@@ -62,19 +68,19 @@ describe('Trusted Origins', () => {
|
||||
const tos = mockTOS('permission_test', JSON.stringify(value));
|
||||
tos.load();
|
||||
it('tos should contain 2 elements', () => {
|
||||
assert.equal(tos.data.size, 2);
|
||||
expect(tos.data.size).toBe(2);
|
||||
});
|
||||
it('should say ok if the permission is set', () => {
|
||||
assert.equal(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION), true);
|
||||
expect(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION)).toBe(true);
|
||||
});
|
||||
it('should say ko if the permission is set to false', () => {
|
||||
assert.equal(tos.checkPermission('https://notmattermost.com', BASIC_AUTH_PERMISSION), false);
|
||||
expect(tos.checkPermission('https://notmattermost.com', BASIC_AUTH_PERMISSION)).toBe(false);
|
||||
});
|
||||
it('should say ko if the uri is not set', () => {
|
||||
assert.equal(tos.checkPermission('https://undefined.com', BASIC_AUTH_PERMISSION), null);
|
||||
expect(tos.checkPermission('https://undefined.com', BASIC_AUTH_PERMISSION)).toBe(undefined);
|
||||
});
|
||||
it('should say null if the permission is unknown', () => {
|
||||
assert.equal(tos.checkPermission('https://mattermost.com'), null);
|
||||
expect(tos.checkPermission('https://mattermost.com')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -90,9 +96,9 @@ describe('Trusted Origins', () => {
|
||||
const tos = mockTOS('permission_test', JSON.stringify(value));
|
||||
tos.load();
|
||||
it('deleting revokes access', () => {
|
||||
assert.equal(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION), true);
|
||||
expect(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION)).toBe(true);
|
||||
tos.delete('https://mattermost.com');
|
||||
assert.equal(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION), null);
|
||||
expect(tos.checkPermission('https://mattermost.com', BASIC_AUTH_PERMISSION)).toBe(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -304,7 +304,7 @@ export class MattermostView extends EventEmitter {
|
||||
}
|
||||
|
||||
handleUpdateTarget = (e: Event, url: string) => {
|
||||
if (!url || !this.tab.server.sameOrigin(url)) {
|
||||
if (!url || !urlUtils.isInternalURL(new URL(url), this.tab.server.url)) {
|
||||
this.emit(UPDATE_TARGET_URL, url);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user