prefer-user-event.js
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAPPING_TO_USER_EVENT = exports.UserEventMethods = exports.RULE_NAME = void 0;
const utils_1 = require("@typescript-eslint/utils");
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'prefer-user-event';
exports.UserEventMethods = [
'click',
'dblClick',
'type',
'upload',
'clear',
'selectOptions',
'deselectOptions',
'tab',
'hover',
'unhover',
'paste',
];
exports.MAPPING_TO_USER_EVENT = {
click: ['click', 'type', 'selectOptions', 'deselectOptions'],
change: ['upload', 'type', 'clear', 'selectOptions', 'deselectOptions'],
dblClick: ['dblClick'],
input: ['type', 'upload', 'selectOptions', 'deselectOptions', 'paste'],
keyDown: ['type', 'tab'],
keyPress: ['type'],
keyUp: ['type', 'tab'],
mouseDown: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
mouseEnter: ['hover', 'selectOptions', 'deselectOptions'],
mouseLeave: ['unhover'],
mouseMove: ['hover', 'unhover', 'selectOptions', 'deselectOptions'],
mouseOut: ['unhover'],
mouseOver: ['hover', 'selectOptions', 'deselectOptions'],
mouseUp: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
paste: ['paste'],
pointerDown: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
pointerEnter: ['hover', 'selectOptions', 'deselectOptions'],
pointerLeave: ['unhover'],
pointerMove: ['hover', 'unhover', 'selectOptions', 'deselectOptions'],
pointerOut: ['unhover'],
pointerOver: ['hover', 'selectOptions', 'deselectOptions'],
pointerUp: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
};
function buildErrorMessage(fireEventMethod) {
const userEventMethods = exports.MAPPING_TO_USER_EVENT[fireEventMethod].map((methodName) => `userEvent.${methodName}`);
return userEventMethods.join(', ').replace(/, ([a-zA-Z.]+)$/, ', or $1');
}
const fireEventMappedMethods = Object.keys(exports.MAPPING_TO_USER_EVENT);
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Suggest using `userEvent` over `fireEvent` for simulating user interactions',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
marko: false,
},
},
messages: {
preferUserEvent: 'Prefer using {{userEventMethods}} over fireEvent.{{fireEventMethod}}',
},
schema: [
{
type: 'object',
properties: {
allowedMethods: { type: 'array' },
},
},
],
},
defaultOptions: [{ allowedMethods: [] }],
create(context, [options], helpers) {
const { allowedMethods } = options;
const createEventVariables = {};
const isfireEventMethodAllowed = (methodName) => !fireEventMappedMethods.includes(methodName) ||
allowedMethods.includes(methodName);
const getFireEventMethodName = (callExpressionNode, node) => {
if (!utils_1.ASTUtils.isIdentifier(callExpressionNode.callee) &&
!(0, node_utils_1.isMemberExpression)(callExpressionNode.callee)) {
return node.name;
}
const secondArgument = callExpressionNode.arguments[1];
if (utils_1.ASTUtils.isIdentifier(secondArgument) &&
createEventVariables[secondArgument.name] !== undefined) {
return createEventVariables[secondArgument.name];
}
if (!(0, node_utils_1.isCallExpression)(secondArgument) ||
!helpers.isCreateEventUtil(secondArgument)) {
return node.name;
}
if (utils_1.ASTUtils.isIdentifier(secondArgument.callee)) {
return secondArgument.arguments[0]
.value;
}
return secondArgument.callee
.property.name;
};
return {
'CallExpression Identifier'(node) {
if (!helpers.isFireEventMethod(node)) {
return;
}
const closestCallExpression = (0, node_utils_1.findClosestCallExpressionNode)(node, true);
if (!closestCallExpression) {
return;
}
const fireEventMethodName = getFireEventMethodName(closestCallExpression, node);
if (!fireEventMethodName ||
isfireEventMethodAllowed(fireEventMethodName)) {
return;
}
context.report({
node: closestCallExpression.callee,
messageId: 'preferUserEvent',
data: {
userEventMethods: buildErrorMessage(fireEventMethodName),
fireEventMethod: fireEventMethodName,
},
});
},
VariableDeclarator(node) {
if (!(0, node_utils_1.isCallExpression)(node.init) ||
!helpers.isCreateEventUtil(node.init) ||
!utils_1.ASTUtils.isIdentifier(node.id)) {
return;
}
let fireEventMethodName = '';
if ((0, node_utils_1.isMemberExpression)(node.init.callee) &&
utils_1.ASTUtils.isIdentifier(node.init.callee.property)) {
fireEventMethodName = node.init.callee.property.name;
}
else if (node.init.arguments.length > 0) {
fireEventMethodName = node.init.arguments[0]
.value;
}
if (!isfireEventMethodAllowed(fireEventMethodName)) {
createEventVariables[node.id.name] = fireEventMethodName;
}
},
};
},
});