Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add mock for convertFileSrc, closes #7935 #7961

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amrbashir can we change this to a patch so we can release the Debian package hotfix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got to have selective release in our workflow, this is not scalable IMO, but I will revert this bump.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we should have that :( it's always so boring.

---

Add `mockConvertFileSrc` in `mocks` module, to mock `convertFileSrc` function.
34 changes: 34 additions & 0 deletions tooling/api/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
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 +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
Expand Down
Loading