Skip to content

Commit

Permalink
test: add afterCommand unit
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Jan 11, 2024
1 parent 9f35bbe commit 9cc06f8
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions test/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { vi, describe, beforeEach, it, expect } from 'vitest';
import { vi, describe, beforeEach, afterEach, it, expect, Mock } from 'vitest';

Check failure on line 1 in test/service.spec.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'afterEach' is defined but never used

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;
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -108,3 +109,29 @@ describe('beforeTest', () => {
expect(restoreAllMocks).toHaveBeenCalled();
});
});

describe('afterCommand', () => {
let mocks: [string, ElectronMock<any, any>][] = [];

Check warning on line 114 in test/service.spec.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

Unexpected any. Specify a different type

Check warning on line 114 in test/service.spec.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

Unexpected any. Specify a different type

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();
});
});

0 comments on commit 9cc06f8

Please sign in to comment.