| 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.RULE_NAME = void 0;
|
|---|
| 4 | const create_testing_library_rule_1 = require("../create-testing-library-rule");
|
|---|
| 5 | const node_utils_1 = require("../node-utils");
|
|---|
| 6 | exports.RULE_NAME = 'prefer-query-matchers';
|
|---|
| 7 | exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
|
|---|
| 8 | name: exports.RULE_NAME,
|
|---|
| 9 | meta: {
|
|---|
| 10 | docs: {
|
|---|
| 11 | description: 'Ensure the configured `get*`/`query*` query is used with the corresponding matchers',
|
|---|
| 12 | recommendedConfig: {
|
|---|
| 13 | dom: false,
|
|---|
| 14 | angular: false,
|
|---|
| 15 | react: false,
|
|---|
| 16 | vue: false,
|
|---|
| 17 | marko: false,
|
|---|
| 18 | },
|
|---|
| 19 | },
|
|---|
| 20 | messages: {
|
|---|
| 21 | wrongQueryForMatcher: 'Use `{{ query }}By*` queries for {{ matcher }}',
|
|---|
| 22 | },
|
|---|
| 23 | schema: [
|
|---|
| 24 | {
|
|---|
| 25 | type: 'object',
|
|---|
| 26 | additionalProperties: false,
|
|---|
| 27 | properties: {
|
|---|
| 28 | validEntries: {
|
|---|
| 29 | type: 'array',
|
|---|
| 30 | items: {
|
|---|
| 31 | type: 'object',
|
|---|
| 32 | properties: {
|
|---|
| 33 | query: {
|
|---|
| 34 | type: 'string',
|
|---|
| 35 | enum: ['get', 'query'],
|
|---|
| 36 | },
|
|---|
| 37 | matcher: {
|
|---|
| 38 | type: 'string',
|
|---|
| 39 | },
|
|---|
| 40 | },
|
|---|
| 41 | },
|
|---|
| 42 | },
|
|---|
| 43 | },
|
|---|
| 44 | },
|
|---|
| 45 | ],
|
|---|
| 46 | type: 'suggestion',
|
|---|
| 47 | },
|
|---|
| 48 | defaultOptions: [
|
|---|
| 49 | {
|
|---|
| 50 | validEntries: [],
|
|---|
| 51 | },
|
|---|
| 52 | ],
|
|---|
| 53 | create(context, [{ validEntries }], helpers) {
|
|---|
| 54 | return {
|
|---|
| 55 | 'CallExpression Identifier'(node) {
|
|---|
| 56 | const expectCallNode = (0, node_utils_1.findClosestCallNode)(node, 'expect');
|
|---|
| 57 | if (!expectCallNode || !(0, node_utils_1.isMemberExpression)(expectCallNode.parent)) {
|
|---|
| 58 | return;
|
|---|
| 59 | }
|
|---|
| 60 | if (!helpers.isSyncQuery(node)) {
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 | const isGetBy = helpers.isGetQueryVariant(node);
|
|---|
| 64 | const expectStatement = expectCallNode.parent;
|
|---|
| 65 | for (const entry of validEntries) {
|
|---|
| 66 | const { query, matcher } = entry;
|
|---|
| 67 | const isMatchingAssertForThisEntry = helpers.isMatchingAssert(expectStatement, matcher);
|
|---|
| 68 | if (!isMatchingAssertForThisEntry) {
|
|---|
| 69 | continue;
|
|---|
| 70 | }
|
|---|
| 71 | const actualQuery = isGetBy ? 'get' : 'query';
|
|---|
| 72 | if (query !== actualQuery) {
|
|---|
| 73 | context.report({
|
|---|
| 74 | node,
|
|---|
| 75 | messageId: 'wrongQueryForMatcher',
|
|---|
| 76 | data: { query, matcher },
|
|---|
| 77 | });
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | },
|
|---|
| 81 | };
|
|---|
| 82 | },
|
|---|
| 83 | });
|
|---|