Skip to content

Commit f692cc7

Browse files
committed
coverage increased added new cases
1 parent 1c2898c commit f692cc7

10 files changed

Lines changed: 1663 additions & 24 deletions

.talismanrc

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
/** @jsxImportSource preact */
6+
import { render, fireEvent, waitFor } from "@testing-library/preact";
7+
import { vi } from "vitest";
8+
import { CslpError } from "../CslpError";
9+
import { visualBuilderStyles } from "../visualBuilder.style";
10+
11+
vi.mock("../visualBuilder.style", () => ({
12+
visualBuilderStyles: vi.fn(() => ({
13+
"visual-builder__focused-toolbar__error": "error-class",
14+
"visual-builder__focused-toolbar__error-text": "error-text-class",
15+
"visual-builder__focused-toolbar__error-toolip": "error-tooltip-class",
16+
})),
17+
}));
18+
19+
vi.mock("../icons", () => ({
20+
WarningOctagonIcon: () => <div data-testid="warning-icon">Warning</div>,
21+
}));
22+
23+
describe("CslpError", () => {
24+
it("should render error component with icon and text", () => {
25+
const { getByText, getByTestId } = render(<CslpError />);
26+
27+
expect(getByTestId("warning-icon")).toBeInTheDocument();
28+
expect(getByText("Error")).toBeInTheDocument();
29+
});
30+
31+
it("should show tooltip on mouseenter and hide on mouseleave", async () => {
32+
const { container, queryByText } = render(<CslpError />);
33+
34+
// Find the error element by its ref (it will have the error class)
35+
const errorElement = container.querySelector(
36+
'[class*="visual-builder__focused-toolbar__error"]'
37+
) || container.firstElementChild;
38+
39+
expect(queryByText("Invalid CSLP tag")).not.toBeInTheDocument();
40+
41+
fireEvent.mouseEnter(errorElement!);
42+
43+
await waitFor(() => {
44+
expect(queryByText("Invalid CSLP tag")).toBeInTheDocument();
45+
expect(
46+
queryByText("The CSLP is invalid or incorrectly generated.")
47+
).toBeInTheDocument();
48+
});
49+
50+
fireEvent.mouseLeave(errorElement!);
51+
52+
await waitFor(() => {
53+
expect(queryByText("Invalid CSLP tag")).not.toBeInTheDocument();
54+
});
55+
});
56+
});
57+

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import React from "preact/compat";
22
import { render, fireEvent } from "@testing-library/preact";
33
import { FieldLocationIcon } from "../FieldLocationIcon";
44
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage";
5+
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types";
56
import { vi } from "vitest";
67
import { asyncRender } from "../../../__test__/utils";
78

9+
vi.mock("../../utils/visualBuilderPostMessage", () => ({
10+
default: {
11+
send: vi.fn(),
12+
},
13+
}));
14+
815

916

1017
describe("FieldLocationIcon", () => {
@@ -119,5 +126,83 @@ describe("FieldLocationIcon", () => {
119126
});
120127

121128
});
122-
129+
130+
it("should handle app click and send post message", () => {
131+
const mockToolbarRef = { current: null as HTMLElement | null };
132+
const mockFieldLocationData = {
133+
apps: [
134+
{
135+
uid: "app1",
136+
title: "Test App",
137+
icon: "icon1.png",
138+
app_installation_uid: "install1",
139+
},
140+
],
141+
};
142+
const mockDomEditStack = [{ uid: "edit1" }];
143+
144+
const { getByTestId } = render(
145+
<FieldLocationIcon
146+
fieldLocationData={mockFieldLocationData}
147+
multipleFieldToolbarButtonClasses="test-class"
148+
handleMoreIconClick={() => {}}
149+
moreButtonRef={{ current: null }}
150+
toolbarRef={mockToolbarRef}
151+
domEditStack={mockDomEditStack}
152+
/>
153+
);
154+
155+
const appIcon = getByTestId("field-location-icon");
156+
fireEvent.click(appIcon);
157+
158+
// Verify send was called with correct event and app data
159+
expect(visualBuilderPostMessage?.send).toHaveBeenCalled();
160+
const callArgs = (visualBuilderPostMessage?.send as any).mock.calls[0];
161+
expect(callArgs[0]).toBe(
162+
VisualBuilderPostMessageEvents.FIELD_LOCATION_SELECTED_APP
163+
);
164+
expect(callArgs[1].app).toEqual(mockFieldLocationData.apps[0]);
165+
expect(callArgs[1].DomEditStack).toEqual(mockDomEditStack);
166+
expect(callArgs[1].position).toBeDefined();
167+
});
168+
169+
it("should not send post message when toolbarRef is null", () => {
170+
const mockFieldLocationData = {
171+
apps: [
172+
{
173+
uid: "app1",
174+
title: "Test App",
175+
icon: "icon1.png",
176+
app_installation_uid: "install1",
177+
},
178+
],
179+
};
180+
181+
const toolbarRef = { current: null };
182+
183+
const { getByTestId } = render(
184+
<FieldLocationIcon
185+
fieldLocationData={mockFieldLocationData}
186+
multipleFieldToolbarButtonClasses="test-class"
187+
handleMoreIconClick={() => {}}
188+
moreButtonRef={{ current: null }}
189+
toolbarRef={toolbarRef}
190+
domEditStack={[]}
191+
/>
192+
);
193+
194+
// Ensure toolbarRef stays null (the component sets it to the div ref)
195+
// But the handleAppClick checks toolbarRef.current before sending
196+
const appIcon = getByTestId("field-location-icon");
197+
198+
// Manually set toolbarRef.current to null to simulate the condition
199+
toolbarRef.current = null;
200+
201+
fireEvent.click(appIcon);
202+
203+
// The function checks if(!toolbarRef.current) return, so it should not be called
204+
// However, the component sets toolbarRef to the container div, so we need to test differently
205+
// Let's verify the click handler runs but the send is not called due to the null check
206+
expect(visualBuilderPostMessage?.send).not.toHaveBeenCalled();
207+
});
123208
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
/** @jsxImportSource preact */
6+
import { render, fireEvent } from "@testing-library/preact";
7+
import { vi } from "vitest";
8+
import HighlightedCommentIcon from "../HighlightedCommentIcon";
9+
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage";
10+
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types";
11+
import Config from "../../../configManager/configManager";
12+
import { toggleCollabPopup } from "../../generators/generateThread";
13+
14+
vi.mock("../../utils/visualBuilderPostMessage", () => ({
15+
default: {
16+
send: vi.fn(),
17+
},
18+
}));
19+
20+
vi.mock("../../../configManager/configManager", () => ({
21+
default: {
22+
get: vi.fn(),
23+
set: vi.fn(),
24+
},
25+
}));
26+
27+
vi.mock("../../generators/generateThread", () => ({
28+
toggleCollabPopup: vi.fn(),
29+
}));
30+
31+
vi.mock("../icons", () => ({
32+
HighlightCommentIcon: () => (
33+
<div data-testid="highlight-comment-icon">Icon</div>
34+
),
35+
}));
36+
37+
describe("HighlightedCommentIcon", () => {
38+
beforeEach(() => {
39+
vi.clearAllMocks();
40+
(Config.get as any).mockReturnValue({
41+
collab: {
42+
isFeedbackMode: false,
43+
},
44+
});
45+
});
46+
47+
it("should render comment icon", () => {
48+
const mockData = {
49+
fieldMetadata: { uid: "field-1" },
50+
discussion: { _id: "discussion-1" },
51+
fieldSchema: { uid: "schema-1" },
52+
absolutePath: "test.path",
53+
};
54+
55+
const { getByTestId } = render(
56+
<HighlightedCommentIcon data={mockData} />
57+
);
58+
59+
expect(getByTestId("highlight-comment-icon")).toBeInTheDocument();
60+
});
61+
62+
it("should handle click and send post message", () => {
63+
const mockData = {
64+
fieldMetadata: { uid: "field-1" },
65+
discussion: { _id: "discussion-1" },
66+
fieldSchema: { uid: "schema-1" },
67+
absolutePath: "test.path",
68+
};
69+
70+
const { container } = render(
71+
<HighlightedCommentIcon data={mockData} />
72+
);
73+
74+
const iconElement = container.querySelector(".collab-icon");
75+
fireEvent.click(iconElement!);
76+
77+
expect(visualBuilderPostMessage?.send).toHaveBeenCalledWith(
78+
VisualBuilderPostMessageEvents.OPEN_FIELD_COMMENT_MODAL,
79+
{
80+
fieldMetadata: mockData.fieldMetadata,
81+
discussion: mockData.discussion,
82+
fieldSchema: mockData.fieldSchema,
83+
absolutePath: mockData.absolutePath,
84+
}
85+
);
86+
expect(toggleCollabPopup).toHaveBeenCalledWith({
87+
threadUid: "",
88+
action: "close",
89+
});
90+
expect(Config.set).toHaveBeenCalledWith("collab.isFeedbackMode", true);
91+
});
92+
});

0 commit comments

Comments
 (0)