Skip to content

Commit 2b0212a

Browse files
authored
feat(api): add mock for convertFileSrc, closes #7935 (#7961)
* feat(api): add mock for `convertFileSrc`, closes #7935 * fix lint * Update tooling/api/src/mocks.ts * fmt
1 parent dfe0bad commit 2b0212a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'minor:feat'
3+
---
4+
5+
Add `mockConvertFileSrc` in `mocks` module, to mock `convertFileSrc` function.

tooling/api/src/mocks.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,38 @@ export function mockWindows(
142142
}
143143
}
144144

145+
/**
146+
* Mock `convertFileSrc` function
147+
*
148+
*
149+
* @example
150+
* ```js
151+
* import { mockConvertFileSrc } from "@tauri-apps/api/mocks";
152+
* import { convertFileSrc } from "@tauri-apps/api/tauri";
153+
*
154+
* mockConvertFileSrc("windows")
155+
*
156+
* const url = convertFileSrc("C:\\Users\\user\\file.txt")
157+
* ```
158+
*
159+
* @param osName The operating system to mock, can be one of linux, macos, or windows
160+
* @param windowsProtocolScheme The scheme to use on Windows, can be either `http` or `https` and defaults to `https`
161+
*
162+
* @since 1.6.0
163+
*/
164+
export function mockConvertFileSrc(
165+
osName: string,
166+
windowsProtocolScheme = 'https'
167+
): void {
168+
window.__TAURI__ = window.__TAURI__ ?? {}
169+
window.__TAURI__.convertFileSrc = function (filePath, protocol = 'asset') {
170+
const path = encodeURIComponent(filePath)
171+
return osName === 'windows'
172+
? `${windowsProtocolScheme}://${protocol}.localhost/${path}`
173+
: `${protocol}://localhost/${path}`
174+
}
175+
}
176+
145177
/**
146178
* Clears mocked functions/data injected by the other functions in this module.
147179
* When using a test runner that doesn't provide a fresh window object for each test, calling this function will reset tauri specific properties.
@@ -169,6 +201,8 @@ export function mockWindows(
169201
* @since 1.0.0
170202
*/
171203
export function clearMocks(): void {
204+
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
205+
delete window.__TAURI__.convertFileSrc
172206
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
173207
delete window.__TAURI_IPC__
174208
// @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case

0 commit comments

Comments
 (0)