Explorar el Código

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
Amr Bashir hace 1 año
padre
commit
2b0212af49
Se han modificado 2 ficheros con 39 adiciones y 0 borrados
  1. 5 0
      .changes/api-convert-file-src-mock.md
  2. 34 0
      tooling/api/src/mocks.ts

+ 5 - 0
.changes/api-convert-file-src-mock.md

@@ -0,0 +1,5 @@
+---
+'@tauri-apps/api': 'minor:feat'
+---
+
+Add `mockConvertFileSrc` in `mocks` module, to mock `convertFileSrc` function.

+ 34 - 0
tooling/api/src/mocks.ts

@@ -142,6 +142,38 @@ export function mockWindows(
   }
 }
 
+/**
+ * Mock `convertFileSrc` function
+ *
+ *
+ * @example
+ * ```js
+ * import { mockConvertFileSrc } from "@tauri-apps/api/mocks";
+ * import { convertFileSrc } from "@tauri-apps/api/tauri";
+ *
+ * mockConvertFileSrc("windows")
+ *
+ * const url = convertFileSrc("C:\\Users\\user\\file.txt")
+ * ```
+ *
+ * @param osName The operating system to mock, can be one of linux, macos, or windows
+ * @param windowsProtocolScheme The scheme to use on Windows, can be either `http` or `https` and defaults to `https`
+ *
+ * @since 1.6.0
+ */
+export function mockConvertFileSrc(
+  osName: string,
+  windowsProtocolScheme = 'https'
+): void {
+  window.__TAURI__ = window.__TAURI__ ?? {}
+  window.__TAURI__.convertFileSrc = function (filePath, protocol = 'asset') {
+    const path = encodeURIComponent(filePath)
+    return osName === 'windows'
+      ? `${windowsProtocolScheme}://${protocol}.localhost/${path}`
+      : `${protocol}://localhost/${path}`
+  }
+}
+
 /**
  * Clears mocked functions/data injected by the other functions in this module.
  * 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(
  * @since 1.0.0
  */
 export function clearMocks(): void {
+  // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
+  delete window.__TAURI__.convertFileSrc
   // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case
   delete window.__TAURI_IPC__
   // @ts-expect-error "The operand of a 'delete' operator must be optional' does not matter in this case