From 9cc06f85efac9f63dda15d3aefd2a5ca1b28b732 Mon Sep 17 00:00:00 2001 From: Sam Maister Date: Thu, 11 Jan 2024 21:25:34 +0000 Subject: [PATCH] test: add `afterCommand` unit --- test/service.spec.ts | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/test/service.spec.ts b/test/service.spec.ts index e8e5f86d2..2b76ffa87 100644 --- a/test/service.spec.ts +++ b/test/service.spec.ts @@ -1,11 +1,12 @@ -import { vi, describe, beforeEach, it, expect } from 'vitest'; +import { vi, describe, beforeEach, afterEach, it, expect, Mock } from 'vitest'; import { mockProcessProperty } from './helpers'; import { clearAllMocks } from '../src/commands/clearAllMocks'; import { resetAllMocks } from '../src/commands/resetAllMocks'; import { restoreAllMocks } from '../src/commands/restoreAllMocks'; -import type { BrowserExtension } from '../src'; +import type { BrowserExtension, ElectronMock } from '../src'; import type ElectronWorkerService from '../src/service'; +import mockStore from '../src/mockStore'; let WorkerService: typeof ElectronWorkerService; let instance: ElectronWorkerService | undefined; @@ -66,13 +67,13 @@ describe('before', () => { describe('beforeTest', () => { vi.mock('../src/commands/clearAllMocks', () => ({ - clearAllMocks: vi.fn().mockResolvedValue({}), + clearAllMocks: vi.fn().mockReturnValue({}), })); vi.mock('../src/commands/resetAllMocks', () => ({ - resetAllMocks: vi.fn().mockResolvedValue({}), + resetAllMocks: vi.fn().mockReturnValue({}), })); vi.mock('../src/commands/restoreAllMocks', () => ({ - restoreAllMocks: vi.fn().mockResolvedValue({}), + restoreAllMocks: vi.fn().mockReturnValue({}), })); it('should clear all mocks when `clearMocks` is set', async () => { @@ -108,3 +109,29 @@ describe('beforeTest', () => { expect(restoreAllMocks).toHaveBeenCalled(); }); }); + +describe('afterCommand', () => { + let mocks: [string, ElectronMock][] = []; + + vi.mock('../src/mockStore', () => ({ + default: { + getMocks: vi.fn(), + }, + })); + + beforeEach(() => { + (mockStore.getMocks as Mock).mockImplementation(() => mocks); + }); + + it('should update mocks which are not already updating', async () => { + instance = new WorkerService(); + mocks = [ + ['electron.app.getName', { update: vi.fn().mockResolvedValue({}), updating: false } as unknown as ElectronMock], + ['electron.app.getVersion', { update: vi.fn().mockResolvedValue({}), updating: true } as unknown as ElectronMock], + ]; + await instance.afterCommand('execute', [['look', 'some', 'args']]); + + expect(mocks[0][1].update).toHaveBeenCalled(); + expect(mocks[1][1].update).not.toHaveBeenCalled(); + }); +});