@@ -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 */
171203export 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