-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: spy on popup apis like alert (#3076)
Co-authored-by: Vladimir <[email protected]>
- Loading branch information
1 parent
efb91e2
commit 280ad1e
Showing
6 changed files
with
109 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const showPopupWarning = <T>(name: string, value: T, defaultValue?: T) => (...params: any[]) => { | ||
const formatedParams = params.map(p => JSON.stringify(p)).join(', ') | ||
|
||
console.warn(`Vitest encountered a \`${name}(${formatedParams})\` call that it cannot handle by default, so it returned \`${value}\`. Read more in https://vitest.dev/guide/browser#thread-blocking-dialogs. | ||
If needed, mock the \`${name}\` call manually like: | ||
\`\`\` | ||
import { expect, vi } from "vitest" | ||
vi.spyOn(window, "${name}")${defaultValue ? `.mockReturnValue(${JSON.stringify(defaultValue)})` : ''} | ||
${name}(${formatedParams}) | ||
expect(${name}).toHaveBeenCalledWith(${formatedParams}) | ||
\`\`\``) | ||
return value | ||
} | ||
|
||
export const setupDialogsSpy = () => { | ||
globalThis.alert = showPopupWarning('alert', undefined) | ||
globalThis.confirm = showPopupWarning('confirm', false, true) | ||
globalThis.prompt = showPopupWarning('prompt', null, 'your value') | ||
} |
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.