1- import { waitFor } from "@testing-library/preact" ;
1+ import { act , waitFor , fireEvent } from "@testing-library/preact" ;
22import "@testing-library/jest-dom" ;
33import { getFieldSchemaMap } from "../../../../__test__/data/fieldSchemaMap" ;
44import Config from "../../../../configManager/configManager" ;
@@ -9,6 +9,7 @@ import visualBuilderPostMessage from "../../../utils/visualBuilderPostMessage";
99import { vi } from "vitest" ;
1010import { VisualBuilderPostMessageEvents } from "../../../utils/types/postMessage.types" ;
1111import { VisualBuilder } from "../../../index" ;
12+ import { triggerAndWaitForClickAction } from "../../../../__test__/utils" ;
1213
1314global . ResizeObserver = vi . fn ( ) . mockImplementation ( ( ) => ( {
1415 observe : vi . fn ( ) ,
@@ -78,9 +79,7 @@ describe("When an element is clicked in visual builder mode", () => {
7879 "get"
7980 ) . mockReturnValue ( 100 ) ;
8081 vi . spyOn ( document . body , "scrollHeight" , "get" ) . mockReturnValue ( 100 ) ;
81- } ) ;
8282
83- beforeEach ( ( ) => {
8483 Config . reset ( ) ;
8584 Config . set ( "mode" , 2 ) ;
8685 mouseClickEvent = new Event ( "click" , {
@@ -89,20 +88,17 @@ describe("When an element is clicked in visual builder mode", () => {
8988 } ) ;
9089 } ) ;
9190
92- afterEach ( ( ) => {
93- vi . clearAllMocks ( ) ;
94- document . body . innerHTML = "" ;
95- } ) ;
96-
9791 afterAll ( ( ) => {
9892 Config . reset ( ) ;
93+ vi . clearAllMocks ( ) ;
94+ document . body . innerHTML = "" ;
9995 } ) ;
10096
10197 describe ( "boolean field" , ( ) => {
10298 let booleanField : HTMLParagraphElement ;
10399 let visualBuilder : VisualBuilder ;
104100
105- beforeEach ( ( ) => {
101+ beforeAll ( async ( ) => {
106102 booleanField = document . createElement ( "p" ) ;
107103 booleanField . setAttribute (
108104 "data-cslp" ,
@@ -111,33 +107,30 @@ describe("When an element is clicked in visual builder mode", () => {
111107 document . body . appendChild ( booleanField ) ;
112108
113109 visualBuilder = new VisualBuilder ( ) ;
110+ await triggerAndWaitForClickAction ( visualBuilderPostMessage , booleanField ) ;
114111 } ) ;
115112
116- afterEach ( ( ) => {
113+ afterAll ( ( ) => {
117114 visualBuilder . destroy ( ) ;
118115 } ) ;
119116
120117 test ( "should have outline" , ( ) => {
121- booleanField . dispatchEvent ( mouseClickEvent ) ;
122118 expect ( booleanField . classList . contains ( "cslp-edit-mode" ) ) ;
123119 } ) ;
124120
125121 test ( "should have an overlay" , ( ) => {
126- booleanField . dispatchEvent ( mouseClickEvent ) ;
127122 const overlay = document . querySelector ( ".visual-builder__overlay" ) ;
128123 expect ( overlay ! . classList . contains ( "visible" ) ) ;
129124 } ) ;
130125
131126 test . skip ( "should have a field path dropdown" , ( ) => {
132- booleanField . dispatchEvent ( mouseClickEvent ) ;
133127 const toolbar = document . querySelector (
134128 ".visual-builder__focused-toolbar__field-label-wrapper__current-field"
135129 ) ;
136130 expect ( toolbar ) . toBeInTheDocument ( ) ;
137131 } ) ;
138132
139133 test ( "should contain a data-cslp-field-type attribute" , async ( ) => {
140- booleanField . dispatchEvent ( mouseClickEvent ) ;
141134 await waitFor ( ( ) => {
142135 expect ( booleanField ) . toHaveAttribute (
143136 VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
@@ -146,14 +139,13 @@ describe("When an element is clicked in visual builder mode", () => {
146139 } ) ;
147140
148141 test ( "should not contain a contenteditable attribute" , async ( ) => {
149- booleanField . dispatchEvent ( mouseClickEvent ) ;
150142 await waitFor ( ( ) => {
151143 expect ( booleanField ) . not . toHaveAttribute ( "contenteditable" ) ;
152144 } ) ;
153145 } ) ;
154146
155147 test ( "should send a focus field message to parent" , async ( ) => {
156- booleanField . dispatchEvent ( mouseClickEvent ) ;
148+
157149 await waitFor ( ( ) => {
158150 expect ( visualBuilderPostMessage ?. send ) . toBeCalledWith (
159151 VisualBuilderPostMessageEvents . FOCUS_FIELD ,
0 commit comments