Files
mattermostest/src/main/autoUpdater.test.js
Devin Binnie f4f4511cc7 Remove WindowManager, separate functionality into smaller modules (#2682)
* Move sendToRenderer to respective singletons

* Move to using ViewManager call for getting view by webContentsId

* Move show and create logic to main window, handle deep linking seperately

* Move resizing logic and event handing to mainWindow

* Move server switching logic to main/app

* Move tab switching logic to main/app, rely on showById for most usage

* Migrate remaining functions, remove windowManager objects, set up imports for self-contained singletons

* Fix E2E tests

* Update src/main/app/servers.ts

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2023-04-19 11:04:26 -04:00

187 lines
5.6 KiB
JavaScript

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {ipcMain} from 'electron';
import {autoUpdater} from 'electron-updater';
import {CHECK_FOR_UPDATES} from 'common/communication';
import {UpdateManager} from './autoUpdater';
import {displayRestartToUpgrade, displayUpgrade} from './notifications';
jest.mock('electron', () => ({
app: {
getAppPath: () => '/path/to/app',
},
nativeImage: {
createFromPath: jest.fn(),
},
ipcMain: {
on: jest.fn(),
emit: jest.fn(),
},
}));
jest.mock('electron-updater', () => ({
autoUpdater: {
on: jest.fn(),
once: jest.fn(),
removeListener: jest.fn(),
quitAndInstall: jest.fn(),
downloadUpdate: jest.fn(),
checkForUpdates: jest.fn(),
},
CancellationToken: jest.fn().mockImplementation(() => {
return {};
}),
}));
jest.mock('common/config', () => ({
canUpgrade: true,
}));
jest.mock('main/notifications', () => ({
displayUpgrade: jest.fn(),
displayRestartToUpgrade: jest.fn(),
}));
jest.mock('main/windows/mainWindow', () => ({
sendToRenderer: jest.fn(),
}));
jest.mock('main/i18nManager', () => ({
localizeMessage: jest.fn(),
}));
jest.mock('main/downloadsManager', () => ({
removeUpdateBeforeRestart: jest.fn(),
}));
describe('main/autoUpdater', () => {
describe('constructor', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('should notify user on update-available', () => {
let cb;
autoUpdater.on.mockImplementation((event, callback) => {
if (event === 'update-available') {
cb = callback;
}
});
const updateManager = new UpdateManager();
updateManager.notify = jest.fn();
cb({version: '5.1.0'});
expect(updateManager.versionAvailable).toBe('5.1.0');
expect(updateManager.notify).toHaveBeenCalled();
});
it('should notify user on update-downloaded', () => {
let cb;
autoUpdater.on.mockImplementation((event, callback) => {
if (event === 'update-downloaded') {
cb = callback;
}
});
const updateManager = new UpdateManager();
updateManager.notifyDownloaded = jest.fn();
cb({version: '5.1.0'});
expect(updateManager.versionDownloaded).toBe('5.1.0');
expect(updateManager.notifyDownloaded).toHaveBeenCalled();
});
it('should check for updates when emitted', () => {
let cb;
ipcMain.on.mockImplementation((event, callback) => {
if (event === CHECK_FOR_UPDATES) {
cb = callback;
}
});
const updateManager = new UpdateManager();
updateManager.checkForUpdates = jest.fn();
cb();
expect(updateManager.checkForUpdates).toHaveBeenCalledWith(true);
});
});
describe('notify', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.runAllTimers();
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
it('should add a new timeout', () => {
const updateManager = new UpdateManager();
updateManager.notify();
updateManager.notify = jest.fn();
jest.runAllTimers();
expect(updateManager.notify).toBeCalled();
});
it('should display upgrade notification', () => {
const updateManager = new UpdateManager();
updateManager.versionAvailable = '5.1.0';
updateManager.notify();
updateManager.notify = jest.fn();
expect(displayUpgrade).toHaveBeenCalledWith('5.1.0', expect.any(Function));
});
it('should display downloaded upgrade notification', () => {
const updateManager = new UpdateManager();
updateManager.versionDownloaded = '5.1.0';
updateManager.notify();
updateManager.notify = jest.fn();
expect(displayRestartToUpgrade).toHaveBeenCalledWith('5.1.0', expect.any(Function));
});
});
describe('checkForUpdates', () => {
beforeEach(() => {
autoUpdater.checkForUpdates.mockReturnValue(Promise.resolve());
jest.useFakeTimers();
});
afterEach(() => {
jest.resetAllMocks();
});
afterAll(() => {
jest.runOnlyPendingTimers();
jest.clearAllTimers();
jest.useRealTimers();
});
it('should show dialog if update is not available', () => {
autoUpdater.once.mockImplementation((event, callback) => {
if (event === 'update-not-available') {
callback();
}
});
const updateManager = new UpdateManager();
updateManager.displayNoUpgrade = jest.fn();
updateManager.checkForUpdates(true);
updateManager.checkForUpdates = jest.fn();
expect(updateManager.displayNoUpgrade).toHaveBeenCalled();
});
it('should check again at the next interval', () => {
const updateManager = new UpdateManager();
updateManager.checkForUpdates();
updateManager.checkForUpdates = jest.fn();
jest.runAllTimers();
expect(updateManager.checkForUpdates).toBeCalled();
});
});
});