1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import { flushSync, mount, unmount } from 'svelte';
import { afterEach, describe, beforeEach, expect, test, vi } from 'vitest';
import ChangePassword from '$lib/components/ChangePassword.svelte';
import axios from 'axios';
vi.mock('axios');
describe('ChangePassword', () => {
afterEach(() => {
vi.restoreAllMocks();
});
test('onsubmit happy path', async () => {
axios.post.mockResolvedValue({ status: 200 });
// Instantiate the component using Svelte's `mount` API
const component = mount(ChangePassword, {
target: document.body, // `document` exists because of jsdom
});
flushSync();
// Set value in all three inputs at once:
const inputs = document.body.querySelectorAll('input[type=password]');
inputs.value = 'pass';
// Click the button, then flush the changes so you can synchronously write expectations
document.body.querySelector('button[type=submit]').click();
flushSync();
// 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);
});
});
|