summaryrefslogtreecommitdiff
path: root/ui/tests/lib/components/ChangePassword.svelte.test.js
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-11-19 12:19:07 -0500
committerOwen Jacobson <owen@grimoire.ca>2024-11-19 12:19:07 -0500
commit4f0e3eedbe094ef7877080623073fabcc26e36b3 (patch)
treed2c5640eef086ae9539210519822bd2e39397c30 /ui/tests/lib/components/ChangePassword.svelte.test.js
parent8c0a8e67ef6978388140083f78e38bfa8dedd0de (diff)
parenta5945c50c41a0a41104a0c28ad1279ea3f9255f9 (diff)
Merge remote-tracking branch 'origin/prop/js-tests'
Diffstat (limited to 'ui/tests/lib/components/ChangePassword.svelte.test.js')
-rw-r--r--ui/tests/lib/components/ChangePassword.svelte.test.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/ui/tests/lib/components/ChangePassword.svelte.test.js b/ui/tests/lib/components/ChangePassword.svelte.test.js
new file mode 100644
index 0000000..5773eae
--- /dev/null
+++ b/ui/tests/lib/components/ChangePassword.svelte.test.js
@@ -0,0 +1,52 @@
+import { flushSync, mount, unmount } from 'svelte';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import ChangePassword from '$lib/components/ChangePassword.svelte';
+import axios from 'axios';
+
+let component;
+
+let mocks = vi.hoisted(() => ({
+ post: vi.fn(),
+}));
+
+describe('ChangePassword', () => {
+ beforeEach(() => {
+ vi.mock('axios', async (importActual) => {
+ const actual = await importActual();
+
+ const mockAxios = {
+ default: {
+ ...actual.default,
+ create: vi.fn(() => ({
+ ...actual.default.create(),
+ post: mocks.post,
+ })),
+ },
+ };
+
+ 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';
+ // 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(['', '', '']);
+ });
+});