1
1
import { test } from "uvu" ;
2
+ import lodash from "lodash" ;
2
3
import * as assert from "uvu/assert" ;
3
4
import { serializeEvent } from "../src/event-to-object.js" ;
4
5
import "./tooling/setup.js" ;
5
6
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
+
6
41
const allTargetData = {
7
42
files : [
8
43
{
@@ -21,33 +56,38 @@ const allTargetData = {
21
56
{
22
57
case : "adds 'files' and 'value' attributes for INPUT if type=file" ,
23
58
tagName : "INPUT" ,
24
- otherAttrs : { type : "file" } ,
59
+ addTargetAttrs : { type : "file" } ,
25
60
output : {
26
- files : allTargetData . files ,
27
- value : allTargetData . value ,
61
+ target : {
62
+ files : allTargetData . files ,
63
+ value : allTargetData . value ,
64
+ } ,
28
65
} ,
29
66
} ,
30
67
...[ "BUTTON" , "INPUT" , "OPTION" , "LI" , "METER" , "PROGRESS" , "PARAM" ] . map (
31
68
( tagName ) => ( {
32
69
case : `adds 'value' attribute for ${ tagName } element` ,
33
70
tagName,
34
- output : { value : allTargetData . value } ,
71
+ output : { target : { value : allTargetData . value } } ,
35
72
} )
36
73
) ,
37
74
...[ "AUDIO" , "VIDEO" ] . map ( ( tagName ) => ( {
38
75
case : `adds 'currentTime' attribute for ${ tagName } element` ,
39
76
tagName,
40
- output : { currentTime : allTargetData . currentTime } ,
77
+ output : { target : { currentTime : allTargetData . currentTime } } ,
41
78
} ) ) ,
42
79
] . forEach ( ( expectation ) => {
43
80
test ( `serializeEvent() ${ expectation . case } ` , ( ) => {
44
81
const eventData = {
45
- target : { ...allTargetData , tagName : expectation . tagName } ,
82
+ target : {
83
+ ...allTargetData ,
84
+ tagName : expectation . tagName ,
85
+ } ,
46
86
} ;
47
- if ( expectation . otherAttrs ) {
48
- Object . assign ( eventData . target , expectation . otherAttrs ) ;
87
+ if ( expectation . addTargetAttrs ) {
88
+ Object . assign ( eventData . target , expectation . addTargetAttrs ) ;
49
89
}
50
- assert . equal ( serializeEvent ( eventData ) , expectation . output ) ;
90
+ assertEqualSerializedEventData ( eventData , expectation . output ) ;
51
91
} ) ;
52
92
} ) ;
53
93
@@ -247,8 +287,8 @@ const allEventData = {
247
287
} ,
248
288
] . forEach ( ( expectation ) => {
249
289
test ( `serializeEvent() adds ${ expectation . case } attributes` , ( ) => {
250
- assert . equal (
251
- serializeEvent ( { ...allEventData , type : expectation . eventType } ) ,
290
+ assertEqualSerializedEventData (
291
+ { ...allEventData , type : expectation . eventType } ,
252
292
expectation . output
253
293
) ;
254
294
} ) ;
@@ -267,9 +307,12 @@ test("serializeEvent() adds text of current selection", () => {
267
307
const start = document . getElementById ( "start" ) ;
268
308
const end = document . getElementById ( "end" ) ;
269
309
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
+ ) ;
273
316
} ) ;
274
317
275
318
test . run ( ) ;
0 commit comments