Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 65 additions & 46 deletions src/visualBuilder/components/__test__/startEditingButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { render, fireEvent, screen } from "@testing-library/preact";
import StartEditingButtonComponent, { getEditButtonPosition } from "../startEditingButton";
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 = '';
document.getElementsByTagName("html")[0].innerHTML = "";
Config.reset();
Config.set("stackDetails.apiKey", "bltapikey");
Config.set("stackDetails.environment", "bltenvironment");
Expand Down Expand Up @@ -38,103 +40,120 @@ describe("StartEditingButtonComponent", () => {
});

test("should update the href when clicked", async () => {
const { getByTestId } = await asyncRender(<StartEditingButtonComponent />);
const { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");

expect(button?.getAttribute("href")).toBe(
"https://app.contentstack.com/#!/stack/bltapikey/visual-builder?branch=main&environment=bltenvironment&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
"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 />);
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 { 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(["bottom-right", "bottom-left", "top-left", "top-right"])(
"should return valid position %s",
(position) => {
expect(getEditButtonPosition(position)).toBe(position);
}
);

test.each([
'invalid-position',
'center',
'',
"invalid-position",
"center",
"",
undefined,
null,
123,
{},
[],
false,
])('should return bottom-right for invalid input: %s', (invalidPosition) => {
expect(getEditButtonPosition(invalidPosition)).toBe('bottom-right');
});

])(
"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 { getByTestId } = await asyncRender(
<StartEditingButtonComponent />
);
const button = getByTestId("vcms-start-editing-btn");

expect(Config.get().editInVisualBuilderButton.position).toBe("bottom-right")

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'),
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});

const { getByTestId } = await asyncRender(<StartEditingButtonComponent />);

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

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"));
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'),
Object.defineProperty(window, "location", {
value: new URL("http://localhost:3000"),
});

const { getByTestId } = await asyncRender(<StartEditingButtonComponent />);

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

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"));
expect(updatedHref).toContain(
encodeURIComponent("http://localhost:3000/contact")
);
});


});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("generateStartEditingButton", () => {
button?.click();

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

Expand All @@ -57,7 +57,7 @@ describe("generateStartEditingButton", () => {
button?.click();

expect(button?.getAttribute("href")).toBe(
"https://app.contentstack.com/#!/stack//visual-builder?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
"https://app.contentstack.com/#!/stack//visual-editor?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('getVisualBuilderRedirectionUrl', () => {
});

const result = getVisualBuilderRedirectionUrl();
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-builder?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-editor?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
});

it('should return the correct URL without branch and environment', () => {
Expand All @@ -44,7 +44,7 @@ describe('getVisualBuilderRedirectionUrl', () => {
});

const result = getVisualBuilderRedirectionUrl();
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-builder?target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-editor?target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
});

it('should use locale from data-cslp attribute if present', () => {
Expand All @@ -64,7 +64,7 @@ describe('getVisualBuilderRedirectionUrl', () => {
extractDetailsFromCslp.mockReturnValue({ locale: 'fr-FR' });

const result = getVisualBuilderRedirectionUrl();
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-builder?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=fr-FR');
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-editor?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=fr-FR');
});

it('should return the correct URL without locale', () => {
Expand All @@ -82,7 +82,7 @@ describe('getVisualBuilderRedirectionUrl', () => {
});

const result = getVisualBuilderRedirectionUrl();
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-builder?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F');
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-editor?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F');
});

it('should ignore invalid data-cslp attribute and use locale from config', () => {
Expand All @@ -101,7 +101,7 @@ describe('getVisualBuilderRedirectionUrl', () => {

const result = getVisualBuilderRedirectionUrl();
// Should use locale from config when data-cslp attribute is invalid (empty or no value)
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-builder?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
expect(result.toString()).toBe('https://app.example.com/#!/stack/12345/visual-editor?branch=main&environment=production&target-url=https%3A%2F%2Fexample.com%2F&locale=en-US');
// Should not call extractDetailsFromCslp for invalid cslp
expect(extractDetailsFromCslp).not.toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion src/visualBuilder/utils/getVisualBuilderRedirectionUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function getVisualBuilderRedirectionUrl(): URL {
}

const completeURL = new URL(
`/#!/stack/${apiKey}/visual-builder?${searchParams.toString()}`,
`/#!/stack/${apiKey}/visual-editor?${searchParams.toString()}`,
appUrl
);
return completeURL;
Expand Down
Loading