Skip to content

Commit

Permalink
feat(api): add mock for convertFileSrc, closes #7935
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Oct 5, 2023
1 parent cdd5516 commit a5f75ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/api-convert-file-src-mock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'minor:feat'
---

Add `mockConvertFileSrc` in `mocks` module, to mock `convertFileSrc` function.
35 changes: 35 additions & 0 deletions tooling/api/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import { convertFileSrc } from './tauri'

Check failure on line 5 in tooling/api/src/mocks.ts

View workflow job for this annotation

GitHub Actions / eslint-api

'convertFileSrc' is defined but never used

interface IPCMessage {
cmd: string
callback: number
Expand Down Expand Up @@ -142,6 +144,37 @@ export function mockWindows(
}
}

/**
* Mock {@linkcode 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(

Check failure on line 166 in tooling/api/src/mocks.ts

View workflow job for this annotation

GitHub Actions / eslint-api

Missing return type on function
osName: string,
windowsProtocolScheme = 'https'
) {
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.
Expand Down Expand Up @@ -169,6 +202,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
Expand Down

0 comments on commit a5f75ed

Please sign in to comment.