-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better Mock updates #269
Better Mock updates #269
Conversation
@goosewobbler feel free to adjust the test coverage if necessary, we can always increase it later as well. |
I think this is good for review, the documentation / API deprecation updates can be done on the main PR and we can follow up with more units. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, I have one suggestion on the API design
example-cjs/e2e/api.spec.ts
Outdated
describe('setMock', () => { | ||
it('should mock an electron API function', async () => { | ||
const dialog = await browser.electron.mock('dialog'); | ||
await dialog.setMock('showOpenDialog'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it makes more sense to just call
const mockedShowOpenDialog = await browser.electron.mock('dialog', 'showOpenDialog')
instead of doing this. It would more align to vitest
and jest
APIs so user are more familiar with what they need to do.
Is there any advantage of an additional getMock
/ setMock
call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would call unmock
via:
browser.electron.unmock('dialog', 'showOpenDialog')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll see what I can do here, it does make sense to have a single-liner for mocking (and unmocking) an API function but there is a reason I did it this way -- I was running into trouble getting the mock value to update after being called - because it needs to get the calls from the inner mock in the electron main process and re-apply them. I messed around with getters for a while and some other approaches but wasn't able to make the following work:
const mockedShowOpenDialog = await browser.electron.mock('dialog', 'showOpenDialog');
await browser.electron.execute(
async (electron) =>
await electron.dialog.showOpenDialog({
title: 'my dialog',
properties: ['openFile', 'openDirectory'],
}),
);
expect(mockedShowOpenDialog).toHaveBeenCalledTimes(1);
expect(mockedShowOpenDialog).toHaveBeenCalledWith({
title: 'my dialog',
properties: ['openFile', 'openDirectory'],
});
So, we may be able to condense setMock
into mock
but a separate call to update the outer mock may well be unavoidable (perhaps e.g. mockedShowOpenDialog.update()
), unless there's another way of doing it that I'm not aware of...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getting there...
@goosewobbler I wonder if there is a way to have a bidirectional messaging between service and injected Electron code to have these mocks autoupdate, wdyt? |
@christian-bromann makes sense that could work, I'll merge this to the original PR and work on it there. |
TODO:
@vitest/spy
mock
mockReturnValue
execute
removeMocks