From fa7d5c053604c6745fa8d71cef35549abc37a57b Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Fri, 26 Nov 2021 00:56:47 -0800 Subject: [PATCH] docs: start documenting differences and queries --- docs/differences.md | 56 +++++++++++++++++++++++++++++++++++++++++++++ docs/queries.md | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 docs/differences.md create mode 100644 docs/queries.md diff --git a/docs/differences.md b/docs/differences.md new file mode 100644 index 0000000..b6e2ea8 --- /dev/null +++ b/docs/differences.md @@ -0,0 +1,56 @@ +# Differences Between `@testing-library/dom` and `cli-testing-library` + +While we clearly take inspiration from [`DOM Testing Library`](https://github.com/testing-library/dom-testing-library) +and [`React Testing Library`](https://github.com/testing-library/react-testing-library), +and try to do our best to align as much as possible, there are some major distinctions between +the project's APIs. + + + + + +- [Instances](#instances) +- [Queries](#queries) +- [Similarities](#similarities) + + + +# Instances + +While the `DOM Testing Library` and it's descendants have a concept of both a `container` and `element`, `CLI Testing Library` +only has a single concept to replace them both: a `TestInstance`. + +This is because, while the DOM makes a clear distinction between different elements (say, [`HTMLBodyElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLBodyElement) +and [`HTMLDivElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDivElement)), there is no such distinction between +processes in the CLI as far as I'm aware. + +While this may change in the future (maybe `container` can be a sort of `TestProcess` of some kind), there are +[unanswered questions around what that API looks like and whether we can meaningfully handle the distinction](https://github.com/crutchcorn/cli-testing-library/issues/2). + +# Queries + +Similarly, `HTMLElement`s provide clean lines between user input items. Meanwhile, the CLI only has the concepts of `stdin`, `stdout`, and `stderr`. +It's unclear how matching a single line of `stdout` would help in a beneficial matter, much less how APIs like `fireEvent` +would utilize this seemingly extraneous metadata. + +As a result, we don't have any `*AllBy` queries that `DOM Testing Library` does. All of our queries are non-plural (`*By`) +and will simply return either the test instance the query was called on, or `null` depending on the query. + +While we would be happy to reconsider, there are once again +[lots of questions around what `*AllBy` queries would like and whether we can meaningfully use that concept](https://github.com/crutchcorn/cli-testing-library/issues/2). + +# Similarities + +This isn't to say that we're not dedicated to being aligned with upstream. We would love to work with the broader Testing Library +community to figure out these problems and adjust our usage. + +This is the primary reason the library is still published as an `@alpha`, despite being stable enough to successfully +power [a popular CLI application](https://github.com/plopjs/plop/)'s testbed. + +What's more, we're dedicated enough to making this happen that we've made sure to: + +- Use the same source code organization (as much as possible) as `DOM Testing Library` and `React Testing Library` +- Use the same deployment process as `DOM Testing Library` +- Use similar documentation styles + +And more! diff --git a/docs/queries.md b/docs/queries.md new file mode 100644 index 0000000..9dd97fa --- /dev/null +++ b/docs/queries.md @@ -0,0 +1,55 @@ +# About Queries + +Queries are the methods that the CLI Testing Library gives you to find processes in the command line. +There are several types of queries ("get", "find", "query"); the difference between them +is whether the query will throw an error if no CLI instance is found or if it will return +a Promise and retry. Depending on what app content you are selecting, different +queries may be more or less appropriate. + +While our APIs [differ slightly](./differences.md) from upstream Testing Library's, +[their "About Queries" page summarizes the goals and intentions of this project's queries quite well](https://testing-library.com/docs/queries/about/). + + + + + +- [Example](#example) +- [Types of Queries](#types-of-queries) + + + +# Example + +```javascript +import {render} from 'cli-testing-library' + +test('should show login form', () => { + const {getByText} = render('ftp-automation.sh') + const input = getByText('Username') + // Events and assertions... +}) +``` + +# Types of Queries + +- `getBy...`: Returns the matching CLI instance for a query, and throw a descriptive + error if no instances match. +- `queryBy...`: Returns the matching CLI instance for a query, and return `null` if no + instances match. This is useful for asserting an instance that is not present. +- `findBy...`: Returns a Promise which resolves when a CLI instance is found which + matches the given query. The promise is rejected if no instance is found after a + default timeout of 1000ms. + +
+ +Summary Table + +
+ +| Type of Query | 0 Matches | 1 Match | >1 Matches | Retry (Async/Await) | +| --------------------- | ------------- | -------------- | ------------ | :-----------------: | +| `getBy...` | Throw error | Return element | Throw error | No | +| `queryBy...` | Return `null` | Return element | Throw error | No | +| `findBy...` | Throw error | Return element | Throw error | Yes | + +
-- knotmirror 0.0.1