1- import { createEvent , SyntheticEvent } from '../event' ;
1+ import { format } from 'pretty-format' ;
2+ import type { TestInstance } from 'test-renderer' ;
23
3- const dispatchConfig = { registrationName : 'onFocus' } ;
4+ import { createEvent , getEventInternals , SyntheticEvent } from '../event' ;
5+ import type { EventTypeConfig } from '../event-types' ;
6+
7+ const typeConfig : EventTypeConfig = { kind : 'direct' , handlerName : 'onFocus' } ;
48
59afterEach ( ( ) => {
610 jest . restoreAllMocks ( ) ;
@@ -10,9 +14,9 @@ test('exposes init values', () => {
1014 const nativeEvent = { target : 1 } ;
1115 const event = new SyntheticEvent (
1216 'layout' ,
13- { cancelable : true , rnIsDirect : true , timeStamp : 123 } ,
17+ { cancelable : true , timeStamp : 123 } ,
1418 nativeEvent ,
15- dispatchConfig ,
19+ typeConfig ,
1620 ) ;
1721
1822 expect ( event . type ) . toBe ( 'layout' ) ;
@@ -21,29 +25,39 @@ test('exposes init values', () => {
2125 expect ( event . rnIsDirect ) . toBe ( true ) ;
2226 expect ( event . timeStamp ) . toBe ( 123 ) ;
2327 expect ( event . nativeEvent ) . toBe ( nativeEvent ) ;
24- expect ( event . dispatchConfig ) . toBe ( dispatchConfig ) ;
28+ expect ( event . dispatchConfig ) . toEqual ( { registrationName : 'onFocus' } ) ;
2529 expect ( event . isTrusted ) . toBe ( true ) ;
2630} ) ;
2731
2832test ( 'has no target and phase outside of dispatch' , ( ) => {
29- const event = new SyntheticEvent ( 'focus' , { bubbles : true } , { } , dispatchConfig ) ;
33+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
3034
3135 expect ( event . target ) . toBeNull ( ) ;
3236 expect ( event . currentTarget ) . toBeNull ( ) ;
3337 expect ( event . eventPhase ) . toBe ( SyntheticEvent . NONE ) ;
3438 expect ( event . composedPath ( ) ) . toEqual ( [ ] ) ;
3539} ) ;
3640
41+ test ( 'keeps dispatch fields out of printed and compared events' , ( ) => {
42+ const event = new SyntheticEvent ( 'focus' , { timeStamp : 1 } , { } , typeConfig ) ;
43+ const target = { type : 'View' , props : { testID : 'target' } } as unknown as TestInstance ;
44+ getEventInternals ( event ) . target = target ;
45+
46+ expect ( event . target ) . toBe ( target ) ;
47+ expect ( event ) . toEqual ( new SyntheticEvent ( 'focus' , { timeStamp : 1 } , { } , typeConfig ) ) ;
48+ expect ( format ( event ) ) . not . toContain ( 'target' ) ;
49+ } ) ;
50+
3751test ( 'defaults timeStamp to performance.now()' , ( ) => {
3852 jest . spyOn ( performance , 'now' ) . mockReturnValue ( 456 ) ;
3953
40- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
54+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
4155
4256 expect ( event . timeStamp ) . toBe ( 456 ) ;
4357} ) ;
4458
4559test ( 'preventDefault() marks cancelable event as default prevented' , ( ) => {
46- const event = new SyntheticEvent ( 'focus' , { cancelable : true } , { } , dispatchConfig ) ;
60+ const event = new SyntheticEvent ( 'focus' , { cancelable : true } , { } , typeConfig ) ;
4761
4862 event . preventDefault ( ) ;
4963
@@ -52,7 +66,7 @@ test('preventDefault() marks cancelable event as default prevented', () => {
5266} ) ;
5367
5468test ( 'preventDefault() does nothing on non-cancelable event' , ( ) => {
55- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
69+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
5670
5771 event . preventDefault ( ) ;
5872
@@ -63,7 +77,7 @@ test('preventDefault() does nothing on non-cancelable event', () => {
6377test . each ( [ 'stopPropagation' , 'stopImmediatePropagation' ] as const ) (
6478 '%s() stops propagation' ,
6579 ( method ) => {
66- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
80+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
6781 expect ( event . isPropagationStopped ( ) ) . toBe ( false ) ;
6882
6983 event [ method ] ( ) ;
@@ -74,7 +88,7 @@ test.each(['stopPropagation', 'stopImmediatePropagation'] as const)(
7488) ;
7589
7690test ( 'cancelBubble can only be set to true' , ( ) => {
77- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
91+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
7892
7993 event . cancelBubble = true ;
8094 event . cancelBubble = false ;
@@ -83,13 +97,13 @@ test('cancelBubble can only be set to true', () => {
8397} ) ;
8498
8599test ( 'persist() is a no-op' , ( ) => {
86- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
100+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
87101
88102 expect ( ( ) => event . persist ( ) ) . not . toThrow ( ) ;
89103} ) ;
90104
91105test ( 'defines event phase constants on class and prototype' , ( ) => {
92- const event = new SyntheticEvent ( 'focus' , { } , { } , dispatchConfig ) ;
106+ const event = new SyntheticEvent ( 'focus' , { } , { } , typeConfig ) ;
93107
94108 expect ( [
95109 SyntheticEvent . NONE ,
@@ -126,6 +140,13 @@ describe('createEvent()', () => {
126140
127141 expect ( event ?. bubbles ) . toBe ( false ) ;
128142 expect ( event ?. rnIsDirect ) . toBe ( false ) ;
143+ expect ( event ?. dispatchConfig ) . toEqual ( {
144+ phasedRegistrationNames : {
145+ bubbled : 'onPointerEnter' ,
146+ captured : 'onPointerEnterCapture' ,
147+ skipBubbling : true ,
148+ } ,
149+ } ) ;
129150 } ) ;
130151
131152 test ( 'creates direct event' , ( ) => {
0 commit comments