@bigbinary/neetoui 3.2.71 → 3.2.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,83 @@
1
+ import React, { useState } from "react";
2
+ import { Radio } from "../lib/components";
3
+ import { render } from "@testing-library/react";
4
+ import userEvent from "@testing-library/user-event";
5
+
6
+ const ControlledRadio = () => {
7
+ const [value, setValue] = useState("");
8
+
9
+ return (
10
+ <Radio
11
+ label="Radio"
12
+ onChange={(e) => setValue(e.target.value)}
13
+ value={value}
14
+ >
15
+ <Radio.Item label="option1" name="options" value="option1" />
16
+ <Radio.Item label="option2" name="options" value="option2" />
17
+ </Radio>
18
+ );
19
+ };
20
+
21
+ describe("Radio", () => {
22
+ it("should render without error", () => {
23
+ const { getByText } = render(
24
+ <Radio label="Radio">
25
+ <Radio.Item label="option1" name="options" value="option1" />
26
+ </Radio>
27
+ );
28
+ expect(getByText("Radio")).toBeInTheDocument();
29
+ });
30
+
31
+ it("should call onChange when radio value is changed", () => {
32
+ const onChange = jest.fn((event) => {
33
+ expect(event.target.value).toBe("option1");
34
+ });
35
+ const { getByText } = render(
36
+ <Radio label="Radio" onChange={onChange}>
37
+ <Radio.Item label="option1" name="options" value="option1" />
38
+ </Radio>
39
+ );
40
+ userEvent.click(getByText("option1"));
41
+ expect(onChange).toHaveBeenCalledTimes(1);
42
+ });
43
+
44
+ it("should display error message", () => {
45
+ const { getByText } = render(
46
+ <Radio label="Radio" error="Error message">
47
+ <Radio.Item label="option1" name="options" value="option1" />
48
+ </Radio>
49
+ );
50
+ expect(getByText("Error message")).toBeInTheDocument();
51
+ });
52
+
53
+ it("should be unchecked by default", () => {
54
+ const { getByRole } = render(
55
+ <Radio label="Radio">
56
+ <Radio.Item label="option1" name="options" value="option1" />
57
+ </Radio>
58
+ );
59
+ expect(getByRole("radio")).not.toBeChecked();
60
+ });
61
+
62
+ it("should show controlled behaviour", () => {
63
+ const { getAllByRole } = render(<ControlledRadio />);
64
+ const radio = getAllByRole("radio");
65
+ userEvent.click(radio[0]);
66
+ expect(radio[0]).toBeChecked();
67
+ expect(radio[1]).not.toBeChecked();
68
+ userEvent.click(radio[1]);
69
+ expect(radio[0]).not.toBeChecked();
70
+ expect(radio[1]).toBeChecked();
71
+ });
72
+
73
+ it("should be checked on clicking the radio item", () => {
74
+ const { getByRole } = render(
75
+ <Radio label="Radio">
76
+ <Radio.Item label="option1" name="options" value="option1" />
77
+ </Radio>
78
+ );
79
+ const radio = getByRole("radio");
80
+ userEvent.click(radio);
81
+ expect(radio).toBeChecked();
82
+ });
83
+ });
@@ -0,0 +1,85 @@
1
+ import React from "react";
2
+ import { Select } from "../lib/components";
3
+ import { render } from "@testing-library/react";
4
+ import userEvent from "@testing-library/user-event";
5
+
6
+ const options = [
7
+ {
8
+ label: "Option 1",
9
+ value: "option-1",
10
+ },
11
+ {
12
+ label: "Option 2",
13
+ value: "option-2",
14
+ },
15
+ ];
16
+
17
+ describe("Select", () => {
18
+ it("should render without error", () => {
19
+ const { getByText } = render(<Select label="Select" options={options} />);
20
+ expect(getByText("Select")).toBeInTheDocument();
21
+ });
22
+
23
+ it("should show option list on clicking", () => {
24
+ const { getByRole, getByText } = render(
25
+ <Select label="Select" options={options} />
26
+ );
27
+ const select = getByRole("combobox");
28
+ userEvent.click(select);
29
+ expect(getByText("Option 1")).toBeInTheDocument();
30
+ expect(getByText("Option 2")).toBeInTheDocument();
31
+ });
32
+
33
+ it("should call onChange on select option", () => {
34
+ const onChange = jest.fn();
35
+ const { getByRole, getByText } = render(
36
+ <Select label="Select" options={options} onChange={onChange} />
37
+ );
38
+ const select = getByRole("combobox");
39
+ userEvent.click(select);
40
+ userEvent.click(getByText("Option 2"));
41
+ expect(onChange).toHaveBeenCalledTimes(1);
42
+ });
43
+
44
+ it("should change selected option value when an option is selected", async () => {
45
+ const { getByRole, getByText } = render(
46
+ <Select label="Select" options={options} />
47
+ );
48
+ const select = getByRole("combobox");
49
+ userEvent.click(select);
50
+ userEvent.click(getByText("Option 2"));
51
+ expect(getByText("Option 2")).toBeInTheDocument();
52
+ });
53
+
54
+ it("should not render label if label is not provided", () => {
55
+ const { queryByTestId } = render(<Select options={options} />);
56
+ expect(queryByTestId("select-label")).not.toBeInTheDocument();
57
+ });
58
+
59
+ it("should show error message if provided", () => {
60
+ const { getByText, getByTestId } = render(
61
+ <Select label="Select" options={options} error="Error message" />
62
+ );
63
+ expect(getByTestId("select-error")).toBeInTheDocument();
64
+ expect(getByText("Error message")).toBeInTheDocument();
65
+ });
66
+
67
+ it("should show help text if provided", () => {
68
+ const { getByText, getByTestId } = render(
69
+ <Select label="Select" options={options} helpText="Help text" />
70
+ );
71
+ expect(getByTestId("select-help-text")).toBeInTheDocument();
72
+ expect(getByText("Help text")).toBeInTheDocument();
73
+ });
74
+
75
+ test("creatable Select should create new element", () => {
76
+ const { getByRole, getByTestId } = render(<Select isCreateable />);
77
+ const select = getByRole("combobox");
78
+ const selectBox = getByTestId("select");
79
+ userEvent.click(select);
80
+ expect(selectBox).toHaveTextContent("No options", { exact: false });
81
+ userEvent.type(select, "hello");
82
+ userEvent.type(select, "{enter}");
83
+ expect(selectBox).toHaveTextContent("hello", { exact: false });
84
+ });
85
+ });