Skip to content

Commit baea843

Browse files
Merge pull request #475 from contentstack/VE-6918/error-text-main
VE-6918 : warning message improved
2 parents 3d5afdc + 7ecfb19 commit baea843

3 files changed

Lines changed: 111 additions & 5 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from "preact/compat";
2+
import { render, fireEvent, waitFor } from "@testing-library/preact";
3+
import { EmptyBlock } from "../emptyBlock";
4+
import visualBuilderPostMessage from "../../utils/visualBuilderPostMessage";
5+
import { observeParentAndFocusNewInstance } from "../../utils/multipleElementAddButton";
6+
import { CslpData } from "../../../cslp/types/cslp.types";
7+
import { ISchemaFieldMap } from "../../utils/types/index.types";
8+
import { VisualBuilderPostMessageEvents } from "../../utils/types/postMessage.types";
9+
10+
vi.mock("../../utils/visualBuilderPostMessage", () => ({
11+
default: {
12+
send: vi.fn(),
13+
},
14+
}));
15+
16+
vi.mock("../../utils/multipleElementAddButton", () => ({
17+
observeParentAndFocusNewInstance: vi.fn(),
18+
}));
19+
20+
describe("EmptyBlock", () => {
21+
const mockDetails = {
22+
fieldMetadata: {
23+
cslpValue: "parent.cslp.value",
24+
} as CslpData,
25+
fieldSchema: {
26+
display_name: "Test Block",
27+
} as ISchemaFieldMap,
28+
};
29+
30+
afterEach(() => {
31+
vi.clearAllMocks();
32+
});
33+
34+
test("should render correctly", () => {
35+
const { getByText, getByTestId } = render(
36+
<EmptyBlock details={mockDetails} />
37+
);
38+
39+
expect(
40+
getByText(
41+
(_, element) =>
42+
element?.textContent ===
43+
"This page doesn’t have any Test Block added. Click the button below to add one."
44+
)
45+
).toBeTruthy();
46+
expect(
47+
getByTestId("visual-builder__empty-block-add-button")
48+
).toBeTruthy();
49+
expect(getByText("Add Test Block")).toBeTruthy();
50+
});
51+
52+
test("should call sendAddInstanceEvent on button click", async () => {
53+
const { getByTestId } = render(<EmptyBlock details={mockDetails} />);
54+
const addButton = getByTestId("visual-builder__empty-block-add-button");
55+
56+
fireEvent.click(addButton);
57+
58+
await waitFor(() => {
59+
expect((visualBuilderPostMessage as any).send).toHaveBeenCalledWith(
60+
VisualBuilderPostMessageEvents.ADD_INSTANCE,
61+
{
62+
fieldMetadata: mockDetails.fieldMetadata,
63+
index: 0,
64+
}
65+
);
66+
});
67+
68+
expect(observeParentAndFocusNewInstance).toHaveBeenCalledWith({
69+
parentCslp: mockDetails.fieldMetadata.cslpValue,
70+
index: 0,
71+
});
72+
});
73+
});

src/visualBuilder/components/emptyBlock.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { observeParentAndFocusNewInstance } from "../utils/multipleElementAddBut
66
import { ISchemaFieldMap } from "../utils/types/index.types";
77
import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types";
88
import React from "preact/compat";
9+
import { startCase, toLower } from "lodash-es";
910

1011
interface EmptyBlockProps {
1112
details: {
@@ -46,7 +47,18 @@ export function EmptyBlock(props: EmptyBlockProps): JSX.Element {
4647
visualBuilderStyles()["visual-builder__empty-block-title"]
4748
)}
4849
>
49-
There are no {blockParentName.toLowerCase()} on this page yet. Click the button below to add one.
50+
This page doesn’t have any{" "}
51+
<span
52+
className={classNames(
53+
"visual-builder__empty-block-field-name",
54+
visualBuilderStyles()[
55+
"visual-builder__empty-block-field-name"
56+
]
57+
)}
58+
>
59+
{startCase(toLower(blockParentName))}
60+
</span>{" "}
61+
added. Click the button below to add one.
5062
</div>
5163
<button
5264
className={classNames(
@@ -59,8 +71,17 @@ export function EmptyBlock(props: EmptyBlockProps): JSX.Element {
5971
type="button"
6072
data-testid="visual-builder__empty-block-add-button"
6173
>
62-
<i className="fas fa-plus"></i> &nbsp;
63-
{blockParentName}
74+
<span
75+
className={classNames(
76+
"visual-builder__empty-block-plus-icon",
77+
visualBuilderStyles()[
78+
"visual-builder__empty-block-plus-icon"
79+
]
80+
)}
81+
>
82+
+
83+
</span>
84+
&nbsp; Add {blockParentName}
6485
</button>
6586
</div>
6687
);

src/visualBuilder/visualBuilder.style.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ export function visualBuilderStyles() {
121121
visibility: visible;
122122
}
123123
`,
124+
"visual-builder__empty-block-plus-icon": css`
125+
font-size: 22px;
126+
font-weight: 300;
127+
display: flex;
128+
align-items: center;
129+
justify-content: center;
130+
`,
124131
"visual-builder__overlay--outline": css`
125132
position: absolute;
126133
outline: 4px solid #715cdd;
@@ -571,19 +578,24 @@ export function visualBuilderStyles() {
571578
line-height: 100%;
572579
color: #647696;
573580
`,
581+
"visual-builder__empty-block-field-name": css`
582+
font-weight: 700;
583+
`,
574584
"visual-builder__empty-block-add-button": css`
575585
height: 32px;
576586
border-radius: 4px;
577587
background: #f9f8ff;
578588
border-color: #6c5ce7;
579589
border-width: 1px;
580-
padding: 8px 16px 8px 16px;
590+
padding: 0 16px;
581591
font-size: 0.9rem;
582592
font-family: Inter;
583593
font-weight: 600;
584594
color: #6c5ce7;
585-
padding-block: 0px;
586595
letter-spacing: 0.01rem;
596+
display: inline-flex;
597+
align-items: center;
598+
justify-content: center;
587599
`,
588600
"visual-builder__hover-outline": css`
589601
position: absolute;

0 commit comments

Comments
 (0)