Fix tests and convert to RTL (#23474)

This commit is contained in:
Michael Telatynski 2022-10-13 09:22:34 +01:00 committed by GitHub
parent aa6b1d128a
commit 172f2f3993
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 194 additions and 302 deletions

View file

@ -14,28 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import request from 'browser-request';
import EventEmitter from 'events';
import { logger } from 'matrix-js-sdk/src/logger';
import { MatrixClient, MatrixEvent, Room } from 'matrix-js-sdk/src/matrix';
import { MatrixEvent, Room } from 'matrix-js-sdk/src/matrix';
import { UpdateCheckStatus } from 'matrix-react-sdk/src/BasePlatform';
import { Action } from 'matrix-react-sdk/src/dispatcher/actions';
import dispatcher from 'matrix-react-sdk/src/dispatcher/dispatcher';
import { MatrixClientPeg } from 'matrix-react-sdk/src/MatrixClientPeg';
import * as rageshake from 'matrix-react-sdk/src/rageshake/rageshake';
import ElectronPlatform from '../../../../src/vector/platform/ElectronPlatform';
jest.mock('matrix-react-sdk/src/rageshake/rageshake', () => ({
flush: jest.fn()
}))
flush: jest.fn(),
}));
describe('ElectronPlatform', () => {
const defaultUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36';
const defaultUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36';
const mockElectron = {
on: jest.fn(),
send: jest.fn()
send: jest.fn(),
};
const dispatchSpy = jest.spyOn(dispatcher, 'dispatch');
@ -58,25 +55,25 @@ describe('ElectronPlatform', () => {
it('flushes rageshake before quitting', () => {
new ElectronPlatform();
const [event, handler] = getElectronEventHandlerCall('before-quit');
// correct event bound
expect(event).toBeTruthy();
const [event, handler] = getElectronEventHandlerCall('before-quit');
// correct event bound
expect(event).toBeTruthy();
handler();
handler();
expect(logSpy).toHaveBeenCalled();
expect(rageshake.flush).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalled();
expect(rageshake.flush).toHaveBeenCalled();
});
it('dispatches view settings action on preferences event', () => {
new ElectronPlatform();
const [event, handler] = getElectronEventHandlerCall('preferences');
// correct event bound
expect(event).toBeTruthy();
const [event, handler] = getElectronEventHandlerCall('preferences');
// correct event bound
expect(event).toBeTruthy();
handler();
handler();
expect(dispatchFireSpy).toHaveBeenCalledWith(Action.ViewUserSettings);
expect(dispatchFireSpy).toHaveBeenCalledWith(Action.ViewUserSettings);
});
describe('updates', () => {
@ -85,39 +82,38 @@ describe('ElectronPlatform', () => {
const [event, handler] = getElectronEventHandlerCall('check_updates');
// correct event bound
expect(event).toBeTruthy();
handler({}, true);
expect(dispatchSpy).toHaveBeenCalledWith({
action: Action.CheckUpdates,
status: UpdateCheckStatus.Downloading
})
status: UpdateCheckStatus.Downloading,
});
});
it('dispatches on check updates action when update not available', () => {
new ElectronPlatform();
const [, handler] = getElectronEventHandlerCall('check_updates');
handler({}, false);
expect(dispatchSpy).toHaveBeenCalledWith({
action: Action.CheckUpdates,
status: UpdateCheckStatus.NotAvailable
})
status: UpdateCheckStatus.NotAvailable,
});
});
it('starts update check', () => {
const platform = new ElectronPlatform();
platform.startUpdateCheck();
expect(mockElectron.send).toHaveBeenCalledWith('check_updates')
expect(mockElectron.send).toHaveBeenCalledWith('check_updates');
});
it('installs update', () => {
const platform = new ElectronPlatform();
platform.installUpdate();
expect(mockElectron.send).toHaveBeenCalledWith('install_update')
expect(mockElectron.send).toHaveBeenCalledWith('install_update');
});
});
it('returns human readable name', () => {
const platform = new ElectronPlatform();
expect(platform.getHumanReadableName()).toEqual('Electron Platform');
@ -125,11 +121,13 @@ describe('ElectronPlatform', () => {
describe("getDefaultDeviceDisplayName", () => {
it.each([[
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Element Desktop: macOS",
],
[
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " +
"electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36",
"Element Desktop: Windows",
],
[
@ -151,9 +149,7 @@ describe('ElectronPlatform', () => {
[
"custom user agent",
"Element Desktop: Unknown",
],
])("%s = %s", (userAgent, result) => {
]])("%s = %s", (userAgent, result) => {
delete window.navigator;
window.navigator = { userAgent } as unknown as Navigator;
const platform = new ElectronPlatform();
@ -232,7 +228,7 @@ describe('ElectronPlatform', () => {
const [channel, { name }] = mockElectron.send.mock.calls[0];
expect(channel).toEqual("ipcCall");
expect(name).toEqual('getAvailableSpellCheckLanguages')
expect(name).toEqual('getAvailableSpellCheckLanguages');
});
});
@ -243,8 +239,8 @@ describe('ElectronPlatform', () => {
platform.getPickleKey(userId, deviceId);
const [, { name, args }] = mockElectron.send.mock.calls[0];
expect(name).toEqual('getPickleKey')
expect(args).toEqual([userId, deviceId])
expect(name).toEqual('getPickleKey');
expect(args).toEqual([userId, deviceId]);
});
it('makes correct ipc call to create pickle key', () => {
@ -253,8 +249,8 @@ describe('ElectronPlatform', () => {
platform.createPickleKey(userId, deviceId);
const [, { name, args }] = mockElectron.send.mock.calls[0];
expect(name).toEqual('createPickleKey')
expect(args).toEqual([userId, deviceId])
expect(name).toEqual('createPickleKey');
expect(args).toEqual([userId, deviceId]);
});
it('makes correct ipc call to destroy pickle key', () => {
@ -263,8 +259,8 @@ describe('ElectronPlatform', () => {
platform.destroyPickleKey(userId, deviceId);
const [, { name, args }] = mockElectron.send.mock.calls[0];
expect(name).toEqual('destroyPickleKey')
expect(args).toEqual([userId, deviceId])
expect(name).toEqual('destroyPickleKey');
expect(args).toEqual([userId, deviceId]);
});
});

View file

@ -33,6 +33,7 @@ describe('WebPlatform', () => {
});
it('registers service worker', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - mocking readonly object
navigator.serviceWorker = { register: jest.fn() };
new WebPlatform();
@ -66,7 +67,8 @@ describe('WebPlatform', () => {
describe("getDefaultDeviceDisplayName", () => {
it.each([[
"https://develop.element.io/#/room/!foo:bar",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/105.0.0.0 Safari/537.36",
"develop.element.io: Chrome on macOS",
]])("%s & %s = %s", (url, userAgent, result) => {
delete window.navigator;
@ -82,14 +84,16 @@ describe('WebPlatform', () => {
const mockNotification = {
requestPermission: jest.fn(),
permission: 'notGranted',
}
};
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.Notification = mockNotification;
mockNotification.permission = 'notGranted';
});
it('supportsNotifications returns false when platform does not support notifications', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.Notification = undefined;
expect(new WebPlatform().supportsNotifications()).toBe(false);
@ -104,7 +108,7 @@ describe('WebPlatform', () => {
});
it('maySendNotifications returns true when notification permissions are granted', () => {
mockNotification.permission = 'granted'
mockNotification.permission = 'granted';
expect(new WebPlatform().maySendNotifications()).toBe(true);
});
@ -115,7 +119,6 @@ describe('WebPlatform', () => {
const result = await platform.requestNotificationPermission();
expect(result).toEqual('test');
});
});
describe('app version', () => {
@ -124,7 +127,7 @@ describe('WebPlatform', () => {
beforeEach(() => {
jest.spyOn(MatrixClientPeg, 'userRegisteredWithinLastHours').mockReturnValue(false);
})
});
afterAll(() => {
process.env.VERSION = envVersion;
@ -154,7 +157,8 @@ describe('WebPlatform', () => {
});
describe('pollForUpdate()', () => {
it('should return not available and call showNoUpdate when current version matches most recent version', async () => {
it('should return not available and call showNoUpdate when current version ' +
'matches most recent version', async () => {
process.env.VERSION = prodVersion;
fetchMock.getOnce("/version", prodVersion);
const platform = new WebPlatform();
@ -183,7 +187,8 @@ describe('WebPlatform', () => {
expect(showNoUpdate).toHaveBeenCalled();
});
it('should return ready and call showUpdate when current version differs from most recent version', async () => {
it('should return ready and call showUpdate when current version ' +
'differs from most recent version', async () => {
process.env.VERSION = '0.0.0'; // old version
fetchMock.getOnce("/version", prodVersion);
const platform = new WebPlatform();