11import { test } from "uvu" ;
2+ import lodash from "lodash" ;
23import * as assert from "uvu/assert" ;
34import { serializeEvent } from "../src/event-to-object.js" ;
45import "./tooling/setup.js" ;
56
7+ function assertEqualSerializedEventData ( eventData , expectedSerializedData ) {
8+ const mockBoundingRect = {
9+ left : 0 ,
10+ top : 0 ,
11+ right : 0 ,
12+ bottom : 0 ,
13+ x : 0 ,
14+ y : 0 ,
15+ width : 0 ,
16+ } ;
17+
18+ const mockElement = {
19+ tagName : null ,
20+ getBoundingClientRect : ( ) => mockBoundingRect ,
21+ } ;
22+
23+ const commonEventData = {
24+ target : mockElement ,
25+ currentTarget : mockElement ,
26+ relatedTarget : mockElement ,
27+ } ;
28+
29+ const commonSerializedEventData = {
30+ target : { boundingClientRect : mockBoundingRect } ,
31+ currentTarget : { boundingClientRect : mockBoundingRect } ,
32+ relatedTarget : { boundingClientRect : mockBoundingRect } ,
33+ } ;
34+
35+ assert . equal (
36+ serializeEvent ( lodash . merge ( { } , commonEventData , eventData ) ) ,
37+ lodash . merge ( { } , commonSerializedEventData , expectedSerializedData )
38+ ) ;
39+ }
40+
641const allTargetData = {
742 files : [
843 {
@@ -21,33 +56,38 @@ const allTargetData = {
2156 {
2257 case : "adds 'files' and 'value' attributes for INPUT if type=file" ,
2358 tagName : "INPUT" ,
24- otherAttrs : { type : "file" } ,
59+ addTargetAttrs : { type : "file" } ,
2560 output : {
26- files : allTargetData . files ,
27- value : allTargetData . value ,
61+ target : {
62+ files : allTargetData . files ,
63+ value : allTargetData . value ,
64+ } ,
2865 } ,
2966 } ,
3067 ...[ "BUTTON" , "INPUT" , "OPTION" , "LI" , "METER" , "PROGRESS" , "PARAM" ] . map (
3168 ( tagName ) => ( {
3269 case : `adds 'value' attribute for ${ tagName } element` ,
3370 tagName,
34- output : { value : allTargetData . value } ,
71+ output : { target : { value : allTargetData . value } } ,
3572 } )
3673 ) ,
3774 ...[ "AUDIO" , "VIDEO" ] . map ( ( tagName ) => ( {
3875 case : `adds 'currentTime' attribute for ${ tagName } element` ,
3976 tagName,
40- output : { currentTime : allTargetData . currentTime } ,
77+ output : { target : { currentTime : allTargetData . currentTime } } ,
4178 } ) ) ,
4279] . forEach ( ( expectation ) => {
4380 test ( `serializeEvent() ${ expectation . case } ` , ( ) => {
4481 const eventData = {
45- target : { ...allTargetData , tagName : expectation . tagName } ,
82+ target : {
83+ ...allTargetData ,
84+ tagName : expectation . tagName ,
85+ } ,
4686 } ;
47- if ( expectation . otherAttrs ) {
48- Object . assign ( eventData . target , expectation . otherAttrs ) ;
87+ if ( expectation . addTargetAttrs ) {
88+ Object . assign ( eventData . target , expectation . addTargetAttrs ) ;
4989 }
50- assert . equal ( serializeEvent ( eventData ) , expectation . output ) ;
90+ assertEqualSerializedEventData ( eventData , expectation . output ) ;
5191 } ) ;
5292} ) ;
5393
@@ -247,8 +287,8 @@ const allEventData = {
247287 } ,
248288] . forEach ( ( expectation ) => {
249289 test ( `serializeEvent() adds ${ expectation . case } attributes` , ( ) => {
250- assert . equal (
251- serializeEvent ( { ...allEventData , type : expectation . eventType } ) ,
290+ assertEqualSerializedEventData (
291+ { ...allEventData , type : expectation . eventType } ,
252292 expectation . output
253293 ) ;
254294 } ) ;
@@ -267,9 +307,12 @@ test("serializeEvent() adds text of current selection", () => {
267307 const start = document . getElementById ( "start" ) ;
268308 const end = document . getElementById ( "end" ) ;
269309 window . getSelection ( ) . setBaseAndExtent ( start , 0 , end , 0 ) ;
270- assert . equal ( serializeEvent ( { ...allEventData , type : "select" } ) , {
271- selectedText : "START\nMIDDLE\n" ,
272- } ) ;
310+ assertEqualSerializedEventData (
311+ { ...allEventData , type : "select" } ,
312+ {
313+ selectedText : "START\nMIDDLE\n" ,
314+ }
315+ ) ;
273316} ) ;
274317
275318test . run ( ) ;
0 commit comments