Skip to content

Commit

Permalink
test: bring over the latest api.spec
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Jul 23, 2024
1 parent ee3a4aa commit cd53c7b
Showing 1 changed file with 134 additions and 20 deletions.
154 changes: 134 additions & 20 deletions test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ describe('clearAllMocks', () => {
await browser.electron.clearAllMocks();

expect(mockSetName.mock.calls).toStrictEqual([]);
expect(mockSetName.mock.instances).toStrictEqual([]);
expect(mockSetName.mock.invocationCallOrder).toStrictEqual([]);
expect(mockSetName.mock.lastCall).toBeUndefined();
expect(mockSetName.mock.results).toStrictEqual([]);

expect(mockWriteText.mock.calls).toStrictEqual([]);
expect(mockWriteText.mock.instances).toStrictEqual([]);
expect(mockWriteText.mock.invocationCallOrder).toStrictEqual([]);
expect(mockWriteText.mock.lastCall).toBeUndefined();
expect(mockWriteText.mock.results).toStrictEqual([]);
Expand All @@ -98,13 +96,11 @@ describe('clearAllMocks', () => {
await browser.electron.clearAllMocks('app');

expect(mockSetName.mock.calls).toStrictEqual([]);
expect(mockSetName.mock.instances).toStrictEqual([]);
expect(mockSetName.mock.invocationCallOrder).toStrictEqual([]);
expect(mockSetName.mock.lastCall).toBeUndefined();
expect(mockSetName.mock.results).toStrictEqual([]);

expect(mockWriteText.mock.calls).toStrictEqual([['text to be written']]);
expect(mockWriteText.mock.instances).toStrictEqual([expect.any(Function)]);
expect(mockWriteText.mock.invocationCallOrder).toStrictEqual([expect.any(Number)]);
expect(mockWriteText.mock.lastCall).toStrictEqual(['text to be written']);
expect(mockWriteText.mock.results).toStrictEqual([{ type: 'return', value: undefined }]);
Expand Down Expand Up @@ -152,13 +148,11 @@ describe('resetAllMocks', () => {
await browser.electron.resetAllMocks();

expect(mockGetName.mock.calls).toStrictEqual([]);
expect(mockGetName.mock.instances).toStrictEqual([]);
expect(mockGetName.mock.invocationCallOrder).toStrictEqual([]);
expect(mockGetName.mock.lastCall).toBeUndefined();
expect(mockGetName.mock.results).toStrictEqual([]);

expect(mockReadText.mock.calls).toStrictEqual([]);
expect(mockReadText.mock.instances).toStrictEqual([]);
expect(mockReadText.mock.invocationCallOrder).toStrictEqual([]);
expect(mockReadText.mock.lastCall).toBeUndefined();
expect(mockReadText.mock.results).toStrictEqual([]);
Expand All @@ -174,13 +168,11 @@ describe('resetAllMocks', () => {
await browser.electron.resetAllMocks('app');

expect(mockSetName.mock.calls).toStrictEqual([]);
expect(mockSetName.mock.instances).toStrictEqual([]);
expect(mockSetName.mock.invocationCallOrder).toStrictEqual([]);
expect(mockSetName.mock.lastCall).toBeUndefined();
expect(mockSetName.mock.results).toStrictEqual([]);

expect(mockWriteText.mock.calls).toStrictEqual([['text to be written']]);
expect(mockWriteText.mock.instances).toStrictEqual([expect.any(Function)]);
expect(mockWriteText.mock.invocationCallOrder).toStrictEqual([expect.any(Number)]);
expect(mockWriteText.mock.lastCall).toStrictEqual(['text to be written']);
expect(mockWriteText.mock.results).toStrictEqual([{ type: 'return', value: undefined }]);
Expand Down Expand Up @@ -252,6 +244,24 @@ describe('restoreAllMocks', () => {
});
});

describe('isMockFunction', () => {
it('should return true when provided with an electron mock', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

expect(browser.electron.isMockFunction(mockGetName)).toBe(true);
});

it('should return false when provided with a function', async () => {
expect(browser.electron.isMockFunction(() => {})).toBe(false);
});

it('should return false when provided with a vitest mock', async () => {
// We have to dynamic import `@vitest/spy` due to it being an ESM only module
const spy = await import('@vitest/spy');
expect(browser.electron.isMockFunction(spy.fn())).toBe(false);
});
});

describe('execute', () => {
it('should execute an arbitrary function in the electron main process', async () => {
expect(
Expand All @@ -268,7 +278,7 @@ describe('execute', () => {
});

it('should execute a string-based function in the electron main process', async () => {
expect(await browser.electron.execute('return 1 + 2 + 3')).toEqual(6);
await expect(browser.electron.execute('return 1 + 2 + 3')).resolves.toEqual(6);
});
});

Expand Down Expand Up @@ -452,7 +462,6 @@ describe('mock object functionality', () => {
await mockShowOpenDialog.mockClear();

expect(mockShowOpenDialog.mock.calls).toStrictEqual([]);
expect(mockShowOpenDialog.mock.instances).toStrictEqual([]);
expect(mockShowOpenDialog.mock.invocationCallOrder).toStrictEqual([]);
expect(mockShowOpenDialog.mock.lastCall).toBeUndefined();
expect(mockShowOpenDialog.mock.results).toStrictEqual([]);
Expand Down Expand Up @@ -513,7 +522,6 @@ describe('mock object functionality', () => {
await mockShowOpenDialog.mockReset();

expect(mockShowOpenDialog.mock.calls).toStrictEqual([]);
expect(mockShowOpenDialog.mock.instances).toStrictEqual([]);
expect(mockShowOpenDialog.mock.invocationCallOrder).toStrictEqual([]);
expect(mockShowOpenDialog.mock.lastCall).toBeUndefined();
expect(mockShowOpenDialog.mock.results).toStrictEqual([]);
Expand Down Expand Up @@ -560,6 +568,13 @@ describe('mock object functionality', () => {

expect(mockImpl()).toBe('mocked name');
});

it('should retrieve an empty mock implementation', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');
const mockImpl = mockGetName.getMockImplementation() as () => undefined;

expect(mockImpl()).toBeUndefined();
});
});

describe('mockReturnThis', () => {
Expand Down Expand Up @@ -587,9 +602,12 @@ describe('mock object functionality', () => {
);

expect(withImplementationResult).toBe('temporary mock name');
expect(await browser.electron.execute((electron) => electron.app.getName())).toBe('first mock name');
expect(await browser.electron.execute((electron) => electron.app.getName())).toBe('second mock name');
expect(await browser.electron.execute((electron) => electron.app.getName())).toBe('default mock name');
const firstName = await browser.electron.execute((electron) => electron.app.getName());
expect(firstName).toBe('first mock name');
const secondName = await browser.electron.execute((electron) => electron.app.getName());
expect(secondName).toBe('second mock name');
const thirdName = await browser.electron.execute((electron) => electron.app.getName());
expect(thirdName).toBe('default mock name');
});

it('should handle promises', async () => {
Expand All @@ -603,15 +621,111 @@ describe('mock object functionality', () => {
);

expect(withImplementationResult).toBe('temporary mock icon');
expect(await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'))).toBe(
'first mock icon',
const firstIcon = await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'));
expect(firstIcon).toBe('first mock icon');
const secondIcon = await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'));
expect(secondIcon).toBe('second mock icon');
const thirdIcon = await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'));
expect(thirdIcon).toBe('default mock icon');
});
});

describe('mock.calls', () => {
it('should return the calls of the mock execution', async () => {
const mockGetFileIcon = await browser.electron.mock('app', 'getFileIcon');

await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'));
await browser.electron.execute((electron) =>
electron.app.getFileIcon('/path/to/another/icon', { size: 'small' }),
);
expect(await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'))).toBe(
'second mock icon',

expect(mockGetFileIcon.mock.calls).toStrictEqual([
['/path/to/icon'], // first call
['/path/to/another/icon', { size: 'small' }], // second call
]);
});

it('should return an empty array when the mock was never invoked', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

expect(mockGetName.mock.calls).toStrictEqual([]);
});
});

describe('mock.lastCall', () => {
it('should return the last call of the mock execution', async () => {
const mockGetFileIcon = await browser.electron.mock('app', 'getFileIcon');

await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'));
expect(mockGetFileIcon.mock.lastCall).toStrictEqual(['/path/to/icon']);
await browser.electron.execute((electron) =>
electron.app.getFileIcon('/path/to/another/icon', { size: 'small' }),
);
expect(await browser.electron.execute((electron) => electron.app.getFileIcon('/path/to/icon'))).toBe(
'default mock icon',
expect(mockGetFileIcon.mock.lastCall).toStrictEqual(['/path/to/another/icon', { size: 'small' }]);
await browser.electron.execute((electron) =>
electron.app.getFileIcon('/path/to/a/massive/icon', { size: 'large' }),
);
expect(mockGetFileIcon.mock.lastCall).toStrictEqual(['/path/to/a/massive/icon', { size: 'large' }]);
});

it('should return undefined when the mock was never invoked', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

expect(mockGetName.mock.lastCall).toBeUndefined();
});
});

describe('mock.results', () => {
it('should return the results of the mock execution', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

// TODO: why does `mockReturnValueOnce` not work for returning 'result' here?
await mockGetName.mockImplementationOnce(() => 'result');
await mockGetName.mockImplementation(() => {
throw new Error('thrown error');
});

await expect(browser.electron.execute((electron) => electron.app.getName())).resolves.toBe('result');
await expect(browser.electron.execute((electron) => electron.app.getName())).rejects.toThrow('thrown error');

expect(mockGetName.mock.results).toStrictEqual([
{
type: 'return',
value: 'result',
},
{
type: 'throw',
value: new Error('thrown error'),
},
]);
});

it('should return an empty array when the mock was never invoked', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

expect(mockGetName.mock.results).toStrictEqual([]);
});
});

describe('mock.invocationCallOrder', () => {
it('should return the order of execution', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');
const mockGetVersion = await browser.electron.mock('app', 'getVersion');

await browser.electron.execute((electron) => electron.app.getName());
await browser.electron.execute((electron) => electron.app.getVersion());
await browser.electron.execute((electron) => electron.app.getName());

const firstInvocationIndex = mockGetName.mock.invocationCallOrder[0];

expect(mockGetName.mock.invocationCallOrder).toStrictEqual([firstInvocationIndex, firstInvocationIndex + 2]);
expect(mockGetVersion.mock.invocationCallOrder).toStrictEqual([firstInvocationIndex + 1]);
});

it('should return an empty array when the mock was never invoked', async () => {
const mockGetName = await browser.electron.mock('app', 'getName');

expect(mockGetName.mock.invocationCallOrder).toStrictEqual([]);
});
});
});

0 comments on commit cd53c7b

Please sign in to comment.