Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d49fdb9

Browse files
committedJan 21, 2020
feat: add support for waitForElementToBeRemoved
1 parent ea220a8 commit d49fdb9

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed
 

‎lib/rules/no-get-by-for-absent-elements.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ module.exports = {
2323
},
2424

2525
create: context => ({
26-
[`CallExpression > Identifier[name=/getBy|getAllBy/]`](node) {
27-
const expectCallNode = findClosestExpectStatement(node);
26+
[`Identifier[name=/getBy|getAllBy/]`](node) {
27+
const expectCallNode = findClosestCallNode(node, 'expect');
2828

29+
// expect(getByText("foo"))...
2930
if (expectCallNode) {
3031
const expectStatement = expectCallNode.parent;
3132
const matcher = expectStatement.property.name;
@@ -48,18 +49,30 @@ module.exports = {
4849
});
4950
}
5051
}
52+
53+
const waitCallNode = findClosestCallNode(
54+
node,
55+
'waitForElementToBeRemoved'
56+
);
57+
58+
if (waitCallNode) {
59+
return context.report({
60+
node,
61+
messageId: 'expectQueryBy',
62+
});
63+
}
5164
},
5265
}),
5366
};
5467

55-
function findClosestExpectStatement(node) {
68+
function findClosestCallNode(node, name) {
5669
if (!node.parent) {
5770
return false;
5871
}
5972

60-
if (node.type === 'CallExpression' && node.callee.name === 'expect') {
73+
if (node.type === 'CallExpression' && node.callee.name === name) {
6174
return node;
6275
} else {
63-
return findClosestExpectStatement(node.parent);
76+
return findClosestCallNode(node.parent, name);
6477
}
6578
}

‎tests/lib/rules/no-get-by-for-absent-elements.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@ const rule = require('../../../lib/rules/no-get-by-for-absent-elements');
55
const { ALL_QUERIES_METHODS } = require('../../../lib/utils');
66

77
const ruleTester = new RuleTester({
8-
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
8+
parserOptions: { ecmaVersion: 2017, sourceType: 'module' },
99
});
1010

11-
const queryByVariants = ALL_QUERIES_METHODS.reduce(
12-
(variants, method) => [
13-
...variants,
14-
...[`query${method}`, `queryAll${method}`],
15-
],
16-
[]
17-
);
1811
const getByVariants = ALL_QUERIES_METHODS.reduce(
1912
(variants, method) => [...variants, ...[`get${method}`, `getAll${method}`]],
2013
[]
2114
);
2215

2316
ruleTester.run('prefer-expect-query-by', rule, {
24-
valid: queryByVariants.reduce(
17+
valid: getByVariants.reduce(
2518
(validRules, queryName) => [
2619
...validRules,
2720
{ code: `expect(${queryName}('Hello')).toBeInTheDocument()` },
2821
{ code: `expect(${queryName}('Hello')).toBe("foo")` },
2922
{ code: `expect(${queryName}('Hello')).toBeTruthy()` },
23+
{ code: `expect(rendered.${queryName}('Hello')).toEqual("World")` },
24+
{ code: `expect(rendered.${queryName}('Hello')).not.toBeFalsy()` },
3025
],
3126
[]
3227
),
@@ -53,6 +48,22 @@ ruleTester.run('prefer-expect-query-by', rule, {
5348
code: `expect(${queryName}('Hello')).toBeFalsy()`,
5449
errors: [{ messageId: 'expectQueryBy' }],
5550
},
51+
{
52+
code: `expect(rendered.${queryName}('Hello')).toBeFalsy()`,
53+
errors: [{ messageId: 'expectQueryBy' }],
54+
},
55+
{
56+
code: `expect(rendered.${queryName}('Hello')).not.toBeTruthy()`,
57+
errors: [{ messageId: 'expectQueryBy' }],
58+
},
59+
{
60+
code: `(async () => {
61+
await waitForElementToBeRemoved(() => {
62+
return getByText("hello")
63+
})
64+
})()`,
65+
errors: [{ messageId: 'expectQueryBy' }],
66+
},
5667
],
5768
[]
5869
),

0 commit comments

Comments
 (0)
Please sign in to comment.