mocks.d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Intercepts all IPC requests with the given mock handler.
  3. *
  4. * This function can be used when testing tauri frontend applications or when running the frontend in a Node.js context during static site generation.
  5. *
  6. * # Examples
  7. *
  8. * Testing setup using vitest:
  9. * ```js
  10. * import { mockIPC, clearMocks } from "@tauri-apps/api/mocks"
  11. * import { invoke } from "@tauri-apps/api/tauri"
  12. *
  13. * afterEach(() => {
  14. * clearMocks()
  15. * })
  16. *
  17. * test("mocked command", () => {
  18. * mockIPC((cmd, args) => {
  19. * switch (cmd) {
  20. * case "add":
  21. * return (args.a as number) + (args.b as number);
  22. * default:
  23. * break;
  24. * }
  25. * });
  26. *
  27. * expect(invoke('add', { a: 12, b: 15 })).resolves.toBe(27);
  28. * })
  29. * ```
  30. *
  31. * The callback function can also return a Promise:
  32. * ```js
  33. * import { mockIPC, clearMocks } from "@tauri-apps/api/mocks"
  34. * import { invoke } from "@tauri-apps/api/tauri"
  35. *
  36. * afterEach(() => {
  37. * clearMocks()
  38. * })
  39. *
  40. * test("mocked command", () => {
  41. * mockIPC((cmd, args) => {
  42. * if(cmd === "get_data") {
  43. * return fetch("https://example.com/data.json")
  44. * .then((response) => response.json())
  45. * }
  46. * });
  47. *
  48. * expect(invoke('get_data')).resolves.toBe({ foo: 'bar' });
  49. * })
  50. * ```
  51. *
  52. * @since 1.0.0
  53. */
  54. export declare function mockIPC(cb: (cmd: string, args: Record<string, unknown>) => any | Promise<any>): void;
  55. /**
  56. * Mocks one or many window labels.
  57. * In non-tauri context it is required to call this function *before* using the `@tauri-apps/api/window` module.
  58. *
  59. * This function only mocks the *presence* of windows,
  60. * window properties (e.g. width and height) can be mocked like regular IPC calls using the `mockIPC` function.
  61. *
  62. * # Examples
  63. *
  64. * ```js
  65. * import { mockWindows } from "@tauri-apps/api/mocks";
  66. * import { getCurrent } from "@tauri-apps/api/window";
  67. *
  68. * mockWindows("main", "second", "third");
  69. *
  70. * const win = getCurrent();
  71. *
  72. * win.label // "main"
  73. * ```
  74. *
  75. * ```js
  76. * import { mockWindows } from "@tauri-apps/api/mocks";
  77. *
  78. * mockWindows("main", "second", "third");
  79. *
  80. * mockIPC((cmd, args) => {
  81. * if (cmd === "tauri") {
  82. * if (
  83. * args?.__tauriModule === "Window" &&
  84. * args?.message?.cmd === "manage" &&
  85. * args?.message?.data?.cmd?.type === "close"
  86. * ) {
  87. * console.log('closing window!');
  88. * }
  89. * }
  90. * });
  91. *
  92. * const { getCurrent } = await import("@tauri-apps/api/window");
  93. *
  94. * const win = getCurrent();
  95. * await win.close(); // this will cause the mocked IPC handler to log to the console.
  96. * ```
  97. *
  98. * @param current Label of window this JavaScript context is running in.
  99. * @param additionalWindows Label of additional windows the app has.
  100. *
  101. * @since 1.0.0
  102. */
  103. export declare function mockWindows(current: string, ...additionalWindows: string[]): void;
  104. /**
  105. * Mock `convertFileSrc` function
  106. *
  107. *
  108. * @example
  109. * ```js
  110. * import { mockConvertFileSrc } from "@tauri-apps/api/mocks";
  111. * import { convertFileSrc } from "@tauri-apps/api/tauri";
  112. *
  113. * mockConvertFileSrc("windows")
  114. *
  115. * const url = convertFileSrc("C:\\Users\\user\\file.txt")
  116. * ```
  117. *
  118. * @param osName The operating system to mock, can be one of linux, macos, or windows
  119. * @param windowsProtocolScheme The scheme to use on Windows, can be either `http` or `https` and defaults to `https`
  120. *
  121. * @since 1.6.0
  122. */
  123. export declare function mockConvertFileSrc(osName: string, windowsProtocolScheme?: string): void;
  124. /**
  125. * Clears mocked functions/data injected by the other functions in this module.
  126. * When using a test runner that doesn't provide a fresh window object for each test, calling this function will reset tauri specific properties.
  127. *
  128. * # Example
  129. *
  130. * ```js
  131. * import { mockWindows, clearMocks } from "@tauri-apps/api/mocks"
  132. *
  133. * afterEach(() => {
  134. * clearMocks()
  135. * })
  136. *
  137. * test("mocked windows", () => {
  138. * mockWindows("main", "second", "third");
  139. *
  140. * expect(window).toHaveProperty("__TAURI_METADATA__")
  141. * })
  142. *
  143. * test("no mocked windows", () => {
  144. * expect(window).not.toHaveProperty("__TAURI_METADATA__")
  145. * })
  146. * ```
  147. *
  148. * @since 1.0.0
  149. */
  150. export declare function clearMocks(): void;