Skip to content

Commit cd9c9cf

Browse files
test(color-picker-web): refactor tests to improve snapshot readability
1 parent 025b508 commit cd9c9cf

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed

packages/pluggableWidgets/color-picker-web/src/components/__tests__/ColorPicker.spec.tsx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ import userEvent, { UserEvent } from "@testing-library/user-event";
44
import { createElement } from "react";
55
import { ColorPicker, ColorPickerProps } from "../ColorPicker";
66

7-
jest.mock("react-color/lib", () => {
8-
const actualModule = jest.requireActual("react-color/lib");
9-
return {
10-
...actualModule,
11-
SketchPicker: () => <div className="sketch-picker" />
12-
};
13-
});
14-
157
describe("ColorPicker", () => {
168
let colorPickerProps: ColorPickerProps;
179
let user: UserEvent;
@@ -44,7 +36,7 @@ describe("ColorPicker", () => {
4436

4537
it("that is disabled renders with the structure", () => {
4638
const colorPickerComponent = renderColorPicker({ disabled: true });
47-
expect(colorPickerComponent.asFragment).toMatchSnapshot();
39+
expect(colorPickerComponent.asFragment()).toMatchSnapshot();
4840
});
4941

5042
it("renders picker with pre-defined default colors", () => {
@@ -174,17 +166,4 @@ describe("ColorPicker", () => {
174166
expect(container.querySelector(".swatches-picker")).toBeInTheDocument();
175167
});
176168
});
177-
178-
describe("with a mode as", () => {
179-
it("popover or input renders with the structure", () => {
180-
const colorPickerComponent = renderColorPicker({ mode: "popover" });
181-
182-
expect(colorPickerComponent.asFragment()).toMatchSnapshot();
183-
});
184-
185-
it("inline renders with the structure", () => {
186-
const colorPickerComponent = renderColorPicker({ mode: "inline" });
187-
expect(colorPickerComponent.asFragment()).toMatchSnapshot();
188-
});
189-
});
190169
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import "@testing-library/jest-dom";
2+
import { render, RenderResult } from "@testing-library/react";
3+
import { createElement } from "react";
4+
import { ColorPicker, ColorPickerProps } from "../ColorPicker";
5+
6+
jest.mock("react-color/lib", () => ({
7+
SketchPicker: "sketch-picker"
8+
}));
9+
10+
describe("ColorPicker", () => {
11+
let colorPickerProps: ColorPickerProps;
12+
13+
beforeEach(() => {
14+
colorPickerProps = {
15+
color: "#000000",
16+
disabled: false,
17+
defaultColors: [],
18+
format: "hex",
19+
mode: "popover",
20+
type: "sketch",
21+
onChange: jest.fn(),
22+
onColorChange: jest.fn(),
23+
id: "color-picker",
24+
name: "color picker"
25+
};
26+
});
27+
28+
function renderColorPicker(configs: Partial<ColorPickerProps> = {}): RenderResult {
29+
return render(<ColorPicker {...colorPickerProps} {...configs} />);
30+
}
31+
32+
describe("with a mode as", () => {
33+
it("popover or input renders with the structure", () => {
34+
const colorPickerComponent = renderColorPicker({ mode: "popover" });
35+
36+
expect(colorPickerComponent.asFragment()).toMatchSnapshot();
37+
});
38+
39+
it("inline renders with the structure", () => {
40+
const colorPickerComponent = renderColorPicker({ mode: "inline" });
41+
42+
expect(colorPickerComponent.asFragment()).toMatchSnapshot();
43+
});
44+
});
45+
});

packages/pluggableWidgets/color-picker-web/src/components/__tests__/__snapshots__/ColorPicker.spec.tsx.snap

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,13 @@ exports[`ColorPicker renders the structure correctly 1`] = `
2525
</DocumentFragment>
2626
`;
2727

28-
exports[`ColorPicker that is disabled renders with the structure 1`] = `[Function]`;
29-
30-
exports[`ColorPicker with a mode as inline renders with the structure 1`] = `
31-
<DocumentFragment>
32-
<div
33-
class="widget-color-picker widget-color-picker-picker"
34-
>
35-
<div
36-
class=""
37-
>
38-
<div
39-
class="sketch-picker"
40-
/>
41-
</div>
42-
</div>
43-
</DocumentFragment>
44-
`;
45-
46-
exports[`ColorPicker with a mode as popover or input renders with the structure 1`] = `
28+
exports[`ColorPicker that is disabled renders with the structure 1`] = `
4729
<DocumentFragment>
4830
<div
49-
class="widget-color-picker widget-color-picker-picker"
31+
class="widget-color-picker widget-color-picker-picker widget-color-picker-disabled"
5032
>
5133
<button
52-
class="btn"
34+
class="btn disabled"
5335
>
5436
<div
5537
class="widget-color-picker-inner"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ColorPicker with a mode as inline renders with the structure 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="widget-color-picker widget-color-picker-picker"
7+
>
8+
<div
9+
class=""
10+
>
11+
<sketch-picker
12+
color="#000000"
13+
disablealpha="true"
14+
disabled="false"
15+
displaycolorpicker="false"
16+
/>
17+
</div>
18+
</div>
19+
</DocumentFragment>
20+
`;
21+
22+
exports[`ColorPicker with a mode as popover or input renders with the structure 1`] = `
23+
<DocumentFragment>
24+
<div
25+
class="widget-color-picker widget-color-picker-picker"
26+
>
27+
<button
28+
class="btn"
29+
>
30+
<div
31+
class="widget-color-picker-inner"
32+
style="background: rgb(0, 0, 0);"
33+
/>
34+
</button>
35+
</div>
36+
</DocumentFragment>
37+
`;

0 commit comments

Comments
 (0)