Skip to content

Commit 6a3c896

Browse files
committed
fix: remove interop and deep DTL imports
1 parent d7483f0 commit 6a3c896

16 files changed

+167
-90
lines changed

src/_interop/dom-events.d.ts

-26
This file was deleted.

src/_interop/dom-helpers.d.ts

-3
This file was deleted.

src/_interop/dtl.ts

-3
This file was deleted.

src/_interop/dtlEventMap.ts

-3
This file was deleted.

src/_interop/dtlHelpers.ts

-3
This file was deleted.

src/event/createEvent.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import {getWindow} from '../utils'
2-
import {eventMap, eventMapKeys} from './eventMap'
2+
import {eventMap} from './eventMap'
33
import {
44
type EventType,
55
type EventTypeInit,
66
type FixedDocumentEventMap,
77
} from './types'
88

9-
const eventInitializer = {
9+
interface InterfaceMap {
10+
ClipboardEvent: {type: ClipboardEvent; init: ClipboardEventInit}
11+
InputEvent: {type: InputEvent; init: InputEventInit}
12+
MouseEvent: {type: MouseEvent; init: MouseEventInit}
13+
PointerEvent: {type: PointerEvent; init: PointerEventInit}
14+
KeyboardEvent: {type: KeyboardEvent; init: KeyboardEventInit}
15+
}
16+
type InterfaceNames = typeof eventMap[keyof typeof eventMap]['EventType']
17+
type Interface<k extends InterfaceNames> = k extends keyof InterfaceMap
18+
? InterfaceMap[k]
19+
: never
20+
21+
const eventInitializer: {
22+
[k in InterfaceNames]: Array<
23+
(e: Interface<k>['type'], i: Interface<k>['init']) => void
24+
>
25+
} = {
1026
ClipboardEvent: [initClipboardEvent],
27+
Event: [],
1128
InputEvent: [initUIEvent, initInputEvent],
1229
MouseEvent: [initUIEvent, initUIEventModififiers, initMouseEvent],
1330
PointerEvent: [
@@ -17,18 +34,19 @@ const eventInitializer = {
1734
initPointerEvent,
1835
],
1936
KeyboardEvent: [initUIEvent, initUIEventModififiers, initKeyboardEvent],
20-
} as Record<EventInterface, undefined | Array<(e: Event, i: EventInit) => void>>
37+
}
2138

2239
export function createEvent<K extends EventType>(
2340
type: K,
2441
target: Element,
2542
init?: EventTypeInit<K>,
2643
) {
2744
const window = getWindow(target)
28-
const {EventType, defaultInit} =
29-
eventMap[eventMapKeys[type] as keyof typeof eventMap]
45+
const {EventType, defaultInit} = eventMap[type]
3046
const event = new (getEventConstructors(window)[EventType])(type, defaultInit)
31-
eventInitializer[EventType]?.forEach(f => f(event, init ?? {}))
47+
eventInitializer[EventType].forEach(f =>
48+
f(event as never, (init ?? {}) as never),
49+
)
3250

3351
return event as FixedDocumentEventMap[K]
3452
}

src/event/eventMap.ts

+105-13
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,131 @@
1-
import dtlEvents from '../_interop/dtlEventMap'
21
import {EventType} from './types'
32

43
export const eventMap = {
5-
...dtlEvents.eventMap,
6-
7-
click: {
4+
auxclick: {
85
EventType: 'PointerEvent',
96
defaultInit: {bubbles: true, cancelable: true, composed: true},
107
},
11-
auxclick: {
8+
beforeinput: {
9+
EventType: 'InputEvent',
10+
defaultInit: {bubbles: true, cancelable: true, composed: true},
11+
},
12+
click: {
1213
EventType: 'PointerEvent',
1314
defaultInit: {bubbles: true, cancelable: true, composed: true},
1415
},
1516
contextmenu: {
1617
EventType: 'PointerEvent',
1718
defaultInit: {bubbles: true, cancelable: true, composed: true},
1819
},
19-
beforeInput: {
20+
copy: {
21+
EventType: 'ClipboardEvent',
22+
defaultInit: {bubbles: true, cancelable: true, composed: true},
23+
},
24+
change: {
25+
EventType: 'Event',
26+
defaultInit: {bubbles: true, cancelable: false},
27+
},
28+
cut: {
29+
EventType: 'ClipboardEvent',
30+
defaultInit: {bubbles: true, cancelable: true, composed: true},
31+
},
32+
dblclick: {
33+
EventType: 'MouseEvent',
34+
defaultInit: {bubbles: true, cancelable: true, composed: true},
35+
},
36+
keydown: {
37+
EventType: 'KeyboardEvent',
38+
defaultInit: {bubbles: true, cancelable: true, composed: true},
39+
},
40+
keypress: {
41+
EventType: 'KeyboardEvent',
42+
defaultInit: {bubbles: true, cancelable: true, composed: true},
43+
},
44+
keyup: {
45+
EventType: 'KeyboardEvent',
46+
defaultInit: {bubbles: true, cancelable: true, composed: true},
47+
},
48+
paste: {
49+
EventType: 'ClipboardEvent',
50+
defaultInit: {bubbles: true, cancelable: true, composed: true},
51+
},
52+
input: {
2053
EventType: 'InputEvent',
54+
defaultInit: {bubbles: true, cancelable: false, composed: true},
55+
},
56+
mousedown: {
57+
EventType: 'MouseEvent',
2158
defaultInit: {bubbles: true, cancelable: true, composed: true},
2259
},
60+
mouseenter: {
61+
EventType: 'MouseEvent',
62+
defaultInit: {bubbles: false, cancelable: false, composed: true},
63+
},
64+
mouseleave: {
65+
EventType: 'MouseEvent',
66+
defaultInit: {bubbles: false, cancelable: false, composed: true},
67+
},
68+
mousemove: {
69+
EventType: 'MouseEvent',
70+
defaultInit: {bubbles: true, cancelable: true, composed: true},
71+
},
72+
mouseout: {
73+
EventType: 'MouseEvent',
74+
defaultInit: {bubbles: true, cancelable: true, composed: true},
75+
},
76+
mouseover: {
77+
EventType: 'MouseEvent',
78+
defaultInit: {bubbles: true, cancelable: true, composed: true},
79+
},
80+
mouseup: {
81+
EventType: 'MouseEvent',
82+
defaultInit: {bubbles: true, cancelable: true, composed: true},
83+
},
84+
pointerover: {
85+
EventType: 'PointerEvent',
86+
defaultInit: {bubbles: true, cancelable: true, composed: true},
87+
},
88+
pointerenter: {
89+
EventType: 'PointerEvent',
90+
defaultInit: {bubbles: false, cancelable: false},
91+
},
92+
pointerdown: {
93+
EventType: 'PointerEvent',
94+
defaultInit: {bubbles: true, cancelable: true, composed: true},
95+
},
96+
pointermove: {
97+
EventType: 'PointerEvent',
98+
defaultInit: {bubbles: true, cancelable: true, composed: true},
99+
},
100+
pointerup: {
101+
EventType: 'PointerEvent',
102+
defaultInit: {bubbles: true, cancelable: true, composed: true},
103+
},
104+
pointercancel: {
105+
EventType: 'PointerEvent',
106+
defaultInit: {bubbles: true, cancelable: false, composed: true},
107+
},
108+
pointerout: {
109+
EventType: 'PointerEvent',
110+
defaultInit: {bubbles: true, cancelable: true, composed: true},
111+
},
112+
pointerleave: {
113+
EventType: 'PointerEvent',
114+
defaultInit: {bubbles: false, cancelable: false},
115+
},
116+
submit: {
117+
EventType: 'Event',
118+
defaultInit: {bubbles: true, cancelable: true},
119+
},
23120
} as const
24121

25-
export const eventMapKeys: {
26-
[k in keyof DocumentEventMap]?: keyof typeof eventMap
27-
} = Object.fromEntries(Object.keys(eventMap).map(k => [k.toLowerCase(), k]))
28-
29122
function getEventClass(type: EventType) {
30-
const k = eventMapKeys[type]
31-
return k && eventMap[k].EventType
123+
return eventMap[type].EventType
32124
}
33125

34126
const mouseEvents = ['MouseEvent', 'PointerEvent']
35127
export function isMouseEvent(type: EventType) {
36-
return mouseEvents.includes(getEventClass(type) as string)
128+
return mouseEvents.includes(getEventClass(type))
37129
}
38130

39131
export function isKeyboardEvent(type: EventType) {

src/event/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export type EventType = keyof DocumentEventMap
1+
import {eventMap} from './eventMap'
2+
3+
export type EventType = keyof typeof eventMap
24

35
export type EventTypeInit<K extends EventType> = SpecificEventInit<
46
FixedDocumentEventMap[K]

src/event/wrapEvent.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import dtl from '../_interop/dtl'
2-
3-
const {getConfig} = dtl
1+
import {getConfig} from '@testing-library/dom'
42

53
export function wrapEvent<R>(cb: () => R, _element: Element) {
64
return getConfig().eventWrapper(cb) as unknown as R

src/setup/wrapAsync.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import dtl from '../_interop/dtl'
2-
3-
const {getConfig} = dtl
1+
import {getConfig} from '@testing-library/dom'
42

53
/**
64
* Wrap an internal Promise

src/utility/selectOptions.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import dtl from '../_interop/dtl'
1+
import {getConfig} from '@testing-library/dom'
22
import {hasPointerEvents, isDisabled, isElementType, wait} from '../utils'
33
import {type Instance} from '../setup'
44
import {focusElement} from '../event'
55

6-
const {getConfig} = dtl
7-
86
export async function selectOptions(
97
this: Instance,
108
select: Element,

src/utils/misc/getWindow.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
import dtlHelpers from '../../_interop/dtlHelpers'
1+
export function getWindow(node: Node) {
2+
if (isDocument(node) && node.defaultView) {
3+
return node.defaultView
4+
} else if (node.ownerDocument?.defaultView) {
5+
return node.ownerDocument.defaultView
6+
}
7+
throw new Error(
8+
`Could not determine window of node. Node was ${describe(node)}`,
9+
)
10+
}
211

3-
const {getWindowFromNode} = dtlHelpers
12+
function isDocument(node: Node): node is Document {
13+
return node.nodeType === 9
14+
}
415

5-
export function getWindow(node: Node) {
6-
return getWindowFromNode(node) as Window & typeof globalThis
16+
function describe(val: unknown) {
17+
return typeof val === 'function'
18+
? `function ${val.name}`
19+
: val === null
20+
? 'null'
21+
: String(val)
722
}

tests/_helpers/dom-events.d.ts

-9
This file was deleted.

tests/_helpers/listeners.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {eventMapKeys} from '#src/event/eventMap'
1+
import {eventMap} from '#src/event/eventMap'
22
import {isElementType} from '#src/utils'
33
import {MouseButton, MouseButtonFlip} from '#src/system/pointer/buttons'
44

@@ -31,6 +31,15 @@ export function addEventListener(
3131

3232
export type EventHandlers = {[k in keyof DocumentEventMap]?: EventListener}
3333

34+
const loggedEvents = [
35+
...(Object.keys(eventMap) as Array<keyof typeof eventMap>),
36+
'focus',
37+
'focusin',
38+
'focusout',
39+
'blur',
40+
'select',
41+
] as const
42+
3443
/**
3544
* Add listeners for logging events.
3645
*/
@@ -50,9 +59,7 @@ export function addListeners(
5059

5160
const generalListener = mocks.fn(eventHandler).mockName('eventListener')
5261

53-
for (const eventType of Object.keys(eventMapKeys) as Array<
54-
keyof typeof eventMapKeys
55-
>) {
62+
for (const eventType of loggedEvents) {
5663
addEventListener(element, eventType, (...args) => {
5764
generalListener(...args)
5865
eventHandlers[eventType]?.(...args)

tests/setup/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {getSpy} from './_mockApis'
2-
import DOMTestingLibrary from '#src/_interop/dtl'
2+
import {getConfig} from '@testing-library/dom'
33
import userEvent from '#src'
44
import {type Instance, type UserEventApi} from '#src/setup/setup'
55
import {render} from '#testHelpers'
66

7-
const {getConfig} = DOMTestingLibrary
8-
97
type ApiDeclarations = {
108
[api in keyof UserEventApi]: {
119
args?: unknown[]

tests/utils/misc/isVisible.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import DOMTestingLibrary from '#src/_interop/dtl'
1+
import {screen} from '@testing-library/dom'
22
import {isVisible} from '#src/utils'
33
import {setup} from '#testHelpers'
44

5-
const {screen} = DOMTestingLibrary
6-
75
test('check if element is visible', async () => {
86
setup(`
97
<input data-testid="visibleInput"/>

0 commit comments

Comments
 (0)