Skip to content

Commit 856cd9c

Browse files
authored
Merge pull request #447 from contentstack/start-editing-button-not-rendering
fix: start editing button not rendering
2 parents e063d6e + f690285 commit 856cd9c

3 files changed

Lines changed: 29 additions & 33 deletions

File tree

src/visualBuilder/generators/generateStartEditingButton.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ import StartEditingButtonComponent from "../components/startEditingButton";
55
/**
66
* Generates a start editing button for the visual builder.
77
*
8-
* @param visualBuilderContainer - The HTMLDivElement that wraps the visual builder.
9-
* @returns The generated HTMLAnchorElement representing the start editing button, or undefined if the visualBuilderContainer is null.
8+
* @returns The generated HTMLAnchorElement representing the start editing button, or undefined if the button cannot be created.
109
*/
11-
export function generateStartEditingButton(
12-
visualBuilderContainer: HTMLDivElement | null
13-
): HTMLAnchorElement | undefined {
14-
if (!visualBuilderContainer) {
15-
PublicLogger.warn("Visual builder overlay not found.");
16-
return;
10+
export function generateStartEditingButton(): HTMLAnchorElement | undefined {
11+
const existingButton = document.querySelector(
12+
".visual-builder__start-editing-btn"
13+
) as HTMLAnchorElement;
14+
15+
if (existingButton) {
16+
return existingButton;
1717
}
1818

1919
const wrapper = document.createDocumentFragment();
2020
render(<StartEditingButtonComponent />, wrapper);
2121

22-
visualBuilderContainer?.appendChild(wrapper);
22+
if (wrapper.children.length === 0) {
23+
return undefined;
24+
}
25+
26+
document.body.appendChild(wrapper);
2327

2428
const startEditingButton = document.querySelector(
2529
".visual-builder__start-editing-btn"

src/visualBuilder/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export class VisualBuilder {
383383
})
384384
.catch(() => {
385385
if (!inIframe()) {
386-
generateStartEditingButton(this.visualBuilderContainer);
386+
generateStartEditingButton();
387387
}
388388
});
389389
}
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
import { getDefaultConfig } from "../../../configManager/config.default";
2-
import { PublicLogger } from "../../../logger/logger";
32
import { IConfig } from "../../../types/types";
43
import { generateStartEditingButton } from "./../../generators/generateStartEditingButton";
54

65
describe("generateStartEditingButton", () => {
76
let config: IConfig;
8-
let visualBuilderContainer: HTMLDivElement;
97

108
beforeEach(() => {
119
config = getDefaultConfig();
1210

1311
config.stackDetails.apiKey = "bltapikey";
1412
config.stackDetails.environment = "bltenvironment";
15-
16-
visualBuilderContainer = document.createElement("div");
17-
document.body.appendChild(visualBuilderContainer);
1813
});
1914

2015
afterEach(() => {
2116
vi.clearAllMocks();
22-
document.body.removeChild(visualBuilderContainer);
17+
const existingButton = document.querySelector(
18+
".visual-builder__start-editing-btn"
19+
);
20+
if (existingButton) {
21+
existingButton.remove();
22+
}
2323
});
2424

25-
test("should return an anchor tag", () => {
26-
const button = generateStartEditingButton(visualBuilderContainer);
25+
test("should return an anchor tag", () => {
26+
const button = generateStartEditingButton();
2727
expect(button).toBeInstanceOf(HTMLAnchorElement);
2828
});
2929

30-
test("should append the button within visualBuilderContainer", () => {
31-
expect(visualBuilderContainer.children.length).toBe(0);
32-
generateStartEditingButton(visualBuilderContainer);
30+
test("should append the button within document.body", () => {
31+
const initialBodyChildren = document.body.children.length;
32+
generateStartEditingButton();
3333

34-
expect(visualBuilderContainer.children.length).toBe(1);
34+
expect(document.body.children.length).toBe(initialBodyChildren + 1);
3535
});
3636

3737
test("should update the href when clicked", () => {
38-
const button = generateStartEditingButton(visualBuilderContainer);
38+
const button = generateStartEditingButton();
3939
button?.click();
4040

4141
expect(button?.getAttribute("href")).toBe(
4242
"https://app.contentstack.com/#!/stack//visual-builder?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
4343
);
4444
});
4545

46-
test("should get the href detal from cslp attribute if present", () => {
46+
test("should get the href detail from cslp attribute if present", () => {
4747
const h1 = document.createElement("h1");
4848

4949
h1.setAttribute(
@@ -53,19 +53,11 @@ describe("generateStartEditingButton", () => {
5353

5454
document.body.appendChild(h1);
5555

56-
const button = generateStartEditingButton(visualBuilderContainer);
56+
const button = generateStartEditingButton();
5757
button?.click();
5858

5959
expect(button?.getAttribute("href")).toBe(
6060
"https://app.contentstack.com/#!/stack//visual-builder?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
6161
);
6262
});
63-
64-
test("should throw a warning if visualBuilderContainer is not found", () => {
65-
const spiedWarn = vi.spyOn(PublicLogger, "warn");
66-
67-
generateStartEditingButton(null);
68-
69-
expect(spiedWarn).toHaveBeenCalledTimes(1);
70-
});
7163
});

0 commit comments

Comments
 (0)