You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -327,6 +327,12 @@ Ensures that the element is visible to a user. Currently, the following checks a
327
327
- Element has a size (its [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) has a non-zero width and height)
328
328
- Element's opacity is greater than 0.05 (opacity of parent elements are considered)
329
329
330
+
#### Target size
331
+
332
+
> [The intent of this success criteria is to ensure that target sizes are large enough for users to easily activate them, even if the user is accessing content on a small handheld touch screen device, has limited dexterity, or has trouble activating small targets for other reasons. For instance, mice and similar pointing devices can be hard to use for these users, and a larger target will help them activate the target.](https://www.w3.org/WAI/WCAG21/Understanding/target-size.html)
333
+
334
+
Per the [W3C Web Content Accessibility Guidelines (WCAG) 2.1](https://www.w3.org/TR/WCAG21/#target-size), the element must be at least 44px wide and at least 44px tall to pass the target size check (configurable via `user.targetSize` option in `WithBrowserOpts` or `targetSize` option for `user.click`).
335
+
330
336
## Full Example
331
337
332
338
There is a menu example in the [examples folder](./examples/menu/index.test.ts)
@@ -362,7 +368,9 @@ Call Signatures:
362
368
-`moduleServer`: Module Server options object (all properties are optional). They will be applied to files imported through [`utils.runJS`](#pleasantestutilsrunjscode-string-promisevoid) or [`utils.loadJS`](#pleasantestutilsloadjsjspath-string-promisevoid).
363
369
-`plugins`: Array of Rollup, Vite, or WMR plugins to add.
364
370
-`envVars`: Object with string keys and string values for environment variables to pass in as `import.meta.env.*` / `process.env.*`
365
-
-`esbuild`: [`TransformOptions`](https://esbuild.github.io/api/#transform-api) | `false`: Options to pass to esbuild. Set to false to disable esbuild.
371
+
-`esbuild`: ([`TransformOptions`](https://esbuild.github.io/api/#transform-api) | `false`) Options to pass to esbuild. Set to false to disable esbuild.
372
+
-`user`: User API options object (all properties are optional). They will be applied when calling `user.*` methods.
373
+
-`targetSize`: (`number | boolean`, default `44`): Set the minimum target size for `user.click`. Set to `false` to disable target size checks. This option can also be passed to individual `user.click` calls in the 2nd parameter.
366
374
367
375
You can configure the default options (applied to all tests in current file) by using the `configureDefaults` method. If you want defaults to apply to all files, Create a [test setup file](https://jestjs.io/docs/configuration#setupfilesafterenv-array) and call `configureDefaults` there:
368
376
@@ -374,6 +382,9 @@ configureDefaults({
374
382
moduleServer: {
375
383
/* ... */
376
384
},
385
+
user: {
386
+
/* ... */
387
+
},
377
388
/* ... */
378
389
})
379
390
```
@@ -525,11 +536,13 @@ See the [`PleasantestUtils`](#utilities-api-pleasantestutils) documentation.
525
536
526
537
The user API allows you to perform actions on behalf of the user. If you have used [`user-event`](https://github.com/testing-library/user-event), then this API will feel familiar. This API is exposed via the [`user` property in `PleasantestContext`](#pleasantestcontextuser-pleasantestuser).
Clicks an element, if the element is visible and the center of it is not covered by another element. If the center of the element is covered by another element, an error is thrown. This is a thin wrapper around Puppeteer's [`ElementHandle.click` method](https://pptr.dev/#?product=Puppeteer&version=v13.5.2&show=api-elementhandleclickoptions). The difference is that `PleasantestUser.click` checks that the target element is an element that actually can be clicked before clicking it!
531
542
532
-
**Actionability checks**: It refuses to click elements that are not [**attached**](#attached) or not [**visible**](#visible). You can override the visibility check by passing `{ force: true }`.
543
+
**Actionability checks**: It refuses to click elements that are not [**attached**](#attached), not [**visible**](#visible) or which have too small of a [**target size**](#target-size). You can override the visibility and target size checks by passing `{ force: true }`.
544
+
545
+
The target size check can be disabled or configured by passing the `targetSize` option in the second parameter. Passing `false` disables the check; passing a number sets the minimum width/height of elements (in px).
533
546
534
547
Additionally, it refuses to click an element if there is another element covering it. `{ force: true }` overrides this behavior.
This error is intended to encourage developers and designers to use target sizes that are large enough for users to easily click or touch.
6
+
7
+
An element's **target size** is the size of the clickable/tappable region that activates the element. Having a large-enough target size ensures that users can easily click/tap elements, especially in cases where low-input-precision devices (like touchscreens) are used, and for users who may have difficulty aiming cursors due to fine motor movement challenges.
8
+
9
+
The [W3C's guidance on target size](https://www.w3.org/WAI/WCAG21/Understanding/target-size.html) is that developers should use target sizes that are at least 44px × 44px.
10
+
11
+
## Implementation
12
+
13
+
Pleasantest implements the target size check as a part of [actionability checks](../../README.md#actionability). Target size is checked when `user.click()` is called.
14
+
15
+
Inline elements are not checked, based on the reasoning used [by the W3C](https://www.w3.org/WAI/WCAG21/Understanding/target-size.html#intent).
16
+
17
+
Elements must be at least 44px × 44px in order to pass the check (or whatever the configured target size is).
18
+
19
+
### Configuring the minimum target size
20
+
21
+
Setting the `targetSize` option changes the minimum width/height (in px) used to check elements when `user.click` is called. The `targetSize` option can be passed in several places to control the scope of the change:
[Configure Jest to run a setup file before all tests](https://jestjs.io/docs/configuration#setupfilesafterenv-array) (usually called `jest.setup.ts` or `jest.setup.js`) and add the same `configureDefaults` call there, so it is applied to all tests.
60
+
61
+
## Making the error go away
62
+
63
+
### Approach 1: Increasing the target size
64
+
65
+
Much of the time, increasing the target size is the correct solution to the problem. By doing this, you are creating a more inclusive user experience. Usually, increasing `padding` or setting a `min-width`/`min-height` is the easiest way to ensure an element's target size is large enough.
Pleasantest's target size check can be disabled per-call, per-file, or globally.
74
+
75
+
**On an individual call**:
76
+
77
+
```ts
78
+
awaituser.click(button, { targetSize: false });
79
+
```
80
+
81
+
**For a single test**:
82
+
83
+
```ts
84
+
test(
85
+
'test name',
86
+
withBrowser(
87
+
{
88
+
user: {
89
+
targetSize: false,
90
+
},
91
+
},
92
+
async ({ user }) => {
93
+
awaituser.click(something);
94
+
},
95
+
),
96
+
);
97
+
```
98
+
99
+
**For a test file**:
100
+
101
+
```ts
102
+
import { configureDefaults } from'pleasantest';
103
+
104
+
configureDefaults({
105
+
user: { targetSize: false },
106
+
});
107
+
```
108
+
109
+
**For all test files**
110
+
111
+
[Configure Jest to run a setup file before all tests](https://jestjs.io/docs/configuration#setupfilesafterenv-array) (usually called `jest.setup.ts` or `jest.setup.js`) and add the same `configureDefaults` call there, so it is applied to all tests.
? `configured minimum of ${minSize}px × ${minSize}px`
77
+
: 'W3C recommendation of 44px × 44px: https://www.w3.org/WAI/WCAG21/Understanding/target-size.html'
78
+
}
79
+
${capitalizeText(elDescriptor)} was ${width}px × ${height}px
80
+
${el}${suggestion}
81
+
You can customize this check by setting the targetSize option, more details at https://github.com/cloudfour/pleasantest/blob/${docsVersion}/docs/errors/target-size.md`;
82
+
83
+
if(width<minSize||height<minSize){
84
+
// Custom messaging for inputs that should have labels (e.g. type="radio").
85
+
//
86
+
// Inputs that aren't expected to have labels (e.g. type="submit")
87
+
// are checked by the general element check.
88
+
//
89
+
// MDN <input> docs were referenced
90
+
// and the following were assumed to not have labels:
0 commit comments