fix(api): do nothing in clearMocks if __TAURI_INTERNALS__ is not defined (#8071)

This commit is contained in:
Konstantin Azizov
2023-10-21 19:51:08 +02:00
committed by GitHub
parent c6c59cf237
commit 287066b279
2 changed files with 14 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@tauri-apps/api": 'patch:bug'
---
No longer crashing in tests without mocks when `clearMocks` is defined in `afterEach` hook.

View File

@@ -152,11 +152,12 @@ export function mockWindows(
*
* @since 1.6.0
*/
export function mockConvertFileSrc(
osName: string
): void {
export function mockConvertFileSrc(osName: string): void {
window.__TAURI_INTERNALS__ = window.__TAURI_INTERNALS__ ?? {}
window.__TAURI_INTERNALS__.convertFileSrc = function (filePath, protocol = 'asset') {
window.__TAURI_INTERNALS__.convertFileSrc = function (
filePath,
protocol = 'asset'
) {
const path = encodeURIComponent(filePath)
return osName === 'windows'
? `http://${protocol}.localhost/${path}`
@@ -191,6 +192,10 @@ export function mockConvertFileSrc(
* @since 1.0.0
*/
export function clearMocks(): void {
if (typeof window.__TAURI_INTERNALS__ !== 'object') {
return
}
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
delete window.__TAURI_INTERNALS__.convertFileSrc
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case