-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstartEditingButton.test.tsx
More file actions
159 lines (132 loc) · 5.21 KB
/
startEditingButton.test.tsx
File metadata and controls
159 lines (132 loc) · 5.21 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { render, fireEvent, screen } from "@testing-library/preact";
import StartEditingButtonComponent, {
getEditButtonPosition,
} from "../startEditingButton";
import Config from "../../../configManager/configManager";
import { asyncRender } from "../../../__test__/utils";
describe("StartEditingButtonComponent", () => {
let visualBuilderContainer: HTMLDivElement;
beforeEach(() => {
document.getElementsByTagName("html")[0].innerHTML = "";
Config.reset();
Config.set("stackDetails.apiKey", "bltapikey");
Config.set("stackDetails.environment", "bltenvironment");
visualBuilderContainer = document.createElement("div");
document.body.appendChild(visualBuilderContainer);
});
afterEach(() => {
vi.clearAllMocks();
document.body.removeChild(visualBuilderContainer);
});
afterAll(() => {
Config.reset();
});
test("renders correctly with EditIcon and Start Editing text", async () => {
const { getByText, getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const editIcon = getByTestId("visual-builder__edit-icon");
const startEditingText = getByText("Start Editing");
expect(editIcon).toBeInTheDocument();
expect(startEditingText).toBeInTheDocument();
});
test("should update the href when clicked", async () => {
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");
expect(button?.getAttribute("href")).toBe(
"https://app.contentstack.com/#!/stack/bltapikey/visual-editor?branch=main&environment=bltenvironment&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
);
});
test("should not render when enable is false", async () => {
Config.set("editInVisualBuilderButton.enable", false);
const { container } = await asyncRender(
<StartEditingButtonComponent />
);
expect(container).toBeEmptyDOMElement();
});
test("should render when enable is true", async () => {
Config.set("editInVisualBuilderButton.enable", true);
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");
expect(button).toBeInTheDocument();
});
test.each(["bottom-right", "bottom-left", "top-left", "top-right"])(
"should return valid position %s",
(position) => {
expect(getEditButtonPosition(position)).toBe(position);
}
);
test.each([
"invalid-position",
"center",
"",
undefined,
null,
123,
{},
[],
false,
])(
"should return bottom-right for invalid input: %s",
(invalidPosition) => {
expect(getEditButtonPosition(invalidPosition)).toBe("bottom-right");
}
);
test("should render with default values when editInVisualBuilderButton config is missing", async () => {
Config.reset();
Config.set("stackDetails.apiKey", "bltapikey");
Config.set("stackDetails.environment", "bltenvironment");
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");
expect(Config.get().editInVisualBuilderButton.position).toBe(
"bottom-right"
);
expect(button).toBeInTheDocument();
});
test("should update href with current URL when mouse enters button", async () => {
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");
const initialHref = button.getAttribute("href");
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000/about"),
writable: true,
});
fireEvent.mouseEnter(button);
const updatedHref = button.getAttribute("href");
expect(updatedHref).not.toBe(initialHref);
expect(updatedHref).toContain(
encodeURIComponent("http://localhost:3000/about")
);
});
test("should update href with current URL when button is focused", async () => {
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");
const initialHref = button.getAttribute("href");
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000/contact"),
writable: true,
});
fireEvent.focus(button);
const updatedHref = button.getAttribute("href");
expect(updatedHref).not.toBe(initialHref);
expect(updatedHref).toContain(
encodeURIComponent("http://localhost:3000/contact")
);
});
});