Skip to content

Commit

Permalink
Merge branch 'dev' into feat/mobile-typed-invoke-args
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Oct 22, 2023
2 parents c0b9e72 + efe7811 commit e9a0947
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-clearmocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": 'patch:bug'
---

No longer crashing in tests without mocks when `clearMocks` is defined in `afterEach` hook.
7 changes: 5 additions & 2 deletions examples/commands/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ <h1>Tauri Commands</h1>
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) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/isolation/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h1>Hello, Tauri!</h1>
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()}`
})
Expand Down
2 changes: 1 addition & 1 deletion examples/parent-window/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion examples/splashscreen/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h1>This is the main window!</h1>
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)
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion examples/streaming/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<source type="video/mp4" />
</video>
<script>
const { invoke, convertFileSrc } = window.__TAURI__.tauri
const { invoke, convertFileSrc } = window.__TAURI__.primitives
const video = document.getElementById('video_source')
const source = document.createElement('source')
invoke('video_uri').then(([scheme, path]) => {
Expand Down
13 changes: 9 additions & 4 deletions tooling/api/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e9a0947

Please sign in to comment.