From 54dd53e4d4c8a89b0ff23f61dbdcd1ce036b2a98 Mon Sep 17 00:00:00 2001 From: Gonzalo D'Elia Date: Wed, 10 Jun 2020 20:13:10 -0300 Subject: [PATCH] docs: update prefer-screen-queries with allowed methods and properties --- docs/rules/prefer-screen-queries.md | 15 ++++++++-- tests/lib/rules/prefer-screen-queries.test.ts | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/rules/prefer-screen-queries.md b/docs/rules/prefer-screen-queries.md index 1ae86b40..e3743a43 100644 --- a/docs/rules/prefer-screen-queries.md +++ b/docs/rules/prefer-screen-queries.md @@ -2,8 +2,8 @@ ## Rule Details -DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain. -This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result. +DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain. +This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result. Given the screen component does not expose utility methods such as `rerender()` or the `container` property, it is correct to use the `render` response in those scenarios. Examples of **incorrect** code for this rule: @@ -43,6 +43,17 @@ getByText('foo'); // calling a method directly from a variable created by within const myWithinVariable = within(screen.getByText('foo')); myWithinVariable.getByText('foo'); + +// accessing the container and the base element +const utils = render(baz); +utils.container.querySelector('foo'); +utils.baseElement.querySelector('foo'); + +// calling the utilities function +const utils = render(); +utils.rerender(); +utils.unmount(); +utils.asFragment(); ``` ## Further Reading diff --git a/tests/lib/rules/prefer-screen-queries.test.ts b/tests/lib/rules/prefer-screen-queries.test.ts index ea19b80e..2de35a37 100644 --- a/tests/lib/rules/prefer-screen-queries.test.ts +++ b/tests/lib/rules/prefer-screen-queries.test.ts @@ -33,6 +33,36 @@ ruleTester.run(RULE_NAME, rule, { myWithinVariable.${queryMethod}('baz') `, })), + { + code: ` + const screen = render(baz); + screen.container.querySelector('foo'); + ` + }, + { + code: ` + const screen = render(baz); + screen.baseElement.querySelector('foo'); + ` + }, + { + code: ` + const utils = render(baz); + screen.rerender(); + ` + }, + { + code: ` + const utils = render(baz); + utils.unmount(); + ` + }, + { + code: ` + const utils = render(baz); + utils.asFragment(); + ` + } ], invalid: [