Skip to content

Commit e6346f7

Browse files
fix: mock brower event about testing-library#1327
1 parent fdc12ec commit e6346f7

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"aria-query": "5.3.0",
6666
"chalk": "^4.1.0",
6767
"dom-accessibility-api": "^0.5.9",
68+
"lodash": "^4.17.21",
6869
"lz-string": "^1.5.0",
6970
"pretty-format": "^27.0.2"
7071
},

src/event-map.js

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
import _ from 'lodash'
3+
import {valiateByAttr} from './life-cycle'
14
export const eventMap = {
25
// Clipboard Events
36
copy: {
@@ -112,10 +115,24 @@ export const eventMap = {
112115
dragOver: {
113116
EventType: 'DragEvent',
114117
defaultInit: {bubbles: true, cancelable: true, composed: true},
118+
cond: conditionObj => {
119+
const shouldTrueAttrArr = ['dragover']
120+
const shouldFalseAttrArr = ['defaultprevented']
121+
if (valiateByAttr(conditionObj, [], shouldFalseAttrArr)) {
122+
return true
123+
}
124+
if (valiateByAttr(conditionObj, shouldTrueAttrArr, [])) {
125+
return true
126+
}
127+
return false
128+
},
115129
},
116130
dragStart: {
117131
EventType: 'DragEvent',
118132
defaultInit: {bubbles: true, cancelable: true, composed: true},
133+
before: (config = {}) => {
134+
return _.pick(config, ['dragstart', 'defaultprevented'])
135+
},
119136
},
120137
drop: {
121138
EventType: 'DragEvent',

src/events.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import {getConfig} from './config'
22
import {getWindowFromNode} from './helpers'
33
import {eventMap, eventAliasMap} from './event-map'
4+
import {beforeFn} from './life-cycle'
5+
const NodeEventMap = new Map()
46

5-
function fireEvent(element, event) {
7+
function fireEvent(
8+
element,
9+
event,
10+
cond = () => {
11+
return true
12+
},
13+
before,
14+
) {
615
return getConfig().eventWrapper(() => {
716
if (!event) {
817
throw new Error(
@@ -14,7 +23,21 @@ function fireEvent(element, event) {
1423
`Unable to fire a "${event.type}" event - please provide a DOM element.`,
1524
)
1625
}
17-
return element.dispatchEvent(event)
26+
const CloneConditionValue = NodeEventMap.get(element)
27+
const condition = beforeFn(CloneConditionValue, event, before)
28+
if (cond && typeof cond == 'function') {
29+
if (cond(condition)) {
30+
element.dispatchEvent(event)
31+
}
32+
}
33+
if (event.defaultPrevented) {
34+
condition.defaultprevented = true
35+
}
36+
NodeEventMap.set(element, condition)
37+
if (event.cancelable && event.defaultPrevented) {
38+
return false
39+
}
40+
return true
1841
})
1942
}
2043

@@ -93,12 +116,14 @@ function createEvent(
93116
}
94117

95118
Object.keys(eventMap).forEach(key => {
96-
const {EventType, defaultInit} = eventMap[key]
119+
const {EventType, defaultInit, cond, before} = eventMap[key]
97120
const eventName = key.toLowerCase()
98121

99122
createEvent[key] = (node, init) =>
100123
createEvent(eventName, node, init, {EventType, defaultInit})
101-
fireEvent[key] = (node, init) => fireEvent(node, createEvent[key](node, init))
124+
fireEvent[key] = (node, init) => {
125+
return fireEvent(node, createEvent[key](node, init), cond, before)
126+
}
102127
})
103128

104129
// function written after some investigation here:

src/life-cycle.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @description before->cond->dispatch
3+
*/
4+
5+
export const beforeFn = (
6+
initValue,
7+
event,
8+
before = config => {
9+
return config
10+
},
11+
) => {
12+
let ConditionValue = initValue
13+
if (before && typeof before == 'function') {
14+
ConditionValue = before(ConditionValue)
15+
}
16+
if (!ConditionValue) {
17+
ConditionValue = {
18+
[`${event.type}`]: true,
19+
}
20+
}
21+
if (ConditionValue) {
22+
ConditionValue = {
23+
...ConditionValue,
24+
[`${event.type}`]: true,
25+
}
26+
}
27+
return ConditionValue
28+
}
29+
30+
/**
31+
* @description judge attr exist about condition
32+
* @param {*} element
33+
* @param {*} AttrObj
34+
* @returns
35+
*/
36+
export function valiateByAttr(element, AttrTrueObj, AttrFalseObj) {
37+
if (!element) {
38+
return false
39+
}
40+
let trueFlag = true
41+
for (const i in AttrTrueObj) {
42+
if (element[`${AttrTrueObj[i]}`]) {
43+
trueFlag = false
44+
}
45+
}
46+
let falseFlag = true
47+
for (const i in AttrFalseObj) {
48+
if (element[`${AttrFalseObj[i]}`]) {
49+
falseFlag = false
50+
}
51+
}
52+
return trueFlag && falseFlag
53+
}

0 commit comments

Comments
 (0)