aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/inputs/ChipInput.test.tsx
blob: cb52ee30c7517cef92e7c154ddeff4eeffbd8f01 (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 { rawRender, screen } from "@/tests";
import userEvent from "@testing-library/user-event";
import { describe, it, vitest } from "vitest";
import ChipInput from "./ChipInput";

describe("ChipInput", () => {
  const existedValues = ["value_1", "value_2"];

  // TODO: Support default value
  it.skip("should works with default value", () => {
    rawRender(<ChipInput defaultValue={existedValues}></ChipInput>);

    existedValues.forEach((value) => {
      expect(screen.getByText(value)).toBeDefined();
    });
  });

  it("should works with value", () => {
    rawRender(<ChipInput value={existedValues}></ChipInput>);

    existedValues.forEach((value) => {
      expect(screen.getByText(value)).toBeDefined();
    });
  });

  it.skip("should allow user creates new value", async () => {
    const typedValue = "value_3";
    const mockedFn = vitest.fn((values: string[]) => {
      expect(values).toContain(typedValue);
    });

    rawRender(
      <ChipInput value={existedValues} onChange={mockedFn}></ChipInput>,
    );

    const element = screen.getByRole("searchbox");

    await userEvent.type(element, typedValue);

    expect(element).toHaveValue(typedValue);

    const createBtn = screen.getByText(`Add "${typedValue}"`);

    await userEvent.click(createBtn);

    expect(mockedFn).toBeCalledTimes(1);
  });
});