From 287066b279f503dd09bfd43d5da37d1f471451fb Mon Sep 17 00:00:00 2001 From: Konstantin Azizov Date: Sat, 21 Oct 2023 19:51:08 +0200 Subject: [PATCH 1/2] fix(api): do nothing in clearMocks if __TAURI_INTERNALS__ is not defined (#8071) --- .changes/fix-clearmocks.md | 5 +++++ tooling/api/src/mocks.ts | 13 +++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 .changes/fix-clearmocks.md diff --git a/.changes/fix-clearmocks.md b/.changes/fix-clearmocks.md new file mode 100644 index 000000000000..b788c1f6566d --- /dev/null +++ b/.changes/fix-clearmocks.md @@ -0,0 +1,5 @@ +--- +"@tauri-apps/api": 'patch:bug' +--- + +No longer crashing in tests without mocks when `clearMocks` is defined in `afterEach` hook. diff --git a/tooling/api/src/mocks.ts b/tooling/api/src/mocks.ts index 685d9f279c95..fd47796589a2 100644 --- a/tooling/api/src/mocks.ts +++ b/tooling/api/src/mocks.ts @@ -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 From efe7811804a32c45f55686a9541b44c022cc571e Mon Sep 17 00:00:00 2001 From: Olivier Lemasle Date: Sun, 22 Oct 2023 02:38:18 +0200 Subject: [PATCH 2/2] fix(examples): use __TAURI__.primitives (#8077) Closes #8073 --- examples/commands/index.html | 7 +++++-- examples/isolation/dist/index.html | 2 +- examples/parent-window/index.html | 2 +- examples/splashscreen/dist/index.html | 2 +- examples/streaming/index.html | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/commands/index.html b/examples/commands/index.html index 3abb8cb8a7e7..66841fd70c2c 100644 --- a/examples/commands/index.html +++ b/examples/commands/index.html @@ -15,10 +15,13 @@

Tauri Commands

function runCommand(commandName, args, optional) { const id = optional ? '#response-optional' : '#response' const result = document.querySelector(id) - window.__TAURI__ + window.__TAURI__.primitives .invoke(commandName, args) .then((response) => { - const val = response instanceof ArrayBuffer ? new TextDecoder().decode(response) : response + const val = + response instanceof ArrayBuffer + ? new TextDecoder().decode(response) + : response result.innerText = `Ok(${val})` }) .catch((error) => { diff --git a/examples/isolation/dist/index.html b/examples/isolation/dist/index.html index 7ecca7406639..7af93b540e07 100644 --- a/examples/isolation/dist/index.html +++ b/examples/isolation/dist/index.html @@ -71,7 +71,7 @@

Hello, Tauri!

const ping = document.querySelector("#ping") const pong = document.querySelector('#pong') ping.addEventListener("click", () => { - window.__TAURI__.tauri.invoke("ping") + window.__TAURI__.primitives.invoke("ping") .then(() => { pong.innerText = `ok: ${Date.now()}` }) diff --git a/examples/parent-window/index.html b/examples/parent-window/index.html index 776293db78f8..059607659d71 100644 --- a/examples/parent-window/index.html +++ b/examples/parent-window/index.html @@ -24,7 +24,7 @@ const responseContainer = document.getElementById('response') function runCommand(commandName, args, optional) { - window.__TAURI__ + window.__TAURI__.primitives .invoke(commandName, args) .then((response) => { responseContainer.innerText += `Ok(${response})\n\n` diff --git a/examples/splashscreen/dist/index.html b/examples/splashscreen/dist/index.html index 7e96301cf59e..a51c58d6088d 100644 --- a/examples/splashscreen/dist/index.html +++ b/examples/splashscreen/dist/index.html @@ -6,7 +6,7 @@

This is the main window!

document.addEventListener('DOMContentLoaded', () => { // we delay here just so we can see the splashscreen for a while setTimeout(() => { - window.__TAURI__.invoke('close_splashscreen') + window.__TAURI__.primitives.invoke('close_splashscreen') }, 2000) }) diff --git a/examples/streaming/index.html b/examples/streaming/index.html index edcc5f020d0a..a0eabf610ab7 100644 --- a/examples/streaming/index.html +++ b/examples/streaming/index.html @@ -20,7 +20,7 @@