prefer-presence-queries.js
2.71 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'prefer-presence-queries';
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
docs: {
description: 'Ensure appropriate `get*`/`query*` queries are used with their respective matchers',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
marko: 'error',
},
},
messages: {
wrongPresenceQuery: 'Use `getBy*` queries rather than `queryBy*` for checking element is present',
wrongAbsenceQuery: 'Use `queryBy*` queries rather than `getBy*` for checking element is NOT present',
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
presence: {
type: 'boolean',
},
absence: {
type: 'boolean',
},
},
},
],
type: 'suggestion',
},
defaultOptions: [
{
presence: true,
absence: true,
},
],
create(context, [{ absence = true, presence = true }], helpers) {
return {
'CallExpression Identifier'(node) {
const expectCallNode = (0, node_utils_1.findClosestCallNode)(node, 'expect');
if (!expectCallNode || !(0, node_utils_1.isMemberExpression)(expectCallNode.parent)) {
return;
}
if (!helpers.isSyncQuery(node)) {
return;
}
const isPresenceQuery = helpers.isGetQueryVariant(node);
const expectStatement = expectCallNode.parent;
const isPresenceAssert = helpers.isPresenceAssert(expectStatement);
const isAbsenceAssert = helpers.isAbsenceAssert(expectStatement);
if (!isPresenceAssert && !isAbsenceAssert) {
return;
}
if (presence && isPresenceAssert && !isPresenceQuery) {
context.report({ node, messageId: 'wrongPresenceQuery' });
}
else if (absence && isAbsenceAssert && isPresenceQuery) {
context.report({ node, messageId: 'wrongAbsenceQuery' });
}
},
};
},
});