From 8a7cc9f8859c14b60da870cee199110e71e0f5da Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Fri, 15 Nov 2024 22:37:39 -0500 Subject: Ratchet up Component test coverage Turns out a number of our components are a pain to run via a mounting test, but Svelte does suggest that this is the dispreferred way to test anyway. Using use:, triggering navigation after logout, and scrollIntoView all make the headless node test process with the janky in-memory mount have a Very Bad Day. But this is still progress! We will press on. --- .../lib/components/MessageInput.svelte.test.js | 49 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'ui/tests/lib/components/MessageInput.svelte.test.js') diff --git a/ui/tests/lib/components/MessageInput.svelte.test.js b/ui/tests/lib/components/MessageInput.svelte.test.js index 597f0c3..d90026c 100644 --- a/ui/tests/lib/components/MessageInput.svelte.test.js +++ b/ui/tests/lib/components/MessageInput.svelte.test.js @@ -1,9 +1,48 @@ -// async onSubmit(event) -// onKeyDown(event) -import { describe, expect, test } from 'vitest'; +import { flushSync, mount, unmount } from 'svelte'; +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; +import MessageInput from '$lib/components/MessageInput.svelte'; +import axios from 'axios'; + +let component; + +let mocks = vi.hoisted(() => ({ + post: vi.fn(), +})); describe('MessageInput', () => { - test('stub', async () => { - expect(true).toBeTruthy(); + 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(MessageInput, { + target: document.body, // `document` exists because of jsdom + }); + flushSync(); + }); + + afterEach(() => { + unmount(component); + }); + + test('onsubmit happy path', async () => { + // Click the button, then flush the changes so you can synchronously write expectations + document.body.querySelector('button[type=submit]').click(); + flushSync(); + + expect(mocks.post).toHaveBeenCalled(); }); }); -- cgit v1.2.3