summaryrefslogtreecommitdiff
path: root/ui/tests/lib/components/ChangePassword.svelte.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'ui/tests/lib/components/ChangePassword.svelte.test.js')
-rw-r--r--ui/tests/lib/components/ChangePassword.svelte.test.js41
1 files changed, 29 insertions, 12 deletions
diff --git a/ui/tests/lib/components/ChangePassword.svelte.test.js b/ui/tests/lib/components/ChangePassword.svelte.test.js
index 09aa992..5773eae 100644
--- a/ui/tests/lib/components/ChangePassword.svelte.test.js
+++ b/ui/tests/lib/components/ChangePassword.svelte.test.js
@@ -1,24 +1,44 @@
import { flushSync, mount, unmount } from 'svelte';
-import { afterEach, describe, beforeEach, expect, test, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import ChangePassword from '$lib/components/ChangePassword.svelte';
import axios from 'axios';
-vi.mock('axios');
+let component;
+
+let mocks = vi.hoisted(() => ({
+ post: vi.fn(),
+}));
describe('ChangePassword', () => {
- afterEach(() => {
- vi.restoreAllMocks();
- });
+ beforeEach(() => {
+ vi.mock('axios', async (importActual) => {
+ const actual = await importActual();
- test('onsubmit happy path', async () => {
- axios.post.mockResolvedValue({ status: 200 });
+ const mockAxios = {
+ default: {
+ ...actual.default,
+ create: vi.fn(() => ({
+ ...actual.default.create(),
+ post: mocks.post,
+ })),
+ },
+ };
- // Instantiate the component using Svelte's `mount` API
- const component = mount(ChangePassword, {
+ return mockAxios;
+ });
+ mocks.post.mockResolvedValue({ status: 200 });
+
+ component = mount(ChangePassword, {
target: document.body, // `document` exists because of jsdom
});
flushSync();
+ });
+ afterEach(() => {
+ unmount(component);
+ });
+
+ test('onsubmit happy path', async () => {
// Set value in all three inputs at once:
const inputs = document.body.querySelectorAll('input[type=password]');
inputs.value = 'pass';
@@ -28,8 +48,5 @@ describe('ChangePassword', () => {
// expect(axios.post).toHaveBeenCalledWith('/password', { password: 'pass', to: 'pass' });
expect(Array.from(inputs.values()).map((i) => i.value)).toEqual(['', '', '']);
-
- // Remove the component from the DOM
- unmount(component);
});
});