Skip to content

Commit f9988a7

Browse files
committed
test: add tests for start editing button in builder
1 parent c006c68 commit f9988a7

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/visualBuilder/components/__test__/startEditingButton.test.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render, fireEvent, screen } from "@testing-library/preact";
2-
import StartEditingButtonComponent from "../startEditingButton";
2+
import StartEditingButtonComponent, { getEditButtonPosition } from "../startEditingButton";
33
import Config from "../../../configManager/configManager";
44
import { asyncRender } from "../../../__test__/utils";
55

@@ -45,4 +45,52 @@ describe("StartEditingButtonComponent", () => {
4545
"https://app.contentstack.com/#!/stack/bltapikey/visual-builder?branch=main&environment=bltenvironment&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
4646
);
4747
});
48+
49+
test("should not render when enable is false", async () => {
50+
Config.set("editButtonBuilder.enable", false);
51+
const { container } = await asyncRender(<StartEditingButtonComponent />);
52+
expect(container).toBeEmptyDOMElement();
53+
});
54+
55+
test("should render when enable is true", async () => {
56+
Config.set("editButtonBuilder.enable", true);
57+
const { getByTestId } = await asyncRender(<StartEditingButtonComponent />);
58+
const button = getByTestId("vcms-start-editing-btn");
59+
expect(button).toBeInTheDocument();
60+
});
61+
62+
test.each([
63+
'bottom-right',
64+
'bottom-left',
65+
'top-left',
66+
'top-right'
67+
])('should return valid position %s', (position) => {
68+
expect(getEditButtonPosition(position)).toBe(position);
69+
});
70+
71+
test.each([
72+
'invalid-position',
73+
'center',
74+
'',
75+
undefined,
76+
null,
77+
123,
78+
{},
79+
[],
80+
false,
81+
])('should return bottom-right for invalid input: %s', (invalidPosition) => {
82+
expect(getEditButtonPosition(invalidPosition)).toBe('bottom-right');
83+
});
84+
85+
test("should render with default values when editButtonBuilder config is missing", async () => {
86+
Config.reset();
87+
Config.set("stackDetails.apiKey", "bltapikey");
88+
Config.set("stackDetails.environment", "bltenvironment");
89+
90+
const { getByTestId } = await asyncRender(<StartEditingButtonComponent />);
91+
const button = getByTestId("vcms-start-editing-btn");
92+
93+
expect(Config.get().editButtonBuilder.position).toBe("bottom-right")
94+
expect(button).toBeInTheDocument();
95+
});
4896
});

src/visualBuilder/components/startEditingButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const positionStyles: Record<Position, string> = {
1616
"top-right": visualBuilderStyles()['visual-builder__start-editing-btn__top-right'],
1717
}
1818

19-
function getEditButtonPosition(position: any): Position {
19+
export function getEditButtonPosition(position: any): Position {
2020
const validPositions: Position[] = ['bottom-left', 'bottom-right', 'top-left', 'top-right']
2121
if(validPositions.includes(position)){
2222
return position

0 commit comments

Comments
 (0)