-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateAddInstanceButtons.test.tsx
More file actions
112 lines (100 loc) · 3.78 KB
/
generateAddInstanceButtons.test.tsx
File metadata and controls
112 lines (100 loc) · 3.78 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from "preact/compat";
import { singleLineFieldSchema } from "../../../__test__/data/fields";
import {
getAddInstanceButtons,
generateAddInstanceButton,
} from "../generateAddInstanceButtons";
import AddInstanceButtonComponentActual from "../../components/addInstanceButton";
const AddInstanceButtonComponent = vi.mocked(AddInstanceButtonComponentActual);
vi.mock("../../components/addInstanceButton", async () => {
return {
default: vi.fn().mockImplementation(() => {
return (
<button data-testid="add-instance-button">
Add instance button
</button>
);
}),
};
});
describe("generateAddInstanceButton", () => {
afterEach(() => {
vi.clearAllMocks();
});
test("should generate and return a button", () => {
const button = generateAddInstanceButton({
fieldSchema: singleLineFieldSchema,
value: "",
// @ts-expect-error mock field metadata
fieldMetadata: { hello: "world" },
onClick: vi.fn(),
// @ts-expect-error mocking preact signal
loading: { value: false },
index: 0,
label: "Add Instance",
});
expect(button).toBeInstanceOf(HTMLButtonElement);
});
test("should call the AddInstanceButtonComponent with the correct props", () => {
generateAddInstanceButton({
fieldSchema: singleLineFieldSchema,
value: "",
// @ts-expect-error mock field metadata
fieldMetadata: { hello: "world" },
onClick: vi.fn(),
// @ts-expect-error mocking preact signal
loading: { value: false },
index: 0,
label: "Add Instance",
});
const args = AddInstanceButtonComponent.mock.calls[0][0];
expect(args).toStrictEqual({
fieldSchema: singleLineFieldSchema,
value: "",
fieldMetadata: { hello: "world" },
onClick: expect.any(Function),
loading: { value: false },
index: 0,
label: "Add Instance",
});
});
});
describe("getAddInstanceButtons", () => {
let wrapper: HTMLDivElement;
beforeEach(() => {
wrapper = document.createElement("div");
wrapper.innerHTML = `
<button class="visual-builder__add-button"></button>
<button class="visual-builder__add-button"></button>
`;
document.body.appendChild(wrapper);
});
afterEach(() => {
document.body.removeChild(wrapper);
});
test("should return null if there are less than 2 buttons and we didn't ask for every buttons", () => {
wrapper.innerHTML = `
<button class="visual-builder__add-button"></button>
`;
const result = getAddInstanceButtons(wrapper);
expect(result).toBeNull();
});
test("should return an array with previous and next buttons if there are 2 or more buttons", () => {
const result = getAddInstanceButtons(wrapper);
expect(result).toHaveLength(2);
expect(result?.[0]).toBeInstanceOf(HTMLButtonElement);
expect(result?.[1]).toBeInstanceOf(HTMLButtonElement);
});
test("should return all buttons if getAllButtons is true", () => {
wrapper.innerHTML = `
<button class="visual-builder__add-button"></button>
<button class="visual-builder__add-button"></button>
<button class="visual-builder__add-button"></button>
<button class="visual-builder__add-button"></button>
`;
const result = getAddInstanceButtons(wrapper, true);
expect(result).toHaveLength(4);
expect(result![0]).toBeInstanceOf(HTMLButtonElement);
expect(result![1]).toBeInstanceOf(HTMLButtonElement);
});
});