Skip to content

Commit b7d5717

Browse files
committed
add Jest tests for ColorField
1 parent 9cf88ab commit b7d5717

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
4+
5+
import "@testing-library/jest-dom";
6+
7+
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
8+
9+
import { ColorField } from "./ColorField";
10+
11+
describe("ColorField", () => {
12+
describe("rendering", () => {
13+
it("renders without crashing, and correct component CSS class is applied", () => {
14+
const { container } = render(<ColorField />);
15+
expect(container).not.toBeEmptyDOMElement();
16+
expect(container.getElementsByClassName(`${eccgui}-colorfield`).length).toBe(1);
17+
});
18+
19+
it("renders a color input by default (no palette presets)", () => {
20+
const { container } = render(
21+
<ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />
22+
);
23+
expect(container.querySelector("input[type='color']")).toBeInTheDocument();
24+
});
25+
26+
// Jest cannot test this because it does not (cannot) load Styles where the palette isconfigured
27+
/*
28+
it("renders a readonly text input when palette colors are configured, and custom picker CSS class is applied", () => {
29+
const { container } = render(<ColorField className="my-custom-class" />);
30+
// With default palette settings, a text input with readOnly is shown
31+
expect(container.querySelector("input[type='text']")).toBeInTheDocument();
32+
expect(container.querySelector("input[readonly]")).toBeInTheDocument();
33+
expect(container.querySelector(`.${eccgui}-colorfield--custom-picker`)).toBeInTheDocument();
34+
});
35+
*/
36+
37+
it("applies additional className", () => {
38+
render(
39+
<ColorField
40+
className="my-custom-class"
41+
includePaletteGroup={[]}
42+
includeColorWeight={[]}
43+
allowCustomColor={true}
44+
/>
45+
);
46+
expect(document.querySelector(".my-custom-class")).toBeInTheDocument();
47+
});
48+
});
49+
50+
describe("value handling", () => {
51+
it("uses defaultValue as initial color", () => {
52+
render(
53+
<ColorField
54+
defaultValue="#ff0000"
55+
includePaletteGroup={[]}
56+
includeColorWeight={[]}
57+
allowCustomColor={true}
58+
/>
59+
);
60+
const input = document.querySelector("input") as HTMLInputElement;
61+
expect(input.value).toBe("#ff0000");
62+
});
63+
64+
it("uses value prop as initial color", () => {
65+
render(
66+
<ColorField value="#00ff00" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />
67+
);
68+
const input = document.querySelector("input") as HTMLInputElement;
69+
expect(input.value).toBe("#00ff00");
70+
});
71+
72+
it("falls back to #000000 when no value or defaultValue is provided", () => {
73+
render(<ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />);
74+
const input = document.querySelector("input") as HTMLInputElement;
75+
expect(input.value).toBe("#000000");
76+
});
77+
78+
it("updates displayed value when value prop changes", () => {
79+
const { rerender } = render(
80+
<ColorField value="#ff0000" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />
81+
);
82+
let input = document.querySelector("input") as HTMLInputElement;
83+
expect(input.value).toBe("#ff0000");
84+
85+
rerender(
86+
<ColorField value="#0000ff" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />
87+
);
88+
input = document.querySelector("input") as HTMLInputElement;
89+
expect(input.value).toBe("#0000ff");
90+
});
91+
});
92+
93+
describe("disabled state", () => {
94+
it("is disabled when disabled prop is true", () => {
95+
render(<ColorField disabled includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />);
96+
const input = document.querySelector("input") as HTMLInputElement;
97+
expect(input).toBeDisabled();
98+
});
99+
100+
it("is disabled when no palette colors and allowCustomColor is false", () => {
101+
render(<ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={false} />);
102+
const input = document.querySelector("input") as HTMLInputElement;
103+
expect(input).toBeDisabled();
104+
});
105+
});
106+
107+
describe("onChange callback", () => {
108+
it("calls onChange when native color input changes", async () => {
109+
const user = userEvent.setup();
110+
const onChange = jest.fn();
111+
render(
112+
<ColorField
113+
onChange={onChange}
114+
includePaletteGroup={[]}
115+
includeColorWeight={[]}
116+
allowCustomColor={true}
117+
/>
118+
);
119+
const input = document.querySelector("input[type='color']") as HTMLInputElement;
120+
input.type = "text"; // for unknown reasons Jest seems not able to test it on color inputs
121+
await user.type(input, "#123456");
122+
expect(onChange).toHaveBeenCalled();
123+
});
124+
});
125+
});

0 commit comments

Comments
 (0)