1- // @vitest -environment happy-dom
1+ // @vitest -environment jsdom
22import { describe , it , expect , beforeEach , vi , type MockedFunction } from 'vitest'
33import {
44 addButtonEventListeners ,
@@ -7,6 +7,29 @@ import {
77 type eventHandler ,
88} from '@components/scripts/elementListeners/index'
99
10+ const createTouchEndEvent = ( ) : TouchEvent => {
11+ if ( typeof TouchEvent === 'function' ) {
12+ return new TouchEvent ( 'touchend' , {
13+ touches : [ ] ,
14+ targetTouches : [ ] ,
15+ changedTouches : [ ] ,
16+ } )
17+ }
18+
19+ const fallbackEvent = new Event ( 'touchend' ) as TouchEvent & {
20+ touches : TouchList & Touch [ ]
21+ targetTouches : TouchList & Touch [ ]
22+ changedTouches : TouchList & Touch [ ]
23+ }
24+ const emptyTouches = [ ] as unknown as TouchList & Touch [ ]
25+
26+ fallbackEvent . touches = emptyTouches
27+ fallbackEvent . targetTouches = emptyTouches
28+ fallbackEvent . changedTouches = emptyTouches
29+
30+ return fallbackEvent
31+ }
32+
1033describe ( 'Element Listeners' , ( ) => {
1134 let mockHandler : MockedFunction < eventHandler >
1235
@@ -83,11 +106,7 @@ describe('Element Listeners', () => {
83106 it ( 'should attach touchend event listener' , ( ) => {
84107 addButtonEventListeners ( button , mockHandler )
85108
86- const touchEvent = new TouchEvent ( 'touchend' , {
87- touches : [ ] ,
88- targetTouches : [ ] ,
89- changedTouches : [ ] ,
90- } )
109+ const touchEvent = createTouchEndEvent ( )
91110 button . dispatchEvent ( touchEvent )
92111
93112 expect ( mockHandler ) . toHaveBeenCalledOnce ( )
@@ -105,11 +124,7 @@ describe('Element Listeners', () => {
105124 button . dispatchEvent ( enterEvent )
106125
107126 // Touch event
108- const touchEvent = new TouchEvent ( 'touchend' , {
109- touches : [ ] ,
110- targetTouches : [ ] ,
111- changedTouches : [ ] ,
112- } )
127+ const touchEvent = createTouchEndEvent ( )
113128 button . dispatchEvent ( touchEvent )
114129
115130 expect ( mockHandler ) . toHaveBeenCalledTimes ( 3 )
@@ -161,11 +176,7 @@ describe('Element Listeners', () => {
161176 it ( 'should attach touchend event listener' , ( ) => {
162177 addLinkEventListeners ( link , mockHandler )
163178
164- const touchEvent = new TouchEvent ( 'touchend' , {
165- touches : [ ] ,
166- targetTouches : [ ] ,
167- changedTouches : [ ] ,
168- } )
179+ const touchEvent = createTouchEndEvent ( )
169180 link . dispatchEvent ( touchEvent )
170181
171182 expect ( mockHandler ) . toHaveBeenCalledOnce ( )
@@ -183,11 +194,7 @@ describe('Element Listeners', () => {
183194 link . dispatchEvent ( enterEvent )
184195
185196 // Touch event
186- const touchEvent = new TouchEvent ( 'touchend' , {
187- touches : [ ] ,
188- targetTouches : [ ] ,
189- changedTouches : [ ] ,
190- } )
197+ const touchEvent = createTouchEndEvent ( )
191198 link . dispatchEvent ( touchEvent )
192199
193200 expect ( mockHandler ) . toHaveBeenCalledTimes ( 3 )
@@ -328,11 +335,7 @@ describe('Element Listeners', () => {
328335 addButtonEventListeners ( button , buttonHandler )
329336 addLinkEventListeners ( link , linkHandler )
330337
331- const touchEvent = new TouchEvent ( 'touchend' , {
332- touches : [ ] ,
333- targetTouches : [ ] ,
334- changedTouches : [ ] ,
335- } )
338+ const touchEvent = createTouchEndEvent ( )
336339
337340 button . dispatchEvent ( touchEvent )
338341 link . dispatchEvent ( touchEvent )
0 commit comments