|
1 | 1 | # Multiple Elements Support |
2 | 2 |
|
3 | | -All element matchers work with arrays of elements (e.g., `$$()` results). |
4 | | -- **Strict Length Matching**: If you provide an array of expected values, the number of values must match the number of elements found. A failure occurs if the lengths differ. |
5 | | -- **Index-based Matching**: When using an array of expected values, each element is compared to the value at the corresponding index. |
6 | | -- **Single Value Matching**: If you provide a single expected value, it is compared against *every* element in the array. |
7 | | -- **Asymmetric Matchers**: Asymmetric matchers can be used within the expected values array for more matching flexibility. |
8 | | -- If no elements exist, a failure occurs (except with `toBeElementsArrayOfSize`). |
9 | | -- Options like `StringOptions` or `HTMLOptions` apply to the entire array (except `NumberOptions`). |
10 | | -- The assertion passes only if **all** elements match the expected value(s). |
11 | | -- Using `.not` applies the negation to each element (e.g., *all* elements must *not* display). |
| 3 | +Matchers element array support (e.g., `$$()`): |
12 | 4 |
|
13 | | -**Note:** Strict length matching does not apply on `toHaveText` to preserve existing behavior. |
| 5 | +- **Strict Index-based Matching**: If an array of expected values is provided, it must match the elements' count; each value is checked at its index. |
| 6 | +- If a single value is provided, every element is compared to it. |
| 7 | +- Asymmetric matchers (e.g., `expect.stringContaining`) work within expected value arrays. |
| 8 | +- An error is thrown if no elements are found (except with `toBeElementsArrayOfSize`). |
| 9 | +- Options like `StringOptions` or `HTMLOptions` apply to the whole array; `NumberOptions` behaves like any expected provided value. |
| 10 | +- The assertion passes only if **all** elements match. |
| 11 | +- Using `.not` means all elements must **not** match. |
| 12 | + |
| 13 | +**Note:** Strict Index-based matching does not apply to `toHaveText`, since an existing behavior was already in placed. |
14 | 14 |
|
15 | 15 | ## Limitations |
16 | | -- An alternative to using `StringOptions` (like `ignoreCase` or `containing`) for a single expected value is to use RegEx (`/MyExample/i`) or Asymmetric Matchers (`expect.stringContaining('Example')`). |
17 | | -- Passing an array of "containing" values, as previously supported by `toHaveText`, is deprecated and not supported for other matchers. |
| 16 | +- Instead of `StringOptions` for a single expected value, use RegExp or asymmetric matchers. |
| 17 | + - For `ignoreCase` use RegEx (`/MyExample/i`) |
| 18 | + - For `containing` use Asymmetric Matchers (`expect.stringContaining('Example')`) |
| 19 | +- Passing an array of "containing" values is deprecated and not supported outside `toHaveText`. |
18 | 20 |
|
19 | 21 | ## Supported types |
20 | | - |
21 | | -Any of the below element types can be passed to `expect`: |
| 22 | +You can pass any of these element types to `expect`: |
22 | 23 | - `ChainablePromiseArray` (the non-awaited case) |
23 | 24 | - `ElementArray` (the awaited case) |
24 | 25 | - `Element[]` (the filtered case) |
25 | 26 |
|
26 | 27 | ## Alternative |
27 | 28 |
|
28 | | -For better control on each element value assertion, a parametrized test of your assertion framework can be used. |
29 | | -Example with mocha |
| 29 | +For more granular or explicit per-element validation, use a parameterized test of your framework. |
| 30 | +Example in Mocha: |
30 | 31 | ```ts |
31 | 32 | describe('Element at index of `$$`', function () { |
32 | 33 | [ { expectedText: 'one', index: 0 }, |
|
0 commit comments