Skip to content

Commit

Permalink
test: ensuring update alert system is working as expected
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>
  • Loading branch information
axel7083 committed Feb 28, 2024
1 parent 3a25b09 commit e0e2d3b
Showing 1 changed file with 87 additions and 8 deletions.
95 changes: 87 additions & 8 deletions packages/main/src/plugin/updater.spec.ts
Expand Up @@ -27,6 +27,8 @@ import { app } from 'electron';
import { type AppUpdater, autoUpdater } from 'electron-updater';
import * as util from '/@/util.js';
import type { AppUpdaterEvents } from 'electron-updater/out/AppUpdater.js';
import type { ConfigurationRegistry } from '/@/plugin/configuration-registry.js';
import type { Configuration } from '@podman-desktop/api';

vi.mock('electron', () => ({
app: {
Expand All @@ -52,11 +54,22 @@ const messageBoxMock = {
showMessageBox: vi.fn(),
} as unknown as MessageBox;

const configurationMock = {
get: vi.fn(),
has: vi.fn(),
update: vi.fn(),
} as unknown as Configuration;

const configurationRegistryMock = {
registerConfigurations: vi.fn(),
getConfiguration: vi.fn(),
} as unknown as ConfigurationRegistry;

const statusBarRegistryMock = {
setEntry: vi.fn(),
} as unknown as StatusBarRegistry;

const commandRegistry = {
const commandRegistryMock = {
registerCommand: vi.fn(),
executeCommand: vi.fn(),
} as unknown as CommandRegistry;
Expand All @@ -72,24 +85,33 @@ beforeEach(() => {
// eslint-disable-next-line no-null/no-null
vi.mocked(autoUpdater.checkForUpdates).mockResolvedValue(null);

vi.mocked(commandRegistry.executeCommand).mockResolvedValue(undefined);
vi.mocked(commandRegistryMock.executeCommand).mockResolvedValue(undefined);
vi.mocked(util.isLinux).mockReturnValue(false);

vi.mocked(configurationMock.get).mockReturnValue('never');
vi.mocked(configurationMock.update).mockResolvedValue(undefined);
vi.mocked(configurationRegistryMock.getConfiguration).mockReturnValue(configurationMock);
});

test('expect env PROD to be truthy', () => {
expect(import.meta.env.PROD).toBeTruthy();
});

test('expect init to provide a disposable', () => {
const updater = new Updater(messageBoxMock, statusBarRegistryMock, commandRegistry);
const updater = new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock);
const disposable: unknown = updater.init();
expect(disposable).toBeDefined();
expect(disposable instanceof Disposable).toBeTruthy();
});

test('expect init to register commands', () => {
new Updater(messageBoxMock, statusBarRegistryMock, commandRegistry).init();
expect(commandRegistry.registerCommand).toHaveBeenCalled();
new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();
expect(commandRegistryMock.registerCommand).toHaveBeenCalled();
});

test('expect init to register configuration', () => {
new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();
expect(configurationRegistryMock.registerConfigurations).toHaveBeenCalled();
});

test('expect update available entry to be displayed when expected', () => {
Expand All @@ -112,7 +134,7 @@ test('expect update available entry to be displayed when expected', () => {
return {} as unknown as AppUpdater;
});

new Updater(messageBoxMock, statusBarRegistryMock, commandRegistry).init();
new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();

// listener should exist
expect(mListener).toBeDefined();
Expand Down Expand Up @@ -143,7 +165,7 @@ test('expect default status entry to be displayed when no update available', ()
return {} as unknown as AppUpdater;
});

new Updater(messageBoxMock, statusBarRegistryMock, commandRegistry).init();
new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();

// listener should exist
expect(mListener).toBeDefined();
Expand Down Expand Up @@ -174,7 +196,7 @@ test('expect default status entry when error No published versions on GitHub', (
return {} as unknown as AppUpdater;
});

new Updater(messageBoxMock, statusBarRegistryMock, commandRegistry).init();
new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();

// listener should exist
expect(mListener).toBeDefined();
Expand All @@ -184,3 +206,60 @@ test('expect default status entry when error No published versions on GitHub', (

expect(setEntryMock).toHaveBeenCalled();
});

test('expect command update to be called when configuration value on startup', () => {
let mListener: (() => void) | undefined;
vi.spyOn(autoUpdater, 'on').mockImplementation((channel: keyof AppUpdaterEvents, listener: unknown): AppUpdater => {
if (channel === 'update-available') mListener = listener as () => void;
return {} as unknown as AppUpdater;
});

vi.mocked(configurationMock.get).mockReturnValue('startup');

new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();

// call the listener (which should be the private updateAvailableEntry method)
mListener?.();

expect(configurationRegistryMock.getConfiguration).toHaveBeenCalled();
expect(commandRegistryMock.executeCommand).toHaveBeenCalledWith('update');
});

test('expect command update not to be called when configuration value on never', () => {
let mListener: (() => void) | undefined;
vi.spyOn(autoUpdater, 'on').mockImplementation((channel: keyof AppUpdaterEvents, listener: unknown): AppUpdater => {
if (channel === 'update-available') mListener = listener as () => void;
return {} as unknown as AppUpdater;
});

vi.mocked(configurationMock.get).mockReturnValue('never');

new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();

// call the listener (which should be the private updateAvailableEntry method)
mListener?.();

expect(configurationRegistryMock.getConfiguration).toHaveBeenCalled();
expect(commandRegistryMock.executeCommand).not.toHaveBeenCalled();
});

test('clicking on "Update Never" should set the configuration value to never', async () => {
vi.mocked(messageBoxMock.showMessageBox).mockResolvedValue({
response: 2, // Update never
});

let mListener: (() => Promise<void>) | undefined;
vi.mocked(commandRegistryMock.registerCommand).mockImplementation(
(channel: string, listener: () => Promise<void>) => {
if (channel === 'update') mListener = listener;
return Disposable.noop();
},
);

new Updater(messageBoxMock, configurationRegistryMock, statusBarRegistryMock, commandRegistryMock).init();
expect(mListener).toBeDefined();

await mListener?.();

expect(configurationMock.update).toHaveBeenCalledWith('update.reminder', 'never');
});

0 comments on commit e0e2d3b

Please sign in to comment.