-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from webdriverio-community/cb/better-mock
- Loading branch information
Showing
38 changed files
with
7,261 additions
and
1,796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Electron Forge doesn't tolerate PNPM symlinks | ||
# can be removed when https://github.com/electron/forge/pull/3351 is merged | ||
# can be removed when https://github.com/electron/forge/issues/2633 is fixed | ||
node-linker=hoisted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,154 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { browser } from '@wdio/globals'; | ||
|
||
import { expect } from '@wdio/globals'; | ||
import { browser } from 'wdio-electron-service'; | ||
|
||
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' })); | ||
const { name, version } = packageJson; | ||
|
||
describe('electron APIs', () => { | ||
describe('app', () => { | ||
it('should retrieve app metadata through the electron API', async () => { | ||
const appName = await browser.electron.app('getName'); | ||
expect(appName).toEqual(name); | ||
const appVersion = await browser.electron.app('getVersion'); | ||
expect(appVersion).toEqual(version); | ||
}); | ||
const { version: appVersion } = packageJson as { name: string; version: string }; | ||
|
||
describe('mocking', () => { | ||
afterEach(async () => { | ||
await browser.electron.removeMocks(); | ||
}); | ||
|
||
describe('browserWindow', () => { | ||
it('should retrieve the window title through the electron API', async () => { | ||
let windowTitle; | ||
await browser.waitUntil( | ||
async () => { | ||
windowTitle = await browser.electron.browserWindow('title'); | ||
if (windowTitle !== 'this is the title of the main window') { | ||
return false; | ||
} | ||
|
||
return windowTitle; | ||
}, | ||
{ | ||
timeoutMsg: 'Window title not updated', | ||
}, | ||
describe('mock', () => { | ||
it('should mock an electron API function', async () => { | ||
const showOpenDialog = await browser.electron.mock('dialog', 'showOpenDialog'); | ||
await browser.electron.execute( | ||
async (electron) => | ||
await electron.dialog.showOpenDialog({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}), | ||
); | ||
expect(windowTitle).toEqual('this is the title of the main window'); | ||
|
||
const mockedShowOpenDialog = await showOpenDialog.update(); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledTimes(1); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledWith({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('custom', () => { | ||
it('should return the expected response', async () => { | ||
const result = await browser.electron.api(); | ||
expect(result).toEqual('test'); | ||
it('should mock a synchronous electron API function', async () => { | ||
const showOpenDialogSync = await browser.electron.mock('dialog', 'showOpenDialogSync'); | ||
await browser.electron.execute((electron) => | ||
electron.dialog.showOpenDialogSync({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}), | ||
); | ||
|
||
const mockedShowOpenDialogSync = await showOpenDialogSync.update(); | ||
expect(mockedShowOpenDialogSync).toHaveBeenCalledTimes(1); | ||
expect(mockedShowOpenDialogSync).toHaveBeenCalledWith({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('mainProcess', () => { | ||
it('should retrieve the process type through the electron API', async () => { | ||
const processType = await browser.electron.mainProcess('type'); | ||
expect(processType).toEqual('browser'); | ||
describe('mockImplementation', () => { | ||
it('should mock an electron API function', async () => { | ||
const showOpenDialog = await browser.electron.mock('dialog', 'showOpenDialog'); | ||
let callsCount = 0; | ||
await showOpenDialog.mockImplementation(() => callsCount++); | ||
await browser.electron.execute( | ||
async (electron) => | ||
await electron.dialog.showOpenDialog({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}), | ||
); | ||
|
||
const mockedShowOpenDialog = await showOpenDialog.update(); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledTimes(1); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledWith({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}); | ||
expect(callsCount).toBe(1); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('mock', () => { | ||
it('should mock the expected electron API function', async () => { | ||
await browser.electron.mock('dialog', 'showOpenDialog', 'I opened a dialog!'); | ||
const result = await browser.electron.dialog('showOpenDialog'); | ||
expect(result).toEqual('I opened a dialog!'); | ||
describe('mockReturnValue', () => { | ||
it('should return the expected value from the mock API', async () => { | ||
const mockGetName = await browser.electron.mock('app', 'getName'); | ||
await mockGetName.mockReturnValue('This is a mock'); | ||
|
||
const name = await browser.electron.execute((electron) => electron.app.getName()); | ||
|
||
expect(name).toBe('This is a mock'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('mockAll', () => { | ||
it('should mock all functions on an API', async () => { | ||
const mockedDialog = await browser.electron.mockAll('dialog'); | ||
await browser.electron.execute( | ||
async (electron) => | ||
await electron.dialog.showOpenDialog({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}), | ||
); | ||
await browser.electron.execute((electron) => | ||
electron.dialog.showOpenDialogSync({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}), | ||
); | ||
|
||
it('should mock the expected synchronous electron API function', async () => { | ||
await browser.electron.mock('dialog', 'showOpenDialogSync', 'I opened a dialog!'); | ||
const result = await browser.electron.dialog('showOpenDialogSync'); | ||
expect(result).toEqual('I opened a dialog!'); | ||
const mockedShowOpenDialog = await mockedDialog.showOpenDialog.update(); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledTimes(1); | ||
expect(mockedShowOpenDialog).toHaveBeenCalledWith({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}); | ||
const mockedShowOpenDialogSync = await mockedDialog.showOpenDialogSync.update(); | ||
expect(mockedShowOpenDialogSync).toHaveBeenCalledTimes(1); | ||
expect(mockedShowOpenDialogSync).toHaveBeenCalledWith({ | ||
title: 'my dialog', | ||
properties: ['openFile', 'openDirectory'], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should allow to execute an arbitrary function in the main process', async () => { | ||
expect( | ||
await browser.electron.execute( | ||
(electron, a, b, c) => { | ||
const win = electron.BrowserWindow.getFocusedWindow(); | ||
return [typeof win, a + b + c]; | ||
}, | ||
1, | ||
2, | ||
3, | ||
), | ||
).toEqual(['object', 6]); | ||
|
||
expect(await browser.electron.execute('return 1 + 2 + 3')).toBe(6); | ||
describe('unMock', () => { | ||
it('should remove an existing mock', async () => { | ||
const getVersion = await browser.electron.mock('app', 'getVersion', 'mocked version'); | ||
let version = await browser.electron.execute((electron) => electron.app.getVersion()); | ||
expect(version).toBe('mocked version'); | ||
|
||
await getVersion.unMock(); | ||
|
||
expect(async () => await getVersion.update()).rejects.toThrowError( | ||
'No mock registered for "electron.app.getVersion"', | ||
); | ||
|
||
version = await browser.electron.execute((electron) => electron.app.getVersion()); | ||
expect(version).toBe(appVersion); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should execute an arbitrary function in the main process', async () => { | ||
expect( | ||
await browser.electron.execute( | ||
(electron, a, b, c) => { | ||
const version = electron.app.getVersion(); | ||
return [version, a + b + c]; | ||
}, | ||
1, | ||
2, | ||
3, | ||
), | ||
).toEqual([appVersion, 6]); | ||
}); | ||
|
||
it('should execute a string-based function in the main process', async () => { | ||
expect(await browser.electron.execute('return 1 + 2 + 3')).toBe(6); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.