@@ -6,12 +6,22 @@ import * as generateOverlay from "../../generators/generateOverlay";
66import { VisualBuilderPostMessageEvents } from "../types/postMessage.types" ;
77import { FieldDataType } from "../types/index.types" ;
88import userEvent from "@testing-library/user-event" ;
9- import { waitFor , screen } from "@testing-library/preact" ;
9+ import { waitFor } from "@testing-library/preact" ;
10+ import { VisualBuilder } from "../../index" ;
11+
12+ vi . mock ( "../../index" , async ( ) => ( {
13+ VisualBuilder : {
14+ VisualBuilderGlobalState : {
15+ value : { focusFieldReceivedInput : false } ,
16+ } ,
17+ } ,
18+ } ) ) ;
1019
1120vi . mock ( "lodash-es" , async ( ) => ( {
1221 ...( await import ( "lodash-es" ) ) ,
1322 throttle : vi . fn ( ( fn ) => fn ) ,
14- } ) )
23+ } ) ) ;
24+
1525describe ( "handle numeric field key down" , ( ) => {
1626 let h1 : HTMLHeadingElement ;
1727 let spiedPreventDefault : MockInstance < ( e : [ ] ) => void > | undefined ;
@@ -117,7 +127,7 @@ describe("handle numeric field key down", () => {
117127 } ) ;
118128
119129 test ( "should only accept characters like a number input" , async ( ) => {
120- h1 . innerHTML = '' ;
130+ h1 . innerHTML = "" ;
121131 await userEvent . click ( h1 ) ;
122132 await userEvent . keyboard ( "ab56c78e-h10" ) ;
123133
@@ -231,7 +241,10 @@ describe("handle single line field key down", () => {
231241 h1 = document . createElement ( "h1" ) ;
232242 h1 . innerHTML = "2.2" ;
233243 h1 . setAttribute ( "contenteditable" , "true" ) ;
234- h1 . setAttribute ( VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY , FieldDataType . SINGLELINE ) ;
244+ h1 . setAttribute (
245+ VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY ,
246+ FieldDataType . SINGLELINE
247+ ) ;
235248
236249 h1 . addEventListener ( "keydown" , ( e ) => {
237250 spiedPreventDefault = vi . spyOn ( e , "preventDefault" ) ;
@@ -260,7 +273,7 @@ describe("handle single line field key down", () => {
260273
261274 expect ( spiedPreventDefault ) . toHaveBeenCalledTimes ( 1 ) ;
262275 } ) ;
263- } )
276+ } ) ;
264277
265278describe ( "`handleFieldInput`" , ( ) => {
266279 let h1 : HTMLHeadingElement ;
@@ -289,18 +302,30 @@ describe("`handleFieldInput`", () => {
289302 } ) ;
290303
291304 test ( "should call `sendFieldEvent` on input event" , ( ) => {
292- const spiedSendFieldEvent = vi . spyOn ( generateOverlay , "sendFieldEvent" )
305+ const spiedSendFieldEvent = vi . spyOn ( generateOverlay , "sendFieldEvent" ) ;
293306 const consoleError = vi . spyOn ( console , "error" ) ;
294- spiedSendFieldEvent . mockImplementation ( ( ) => {
295- throw new Error ( "sendFieldEvent not implemented" )
307+ spiedSendFieldEvent . mockImplementation ( ( ) => {
308+ throw new Error ( "sendFieldEvent not implemented" ) ;
296309 } ) ;
297310 const inputEvent = new InputEvent ( "input" , {
298311 bubbles : true ,
299312 } ) ;
300313 h1 . dispatchEvent ( inputEvent ) ;
301314
302- expect ( spiedSendFieldEvent ) . toHaveBeenCalledWith ( { visualBuilderContainer, eventType : VisualBuilderPostMessageEvents . SYNC_FIELD } ) ;
315+ expect ( spiedSendFieldEvent ) . toHaveBeenCalledWith ( {
316+ visualBuilderContainer,
317+ eventType : VisualBuilderPostMessageEvents . SYNC_FIELD ,
318+ } ) ;
303319 expect ( consoleError ) . toHaveBeenCalled ( ) ;
304320 } ) ;
305321
306- } )
322+ test ( "should set focusFieldReceivedInput to true" , ( ) => {
323+ const inputEvent = new InputEvent ( "input" , {
324+ bubbles : true ,
325+ } ) ;
326+ h1 . dispatchEvent ( inputEvent ) ;
327+ expect (
328+ VisualBuilder . VisualBuilderGlobalState . value . focusFieldReceivedInput
329+ ) . toBe ( true ) ;
330+ } ) ;
331+ } ) ;
0 commit comments