summaryrefslogtreecommitdiff
path: root/ui/tests/lib/components/MessageInput.svelte.test.js
blob: 3dde5d72de7a45938ab697bfc3319868df18200f (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
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', () => {
  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();
  });
});