@ember-joinery/test-support 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +574 -0
- package/LICENSE.md +9 -0
- package/NOTICE +532 -0
- package/README.md +356 -2
- package/addon-main.cjs +4 -0
- package/dist/-private/checkable.d.ts +21 -0
- package/dist/-private/checkable.d.ts.map +1 -0
- package/dist/-private/checkable.js +80 -0
- package/dist/-private/control.d.ts +18 -0
- package/dist/-private/control.d.ts.map +1 -0
- package/dist/-private/control.js +55 -0
- package/dist/-private/menu.d.ts +145 -0
- package/dist/-private/menu.d.ts.map +1 -0
- package/dist/-private/menu.js +322 -0
- package/dist/-private/overlay.d.ts +9 -0
- package/dist/-private/overlay.d.ts.map +1 -0
- package/dist/-private/overlay.js +39 -0
- package/dist/-private/showing.d.ts +7 -0
- package/dist/-private/showing.d.ts.map +1 -0
- package/dist/-private/showing.js +10 -0
- package/dist/-private/size.d.ts +10 -0
- package/dist/-private/size.d.ts.map +1 -0
- package/dist/-private/size.js +24 -0
- package/dist/-private/tree.d.ts +34 -0
- package/dist/-private/tree.d.ts.map +1 -0
- package/dist/-private/tree.js +80 -0
- package/dist/accordion.d.ts +62 -0
- package/dist/accordion.d.ts.map +1 -0
- package/dist/accordion.js +112 -0
- package/dist/alert-dialog.d.ts +24 -0
- package/dist/alert-dialog.d.ts.map +1 -0
- package/dist/alert-dialog.js +37 -0
- package/dist/alert.d.ts +27 -0
- package/dist/alert.d.ts.map +1 -0
- package/dist/alert.js +43 -0
- package/dist/app-shell.d.ts +43 -0
- package/dist/app-shell.d.ts.map +1 -0
- package/dist/app-shell.js +78 -0
- package/dist/aspect-ratio.d.ts +21 -0
- package/dist/aspect-ratio.d.ts.map +1 -0
- package/dist/aspect-ratio.js +30 -0
- package/dist/attachment.d.ts +86 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +156 -0
- package/dist/avatar.d.ts +65 -0
- package/dist/avatar.d.ts.map +1 -0
- package/dist/avatar.js +106 -0
- package/dist/badge.d.ts +29 -0
- package/dist/badge.d.ts.map +1 -0
- package/dist/badge.js +49 -0
- package/dist/breadcrumb.d.ts +36 -0
- package/dist/breadcrumb.d.ts.map +1 -0
- package/dist/breadcrumb.js +80 -0
- package/dist/bubble.d.ts +56 -0
- package/dist/bubble.d.ts.map +1 -0
- package/dist/bubble.js +94 -0
- package/dist/button-group.d.ts +34 -0
- package/dist/button-group.d.ts.map +1 -0
- package/dist/button-group.js +62 -0
- package/dist/button.d.ts +37 -0
- package/dist/button.d.ts.map +1 -0
- package/dist/button.js +77 -0
- package/dist/calendar.d.ts +115 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +276 -0
- package/dist/card.d.ts +31 -0
- package/dist/card.d.ts.map +1 -0
- package/dist/card.js +49 -0
- package/dist/checkbox.d.ts +46 -0
- package/dist/checkbox.d.ts.map +1 -0
- package/dist/checkbox.js +84 -0
- package/dist/child.d.ts +29 -0
- package/dist/child.d.ts.map +1 -0
- package/dist/child.js +27 -0
- package/dist/collapsible.d.ts +44 -0
- package/dist/collapsible.d.ts.map +1 -0
- package/dist/collapsible.js +83 -0
- package/dist/color-scheme.d.ts +44 -0
- package/dist/color-scheme.d.ts.map +1 -0
- package/dist/color-scheme.js +123 -0
- package/dist/combobox.d.ts +114 -0
- package/dist/combobox.d.ts.map +1 -0
- package/dist/combobox.js +293 -0
- package/dist/command.d.ts +92 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +183 -0
- package/dist/context-menu.d.ts +37 -0
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/context-menu.js +59 -0
- package/dist/date-picker.d.ts +48 -0
- package/dist/date-picker.d.ts.map +1 -0
- package/dist/date-picker.js +136 -0
- package/dist/dialog.d.ts +64 -0
- package/dist/dialog.d.ts.map +1 -0
- package/dist/dialog.js +142 -0
- package/dist/drawer.d.ts +63 -0
- package/dist/drawer.d.ts.map +1 -0
- package/dist/drawer.js +136 -0
- package/dist/dropdown-menu.d.ts +35 -0
- package/dist/dropdown-menu.d.ts.map +1 -0
- package/dist/dropdown-menu.js +49 -0
- package/dist/empty.d.ts +29 -0
- package/dist/empty.d.ts.map +1 -0
- package/dist/empty.js +47 -0
- package/dist/field.d.ts +86 -0
- package/dist/field.d.ts.map +1 -0
- package/dist/field.js +150 -0
- package/dist/file-drop.d.ts +74 -0
- package/dist/file-drop.d.ts.map +1 -0
- package/dist/file-drop.js +137 -0
- package/dist/focus.d.ts +54 -0
- package/dist/focus.d.ts.map +1 -0
- package/dist/focus.js +174 -0
- package/dist/harness.d.ts +90 -0
- package/dist/harness.d.ts.map +1 -0
- package/dist/harness.js +189 -0
- package/dist/heading.d.ts +16 -0
- package/dist/heading.d.ts.map +1 -0
- package/dist/heading.js +19 -0
- package/dist/hero.d.ts +21 -0
- package/dist/hero.d.ts.map +1 -0
- package/dist/hero.js +28 -0
- package/dist/hover-card.d.ts +46 -0
- package/dist/hover-card.d.ts.map +1 -0
- package/dist/hover-card.js +85 -0
- package/dist/icon.d.ts +10 -0
- package/dist/icon.d.ts.map +1 -0
- package/dist/icon.js +13 -0
- package/dist/index.d.ts +98 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/input-group.d.ts +55 -0
- package/dist/input-group.d.ts.map +1 -0
- package/dist/input-group.js +103 -0
- package/dist/input-otp.d.ts +103 -0
- package/dist/input-otp.d.ts.map +1 -0
- package/dist/input-otp.js +293 -0
- package/dist/input.d.ts +53 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +90 -0
- package/dist/inspector.d.ts +47 -0
- package/dist/inspector.d.ts.map +1 -0
- package/dist/inspector.js +97 -0
- package/dist/item.d.ts +63 -0
- package/dist/item.d.ts.map +1 -0
- package/dist/item.js +104 -0
- package/dist/kbd.d.ts +28 -0
- package/dist/kbd.d.ts.map +1 -0
- package/dist/kbd.js +36 -0
- package/dist/keys.d.ts +20 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +32 -0
- package/dist/label.d.ts +24 -0
- package/dist/label.d.ts.map +1 -0
- package/dist/label.js +39 -0
- package/dist/layers.d.ts +42 -0
- package/dist/layers.d.ts.map +1 -0
- package/dist/layers.js +70 -0
- package/dist/link.d.ts +44 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +60 -0
- package/dist/marker.d.ts +42 -0
- package/dist/marker.d.ts.map +1 -0
- package/dist/marker.js +73 -0
- package/dist/menubar.d.ts +76 -0
- package/dist/menubar.d.ts.map +1 -0
- package/dist/menubar.js +139 -0
- package/dist/message-scroller.d.ts +101 -0
- package/dist/message-scroller.d.ts.map +1 -0
- package/dist/message-scroller.js +215 -0
- package/dist/message.d.ts +48 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +72 -0
- package/dist/native-form.d.ts +44 -0
- package/dist/native-form.d.ts.map +1 -0
- package/dist/native-form.js +111 -0
- package/dist/native-select.d.ts +56 -0
- package/dist/native-select.d.ts.map +1 -0
- package/dist/native-select.js +100 -0
- package/dist/navigation-bar.d.ts +30 -0
- package/dist/navigation-bar.d.ts.map +1 -0
- package/dist/navigation-bar.js +52 -0
- package/dist/navigation-menu.d.ts +97 -0
- package/dist/navigation-menu.d.ts.map +1 -0
- package/dist/navigation-menu.js +207 -0
- package/dist/navigation-stack.d.ts +36 -0
- package/dist/navigation-stack.d.ts.map +1 -0
- package/dist/navigation-stack.js +67 -0
- package/dist/observers.d.ts +21 -0
- package/dist/observers.d.ts.map +1 -0
- package/dist/observers.js +34 -0
- package/dist/page.d.ts +38 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/page.js +68 -0
- package/dist/pagination.d.ts +39 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.js +71 -0
- package/dist/pointer.d.ts +38 -0
- package/dist/pointer.d.ts.map +1 -0
- package/dist/pointer.js +82 -0
- package/dist/popover.d.ts +50 -0
- package/dist/popover.d.ts.map +1 -0
- package/dist/popover.js +90 -0
- package/dist/progress.d.ts +38 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +81 -0
- package/dist/pull-to-refresh.d.ts +40 -0
- package/dist/pull-to-refresh.d.ts.map +1 -0
- package/dist/pull-to-refresh.js +65 -0
- package/dist/questionnaire.d.ts +117 -0
- package/dist/questionnaire.d.ts.map +1 -0
- package/dist/questionnaire.js +240 -0
- package/dist/radio-group.d.ts +53 -0
- package/dist/radio-group.d.ts.map +1 -0
- package/dist/radio-group.js +95 -0
- package/dist/rating.d.ts +67 -0
- package/dist/rating.d.ts.map +1 -0
- package/dist/rating.js +160 -0
- package/dist/resizable.d.ts +76 -0
- package/dist/resizable.d.ts.map +1 -0
- package/dist/resizable.js +138 -0
- package/dist/routing.d.ts +36 -0
- package/dist/routing.d.ts.map +1 -0
- package/dist/routing.js +50 -0
- package/dist/roving-focus.d.ts +38 -0
- package/dist/roving-focus.d.ts.map +1 -0
- package/dist/roving-focus.js +60 -0
- package/dist/scroll-area.d.ts +58 -0
- package/dist/scroll-area.d.ts.map +1 -0
- package/dist/scroll-area.js +153 -0
- package/dist/scroller.d.ts +41 -0
- package/dist/scroller.d.ts.map +1 -0
- package/dist/scroller.js +65 -0
- package/dist/select.d.ts +98 -0
- package/dist/select.d.ts.map +1 -0
- package/dist/select.js +203 -0
- package/dist/selectors.d.ts +44 -0
- package/dist/selectors.d.ts.map +1 -0
- package/dist/selectors.js +63 -0
- package/dist/send-pointer.d.ts +17 -0
- package/dist/send-pointer.d.ts.map +1 -0
- package/dist/send-pointer.js +35 -0
- package/dist/separator.d.ts +26 -0
- package/dist/separator.d.ts.map +1 -0
- package/dist/separator.js +40 -0
- package/dist/shadowed.d.ts +55 -0
- package/dist/shadowed.d.ts.map +1 -0
- package/dist/shadowed.js +97 -0
- package/dist/sheet.d.ts +14 -0
- package/dist/sheet.d.ts.map +1 -0
- package/dist/sheet.js +15 -0
- package/dist/sidebar.d.ts +159 -0
- package/dist/sidebar.d.ts.map +1 -0
- package/dist/sidebar.js +299 -0
- package/dist/skeleton.d.ts +18 -0
- package/dist/skeleton.d.ts.map +1 -0
- package/dist/skeleton.js +25 -0
- package/dist/slider.d.ts +66 -0
- package/dist/slider.d.ts.map +1 -0
- package/dist/slider.js +155 -0
- package/dist/spinner.d.ts +10 -0
- package/dist/spinner.d.ts.map +1 -0
- package/dist/spinner.js +13 -0
- package/dist/split-view.d.ts +77 -0
- package/dist/split-view.d.ts.map +1 -0
- package/dist/split-view.js +145 -0
- package/dist/status-bar.d.ts +25 -0
- package/dist/status-bar.d.ts.map +1 -0
- package/dist/status-bar.js +42 -0
- package/dist/sticky-footer.d.ts +33 -0
- package/dist/sticky-footer.d.ts.map +1 -0
- package/dist/sticky-footer.js +65 -0
- package/dist/switch.d.ts +44 -0
- package/dist/switch.d.ts.map +1 -0
- package/dist/switch.js +77 -0
- package/dist/tab-bar.d.ts +38 -0
- package/dist/tab-bar.d.ts.map +1 -0
- package/dist/tab-bar.js +78 -0
- package/dist/table.d.ts +41 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +100 -0
- package/dist/tabs.d.ts +66 -0
- package/dist/tabs.d.ts.map +1 -0
- package/dist/tabs.js +149 -0
- package/dist/test-hooks.d.ts +9 -0
- package/dist/test-hooks.d.ts.map +1 -0
- package/dist/test-hooks.js +1 -0
- package/dist/textarea.d.ts +47 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +78 -0
- package/dist/title-bar.d.ts +43 -0
- package/dist/title-bar.d.ts.map +1 -0
- package/dist/title-bar.js +74 -0
- package/dist/toaster.d.ts +172 -0
- package/dist/toaster.d.ts.map +1 -0
- package/dist/toaster.js +307 -0
- package/dist/toggle-group.d.ts +51 -0
- package/dist/toggle-group.d.ts.map +1 -0
- package/dist/toggle-group.js +97 -0
- package/dist/toggle.d.ts +21 -0
- package/dist/toggle.d.ts.map +1 -0
- package/dist/toggle.js +26 -0
- package/dist/toolbar.d.ts +40 -0
- package/dist/toolbar.d.ts.map +1 -0
- package/dist/toolbar.js +67 -0
- package/dist/tooltip.d.ts +48 -0
- package/dist/tooltip.d.ts.map +1 -0
- package/dist/tooltip.js +91 -0
- package/dist/trace.d.ts +118 -0
- package/dist/trace.d.ts.map +1 -0
- package/dist/trace.js +196 -0
- package/dist/translator.d.ts +34 -0
- package/dist/translator.d.ts.map +1 -0
- package/dist/translator.js +75 -0
- package/dist/window-frame.d.ts +40 -0
- package/dist/window-frame.d.ts.map +1 -0
- package/dist/window-frame.js +66 -0
- package/dist/zoetrope.d.ts +51 -0
- package/dist/zoetrope.d.ts.map +1 -0
- package/dist/zoetrope.js +94 -0
- package/package.json +68 -3
package/README.md
CHANGED
|
@@ -1,3 +1,357 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ember-joinery/test-support
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A test harness for every [Joinery](https://joegaudet.com/ember-joinery/) primitive and
|
|
4
|
+
component. Harnesses find components by their `data-test-jn-*` attributes, read state through
|
|
5
|
+
ARIA, and drive them the way a user does, with `@ember/test-helpers` (QUnit or Vitest).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @ember-joinery/test-support
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Full documentation: the Testing page of the guide at **https://joegaudet.com/ember-joinery/guide/**.
|
|
12
|
+
|
|
13
|
+
## Harnesses
|
|
14
|
+
|
|
15
|
+
A harness drives one instance of a component. Make one with the component's `@name`, or with
|
|
16
|
+
nothing for the first instance in the test:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const dialog = new DialogHarness('edit-profile') // <Dialog @name="edit-profile">
|
|
20
|
+
const first = new ButtonHarness() // the first Button in the test
|
|
21
|
+
const third = new ButtonHarness({ index: 2 }) // by position
|
|
22
|
+
const save = new ButtonHarness({ name: 'save', within: dialog }) // inside another harness
|
|
23
|
+
const all = ButtonHarness.findAll() // one harness per Button in the test
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
A harness looks its component up every time you read it, so you can make it before the
|
|
27
|
+
component renders, and it stays valid across re-renders. `harness.element` is the component's
|
|
28
|
+
root and throws when it isn't rendered; `harness.exists` says whether it is, without throwing.
|
|
29
|
+
|
|
30
|
+
## Page objects
|
|
31
|
+
|
|
32
|
+
Describe a part of your app once, as a harness subclass. `@child` declares the components
|
|
33
|
+
inside it, and `static named` the `@name` it finds by default:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { DialogHarness, ButtonHarness, InputHarness, child } from '@ember-joinery/test-support'
|
|
37
|
+
|
|
38
|
+
class EditProfile extends DialogHarness {
|
|
39
|
+
static named = 'edit-profile'
|
|
40
|
+
|
|
41
|
+
@child(InputHarness, 'name') declare name: InputHarness // <Input @name="name">
|
|
42
|
+
@child(ButtonHarness, 'save') declare save: ButtonHarness
|
|
43
|
+
@child(ButtonHarness, { index: 1 }) declare second: ButtonHarness // by position
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const dialog = new EditProfile()
|
|
47
|
+
await dialog.open()
|
|
48
|
+
await dialog.name.fillIn('Ada')
|
|
49
|
+
await dialog.save.click()
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Each child is created the first time you read it and kept: `dialog.save` is the same object
|
|
53
|
+
for the life of `dialog`, and `dialog.child(ButtonHarness, 'save')` returns that object too.
|
|
54
|
+
Without a name, a child is the class's `static named`, or else the first instance. A child's
|
|
55
|
+
lookup is scoped to its parent: the parent's root, and every part the parent owns wherever
|
|
56
|
+
that part is rendered (see [Portals and the top layer](#portals-and-the-top-layer)). Children
|
|
57
|
+
are found at any depth, through other components in between.
|
|
58
|
+
|
|
59
|
+
With Ember's legacy decorators, write `declare` (TypeScript) or a bare field (JavaScript):
|
|
60
|
+
`@child(ButtonHarness, 'save') save;`. With standard decorators, write `accessor`:
|
|
61
|
+
`@child(ButtonHarness, 'save') accessor save!: ButtonHarness`.
|
|
62
|
+
|
|
63
|
+
For a one-off lookup, `find()` makes a new harness on each call, and `findAll()` makes one for
|
|
64
|
+
each instance:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
await dialog.find(ButtonHarness, 'cancel').click()
|
|
68
|
+
dialog.findAll(ButtonHarness).length // the buttons inside the dialog
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Writing a component harness
|
|
72
|
+
|
|
73
|
+
Extend `Harness`, name the component it drives in `static component` (its `data-test-jn`
|
|
74
|
+
value), read state through ARIA, and drive it the way a user does:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { click } from '@ember/test-helpers'
|
|
78
|
+
import { Harness } from '@ember-joinery/test-support'
|
|
79
|
+
|
|
80
|
+
export class DisclosureHarness extends Harness {
|
|
81
|
+
static component = 'disclosure'
|
|
82
|
+
|
|
83
|
+
get trigger(): HTMLButtonElement {
|
|
84
|
+
return this.part<HTMLButtonElement>('trigger')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get content(): HTMLElement {
|
|
88
|
+
return this.part('content')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get isOpen(): boolean {
|
|
92
|
+
return this.trigger.getAttribute('aria-expanded') === 'true'
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async toggle(): Promise<void> {
|
|
96
|
+
await click(this.trigger)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
A harness pointed at another component fails with a message that says so:
|
|
102
|
+
`the component named "more" is a "menu"; this harness drives "disclosure"`. A harness that
|
|
103
|
+
drives several components lists them all (`static component = ['sheet', 'dialog']`); the base
|
|
104
|
+
`Harness` drives any component. Keep the constructor's signature in subclasses: `@child`,
|
|
105
|
+
`find()` and `findAll()` construct them.
|
|
106
|
+
|
|
107
|
+
What `Harness` gives a subclass:
|
|
108
|
+
|
|
109
|
+
| Member | What it is |
|
|
110
|
+
| -------------------------- | ---------------------------------------------------------------------------------------------- |
|
|
111
|
+
| `element` | The component's root, looked up on every read. Throws when it isn't rendered |
|
|
112
|
+
| `exists` | Whether the component is rendered now |
|
|
113
|
+
| `part(name, value?)` | One of the component's parts, or the one with that `data-test-jn-value`. Throws when it's none |
|
|
114
|
+
| `queryPart(name, value?)` | The same, or `null` |
|
|
115
|
+
| `parts(name)` | All of the component's parts with that name, in document order |
|
|
116
|
+
| `values(name)` | The `data-test-jn-value` of each of those parts |
|
|
117
|
+
| `child(Class, options?)` | A child harness, created once and kept (what `@child` uses) |
|
|
118
|
+
| `find(Class, options?)` | A one-off child harness |
|
|
119
|
+
| `findAll(Class, options?)` | A harness for every instance of `Class` inside this one |
|
|
120
|
+
| `Class.findAll(options?)` | A harness for every instance in the test |
|
|
121
|
+
|
|
122
|
+
`options` is a `@name`, or `{ name, index }`. A part belongs to the nearest component above it,
|
|
123
|
+
so a component nested inside another keeps its own parts: an accordion's `parts('item')` leaves
|
|
124
|
+
out the items of an accordion inside it. A component's root is never its own part.
|
|
125
|
+
|
|
126
|
+
A component that wraps your content, such as a `Scroller`, a layout or a `Heading`, marks its
|
|
127
|
+
root `data-test-jn-wrapper`. Its harness finds it, and scopes `within`, `find()` and `@child`
|
|
128
|
+
to it, as any harness does, but it has no parts of its own: a part inside a wrapper belongs to
|
|
129
|
+
the component around the wrapper, so wrapping a dialog's body in a `Scroller` leaves the
|
|
130
|
+
dialog's parts the dialog's. A wrapper can be a part itself (`<Heading data-test-jn-part="title">`).
|
|
131
|
+
|
|
132
|
+
## Portals and the top layer
|
|
133
|
+
|
|
134
|
+
Some parts render outside their component's root: a dialog's content in the top layer, a
|
|
135
|
+
popover's content portalled to the end of `<body>`. Those parts carry `data-test-jn-owner`, the
|
|
136
|
+
`id` of the root they belong to, and the harnesses follow it, so `dialog.part('content')` and
|
|
137
|
+
any child inside the content are found wherever the content is rendered, at any depth of
|
|
138
|
+
nesting.
|
|
139
|
+
|
|
140
|
+
## Layers, anchoring and dismissal
|
|
141
|
+
|
|
142
|
+
Overlays sit in the top layer and close on Escape or a press outside, through the dismiss stack
|
|
143
|
+
in `@ember-joinery/primitives/dismiss`. These drive them as a user does:
|
|
144
|
+
|
|
145
|
+
| Helper | What it does |
|
|
146
|
+
| ------------------------------ | ------------------------------------------------------------------------------------- |
|
|
147
|
+
| `pressEscape(target?)` | Presses Escape where focus is, or on `target`. Only the top layer closes |
|
|
148
|
+
| `press(element, options?)` | Presses an element as a pointer does: `pointerdown`, then `click()`, then `pointerup` |
|
|
149
|
+
| `pressOutside(target?, opts?)` | `press()` outside every layer: on `target`, or the test's root element |
|
|
150
|
+
| `isInTopLayer(element)` | Whether it is an open popover or a modal `<dialog>` |
|
|
151
|
+
| `placementOf(element)` | `{ side, align }`, where an anchored element ended up, from `data-side`/`data-align` |
|
|
152
|
+
| `setupAnchorFallback(hooks)` | Forces the Floating UI fallback for a module's tests, as CI's fallback run does |
|
|
153
|
+
| `hover(element, options?)` | Rests the pointer on an element, as a mouse does, and waits for a delay it sets off |
|
|
154
|
+
| `unhover(element, options?)` | Moves the pointer off an element (`away` from another), and waits as `hover()` does |
|
|
155
|
+
| `setupTranslator(hooks, t)` | Registers a translator as `translator:main` for a module's tests |
|
|
156
|
+
| `pseudoTranslator` | A pseudo-locale translator, `⟦Ćĺóśé⟧`, that shows a string that skipped translation |
|
|
157
|
+
|
|
158
|
+
`click()` sends no pointer events, and the dismiss stack listens for `pointerdown`, as Radix's
|
|
159
|
+
does, so use `press()` and `pressOutside()` for presses that should dismiss something.
|
|
160
|
+
`{ pointerType: 'touch' }` presses as a finger does, which dismisses on the click that follows.
|
|
161
|
+
|
|
162
|
+
## Selectors
|
|
163
|
+
|
|
164
|
+
Every Joinery component renders these attributes. They are public API: the harnesses use them,
|
|
165
|
+
and so can you.
|
|
166
|
+
|
|
167
|
+
| Attribute | On |
|
|
168
|
+
| -------------------------------- | ------------------------------------------------------------------------------------- |
|
|
169
|
+
| `data-test-jn="<component>"` | The root of each component instance: `dialog`, `button`, `select`… |
|
|
170
|
+
| `data-test-jn-name="<@name>"` | The same root, when the component was given `@name` |
|
|
171
|
+
| `data-test-jn-part="<part>"` | Each part of a component: `trigger`, `content`, `item`, `option`… |
|
|
172
|
+
| `data-test-jn-owner="<root id>"` | A part rendered outside its root (top layer or portal): the `id` of the root it's for |
|
|
173
|
+
| `data-test-jn-value="<key>"` | An option, item, tab or toggle, by its key |
|
|
174
|
+
| `data-test-jn-wrapper` | A root that wraps your content: the parts inside belong to the component around it |
|
|
175
|
+
|
|
176
|
+
`selectors` builds CSS selectors for them, escaping the value, and `attributes` has their names:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { selectors, attributes } from '@ember-joinery/test-support'
|
|
180
|
+
|
|
181
|
+
selectors.component('dialog', 'edit-profile') // '[data-test-jn="dialog"][data-test-jn-name="edit-profile"]'
|
|
182
|
+
selectors.name('save') // '[data-test-jn-name="save"]'
|
|
183
|
+
selectors.part('trigger') // '[data-test-jn-part="trigger"]'
|
|
184
|
+
selectors.owner('ember12') // '[data-test-jn-owner="ember12"]'
|
|
185
|
+
selectors.value('apple') // '[data-test-jn-value="apple"]'
|
|
186
|
+
selectors.part() // '[data-test-jn-part]', any part
|
|
187
|
+
selectors.wrapper() // '[data-test-jn-wrapper]'
|
|
188
|
+
|
|
189
|
+
attributes.owner // 'data-test-jn-owner'
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## The color scheme
|
|
193
|
+
|
|
194
|
+
Each test gets its own `ColorScheme` (from `@ember-joinery/primitives/color-scheme`), but not
|
|
195
|
+
its own operating system or `localStorage`. `setupColorScheme(hooks)` isolates both: before
|
|
196
|
+
each test it clears the saved choice and makes `matchMedia('(prefers-color-scheme: …)')`
|
|
197
|
+
answer from an emulated system preference; after each, it puts both back.
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { setupColorScheme } from '@ember-joinery/test-support'
|
|
201
|
+
|
|
202
|
+
module('dark mode', function (hooks) {
|
|
203
|
+
setupRenderingTest(hooks)
|
|
204
|
+
const system = setupColorScheme(hooks, { system: 'light' })
|
|
205
|
+
|
|
206
|
+
test('it follows the system', async function (assert) {
|
|
207
|
+
await render(<template><ModeToggle /></template>)
|
|
208
|
+
system.setSystem('dark') // as switching the OS to dark mode would
|
|
209
|
+
await settled()
|
|
210
|
+
assert.dom(document.documentElement).hasClass('dark')
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`system.fromAnotherTab('dark')` saves a choice and sends the `storage` event, as another tab
|
|
216
|
+
would (`null` clears it), and `system.stored` is the saved choice. Pass `storageKey` when the
|
|
217
|
+
app configures its own.
|
|
218
|
+
|
|
219
|
+
## Toasts
|
|
220
|
+
|
|
221
|
+
`toast()` is called from anywhere, so it finds its app through the rendered `<Toaster>`.
|
|
222
|
+
`setupToaster(hooks)` points it at each test's owner from the start of the test (through
|
|
223
|
+
`globalThis.JOINERY_TOAST_OWNER`), so a toast made before a Toaster renders goes to the test,
|
|
224
|
+
and never to the next one. The toasts are the owner's, so they go with it. `ToasterHarness`
|
|
225
|
+
drives the Toaster and `ToastHarness` one toast:
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
import { ToasterHarness, setupToaster } from '@ember-joinery/test-support'
|
|
229
|
+
|
|
230
|
+
module('saving', function (hooks) {
|
|
231
|
+
setupRenderingTest(hooks)
|
|
232
|
+
setupToaster(hooks)
|
|
233
|
+
|
|
234
|
+
test('it says so', async function (assert) {
|
|
235
|
+
await render(<template><SaveButton /><Toaster /></template>)
|
|
236
|
+
await click('[data-test-save]')
|
|
237
|
+
const toast = new ToasterHarness().toast() // the newest
|
|
238
|
+
assert.strictEqual(toast.title, 'Saved')
|
|
239
|
+
await toast.clickAction()
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Keyboard and focus
|
|
245
|
+
|
|
246
|
+
A test can't press the real Tab key: `triggerKeyEvent(element, 'keydown', 'Tab')` sends the
|
|
247
|
+
event, but focus stays where it is. `tab()` does what the browser would: the focused element
|
|
248
|
+
gets the `keydown`, and unless the page prevents it, focus moves to the next element in the
|
|
249
|
+
tab order. That is how a test checks a focus trap or a roving tab stop.
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { pressKey, tab, tabOrder, typeKeys } from '@ember-joinery/test-support'
|
|
253
|
+
|
|
254
|
+
await tab() // to the next element in the tab order
|
|
255
|
+
await tab({ shift: true }) // to the previous one
|
|
256
|
+
tabOrder(form) // the elements Tab visits, in order
|
|
257
|
+
await pressKey('ArrowDown') // keydown and keyup on the focused element; true if prevented
|
|
258
|
+
await pressKey('Tab', { shiftKey: true })
|
|
259
|
+
await typeKeys('ba') // one key press per character, for typeahead
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
`tab()` follows a browser's rules: positive tabindexes first (lowest first), then document
|
|
263
|
+
order; one stop per group of native radio buttons (the checked one); nothing hidden, inert or
|
|
264
|
+
disabled. Past the last element, focus leaves the page, so nothing is focused afterwards. The
|
|
265
|
+
test runner's own controls (QUnit's toolbar, testem's frame) aren't in the order. `pressKey` and
|
|
266
|
+
`typeKeys` take `KeyboardEvent#key` values (`'a'`, `' '`, `'Home'`) and send key events only:
|
|
267
|
+
they don't type into inputs or press buttons, which `typeIn` and `click` do.
|
|
268
|
+
|
|
269
|
+
`RovingFocusGroupHarness` drives a `<RovingFocusGroup>` from
|
|
270
|
+
`@ember-joinery/primitives/roving-focus`: `items` and `ids`, `item(id)`, the `focused` item,
|
|
271
|
+
the `tabStop` Tab comes back to, `focus(id)` and `press(key)`.
|
|
272
|
+
|
|
273
|
+
## Routing
|
|
274
|
+
|
|
275
|
+
`setupRouting(owner, map, options)` gives a test its own routes, without scaffolding them in
|
|
276
|
+
the app: it replaces the app's router with one that has `map`'s routes (and `rootURL`), starts
|
|
277
|
+
it, and returns the router service. Call it before `visit()` or `render()`. `getRouter(owner)` is
|
|
278
|
+
the router service, typed.
|
|
279
|
+
|
|
280
|
+
Unlike ember-primitives' `setupRouting`, it doesn't decorate the test's router with
|
|
281
|
+
`properLinks`, since this package doesn't depend on `@ember-joinery/primitives`. For plain
|
|
282
|
+
`<a href>` links to transition, call `setupProperLinks(this.owner)` too, as below.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import { click, currentURL, visit } from '@ember/test-helpers'
|
|
286
|
+
import { setupProperLinks } from '@ember-joinery/primitives/proper-links'
|
|
287
|
+
import { setupRouting } from '@ember-joinery/test-support'
|
|
288
|
+
|
|
289
|
+
test('the nav routes', async function (assert) {
|
|
290
|
+
setupRouting(this.owner, function () {
|
|
291
|
+
this.route('settings', function () {
|
|
292
|
+
this.route('profile')
|
|
293
|
+
})
|
|
294
|
+
})
|
|
295
|
+
setupProperLinks(this.owner) // plain <a href> links transition, as with @properLinks
|
|
296
|
+
this.owner.register('template:application', <template><a href="/settings/profile">Profile</a></template>)
|
|
297
|
+
|
|
298
|
+
await visit('/')
|
|
299
|
+
await click('a')
|
|
300
|
+
assert.strictEqual(currentURL(), '/settings/profile')
|
|
301
|
+
})
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
In a rendering test it starts the router too, so what reads the URL (`qp`, `link`) has one once
|
|
305
|
+
you `transitionTo()` it.
|
|
306
|
+
|
|
307
|
+
## One-time codes
|
|
308
|
+
|
|
309
|
+
`fillOTP(code, within?)` fills an `InputOTP` (styled or headless) with a code, as
|
|
310
|
+
ember-primitives' `fillOTP` does: the only one in the test, or the one inside `within` (a
|
|
311
|
+
selector or an element). `InputOTPHarness` does more: it types, deletes and moves the caret as a
|
|
312
|
+
browser edits the input, and pastes and autofills a code.
|
|
313
|
+
|
|
314
|
+
```ts
|
|
315
|
+
import { InputOTPHarness, fillOTP } from '@ember-joinery/test-support'
|
|
316
|
+
|
|
317
|
+
await fillOTP('123456')
|
|
318
|
+
await fillOTP('123456', '[data-test-login]')
|
|
319
|
+
await new InputOTPHarness().paste('654321')
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Observers
|
|
323
|
+
|
|
324
|
+
`settled()` waits for the first report of an element that Joinery's `viewport` or `onResize`
|
|
325
|
+
observes. The reports a scroll or a resize causes come in a later frame, so
|
|
326
|
+
`waitForObservers()` waits two frames and settles, and `scrollIntoView(target)` scrolls an
|
|
327
|
+
element (or a selector) into view as a user would, then waits:
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
import { scrollIntoView, waitForObservers } from '@ember-joinery/test-support'
|
|
331
|
+
|
|
332
|
+
await scrollIntoView('[data-test-comments]') // what an InViewport holds renders
|
|
333
|
+
box.style.width = '300px'
|
|
334
|
+
await waitForObservers() // and {{onResize}} has heard of it
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Layouts, scrolling and links
|
|
338
|
+
|
|
339
|
+
Harnesses for ember-primitives' components that shadcn has no counterpart for, from
|
|
340
|
+
`@ember-joinery/primitives`:
|
|
341
|
+
|
|
342
|
+
| Harness | Drives | What it has |
|
|
343
|
+
| --------------------- | ---------------- | ------------------------------------------------------------------------------------ |
|
|
344
|
+
| `ScrollerHarness` | `<Scroller>` | `position`, `range`, `isAtTop`/`Bottom`/`Left`/`Right`, `scrollTo()`, `focus()` |
|
|
345
|
+
| `ZoetropeHarness` | `<Zoetrope>` | `items`, `visibleItems`, `canScrollLeft`/`Right`, `scrolled`, `scrollLeft`/`Right()` |
|
|
346
|
+
| `HeroHarness` | `<Hero>` | `size`, `fillsViewport` |
|
|
347
|
+
| `StickyFooterHarness` | `<StickyFooter>` | `content`, `footer`, `overflows`, `isFooterAtBottom`, `scrollToEnd()` |
|
|
348
|
+
| `LinkHarness` | `<Link>` | `href`, `text`, `isCurrent`, `current`, `opensInNewTab`, `isIsolated`, `follow()` |
|
|
349
|
+
| `ExternalLinkHarness` | `<ExternalLink>` | `LinkHarness`'s members |
|
|
350
|
+
| `HeadingHarness` | `<Heading>` | `level`, `text` |
|
|
351
|
+
| `ShadowedHarness` | `<Shadowed>` | `shadowRoot`, `query()`, `queryAll()`, `text` |
|
|
352
|
+
|
|
353
|
+
Harnesses don't look into shadow roots, so `ShadowedHarness` reads what is there, and
|
|
354
|
+
ember-primitives' shadow DOM helpers come too: `findInShadow(root, selector)`,
|
|
355
|
+
`findShadow(root?)`, `findInFirstShadow(selector)` and `hasShadowRoot(element)`. Scrolls the
|
|
356
|
+
Zoetrope and the Scroller start wait for their end, smooth ones included, so `await click()`
|
|
357
|
+
and `await settled()` leave them where they stopped.
|
package/addon-main.cjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** An element's text, whitespace collapsed. */
|
|
2
|
+
export declare function textOf(element: Element): string;
|
|
3
|
+
/**
|
|
4
|
+
* A control's accessible name, as assistive technology reads it: `aria-label`, else the text of
|
|
5
|
+
* the elements `aria-labelledby` names, else its `<label>`s' text (one tied to it by `for`, or
|
|
6
|
+
* one around it), else its own text.
|
|
7
|
+
*/
|
|
8
|
+
export declare function accessibleName(element: HTMLElement): string;
|
|
9
|
+
/**
|
|
10
|
+
* Whether a control can't be used: `disabled`, inside a disabled `<fieldset>` (`:disabled`), or
|
|
11
|
+
* `aria-disabled="true"`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isDisabled(element: HTMLElement): boolean;
|
|
14
|
+
/** Whether `aria-invalid` marks it as invalid: any value but `false` (an empty one is false). */
|
|
15
|
+
export declare function isInvalid(element: HTMLElement): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The hidden native input a checkbox, switch or radio renders beside itself in a form (Radix's
|
|
18
|
+
* "bubble input"), which the form submits, or `null` outside a form.
|
|
19
|
+
*/
|
|
20
|
+
export declare function hiddenInputOf(element: HTMLElement): HTMLInputElement | null;
|
|
21
|
+
//# sourceMappingURL=checkable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkable.d.ts","sourceRoot":"","sources":["../../src/-private/checkable.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,wBAAgB,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAE/C;AAuBD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAoB3D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAIxD;AAED,iGAAiG;AACjG,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAGvD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,gBAAgB,GAAG,IAAI,CAK3E"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** An element's text, whitespace collapsed. */
|
|
2
|
+
export function textOf(element) {
|
|
3
|
+
return (element.textContent ?? '').replace(/\s+/g, ' ').trim();
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A label's text as assistive technology reads it: without the text of the control it wraps (a
|
|
7
|
+
* choice card's own mark), or of anything in it hidden with `aria-hidden="true"` (an icon).
|
|
8
|
+
*/
|
|
9
|
+
function labelText(label, control) {
|
|
10
|
+
const hidden = label.querySelector('[aria-hidden="true"]');
|
|
11
|
+
if (!label.contains(control) && !hidden)
|
|
12
|
+
return textOf(label);
|
|
13
|
+
const copy = label.cloneNode(true);
|
|
14
|
+
if (label.contains(control)) {
|
|
15
|
+
const path = [];
|
|
16
|
+
for (let node = control; node && node !== label; node = node.parentElement) {
|
|
17
|
+
path.unshift([...node.parentElement.children].indexOf(node));
|
|
18
|
+
}
|
|
19
|
+
let inCopy = copy;
|
|
20
|
+
for (const index of path)
|
|
21
|
+
inCopy = inCopy.children[index];
|
|
22
|
+
inCopy.remove();
|
|
23
|
+
}
|
|
24
|
+
for (const element of copy.querySelectorAll('[aria-hidden="true"]'))
|
|
25
|
+
element.remove();
|
|
26
|
+
return textOf(copy);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A control's accessible name, as assistive technology reads it: `aria-label`, else the text of
|
|
30
|
+
* the elements `aria-labelledby` names, else its `<label>`s' text (one tied to it by `for`, or
|
|
31
|
+
* one around it), else its own text.
|
|
32
|
+
*/
|
|
33
|
+
export function accessibleName(element) {
|
|
34
|
+
const label = element.getAttribute('aria-label');
|
|
35
|
+
if (label)
|
|
36
|
+
return label.trim();
|
|
37
|
+
const labelledBy = element.getAttribute('aria-labelledby');
|
|
38
|
+
if (labelledBy) {
|
|
39
|
+
return labelledBy
|
|
40
|
+
.split(/\s+/)
|
|
41
|
+
.map(id => element.ownerDocument.getElementById(id))
|
|
42
|
+
.filter((found) => Boolean(found))
|
|
43
|
+
.map(textOf)
|
|
44
|
+
.join(' ');
|
|
45
|
+
}
|
|
46
|
+
const labels = element.labels;
|
|
47
|
+
if (labels && labels.length) {
|
|
48
|
+
return [...labels]
|
|
49
|
+
.map(found => labelText(found, element))
|
|
50
|
+
.join(' ')
|
|
51
|
+
.trim();
|
|
52
|
+
}
|
|
53
|
+
return textOf(element);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Whether a control can't be used: `disabled`, inside a disabled `<fieldset>` (`:disabled`), or
|
|
57
|
+
* `aria-disabled="true"`.
|
|
58
|
+
*/
|
|
59
|
+
export function isDisabled(element) {
|
|
60
|
+
if ('disabled' in element && element.disabled)
|
|
61
|
+
return true;
|
|
62
|
+
if (element.matches(':disabled'))
|
|
63
|
+
return true;
|
|
64
|
+
return element.getAttribute('aria-disabled') === 'true';
|
|
65
|
+
}
|
|
66
|
+
/** Whether `aria-invalid` marks it as invalid: any value but `false` (an empty one is false). */
|
|
67
|
+
export function isInvalid(element) {
|
|
68
|
+
const value = element.getAttribute('aria-invalid');
|
|
69
|
+
return value !== null && value !== '' && value !== 'false';
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The hidden native input a checkbox, switch or radio renders beside itself in a form (Radix's
|
|
73
|
+
* "bubble input"), which the form submits, or `null` outside a form.
|
|
74
|
+
*/
|
|
75
|
+
export function hiddenInputOf(element) {
|
|
76
|
+
const next = element.nextElementSibling;
|
|
77
|
+
return next instanceof HTMLInputElement && next.getAttribute('aria-hidden') === 'true'
|
|
78
|
+
? next
|
|
79
|
+
: null;
|
|
80
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** A native form control a harness reads: an input, a textarea or a select. */
|
|
2
|
+
export type FormControl = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
3
|
+
/** An element's text, whitespace collapsed. */
|
|
4
|
+
export declare function textOf(element: Element): string;
|
|
5
|
+
/**
|
|
6
|
+
* A control's accessible name, as assistive technology reads it: the text of the elements its
|
|
7
|
+
* `aria-labelledby` names, else its `aria-label`, else its `<label>`s' text (`for`, or one
|
|
8
|
+
* around it), else its `title`, else its `placeholder`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function controlLabel(control: FormControl): string;
|
|
11
|
+
/** The text of the elements a control's `aria-describedby` names, or `''`. */
|
|
12
|
+
export declare function controlDescription(control: FormControl): string;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a control is marked invalid: `aria-invalid` with any value but `false` (an empty
|
|
15
|
+
* `aria-invalid` counts as false, as in ARIA).
|
|
16
|
+
*/
|
|
17
|
+
export declare function isMarkedInvalid(control: Element): boolean;
|
|
18
|
+
//# sourceMappingURL=control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"control.d.ts","sourceRoot":"","sources":["../../src/-private/control.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,MAAM,MAAM,WAAW,GAAG,gBAAgB,GAAG,mBAAmB,GAAG,iBAAiB,CAAA;AAEpF,+CAA+C;AAC/C,wBAAgB,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAE/C;AAqBD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAUzD;AAED,8EAA8E;AAC9E,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAE/D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAGzD"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** An element's text, whitespace collapsed. */
|
|
2
|
+
export function textOf(element) {
|
|
3
|
+
return (element.textContent ?? '').replace(/\s+/g, ' ').trim();
|
|
4
|
+
}
|
|
5
|
+
/** The text of the elements an `aria-labelledby` or `aria-describedby` names, in order. */
|
|
6
|
+
function referenced(element, attribute) {
|
|
7
|
+
const ids = element.getAttribute(attribute);
|
|
8
|
+
if (!ids)
|
|
9
|
+
return undefined;
|
|
10
|
+
return ids
|
|
11
|
+
.split(/\s+/)
|
|
12
|
+
.map(id => element.ownerDocument.getElementById(id))
|
|
13
|
+
.filter((found) => Boolean(found))
|
|
14
|
+
.map(textOf)
|
|
15
|
+
.join(' ');
|
|
16
|
+
}
|
|
17
|
+
/** A `<label>`'s text, leaving out the controls inside it (a label can wrap its control). */
|
|
18
|
+
function labelText(label) {
|
|
19
|
+
const copy = label.cloneNode(true);
|
|
20
|
+
for (const control of copy.querySelectorAll('input, select, textarea, button'))
|
|
21
|
+
control.remove();
|
|
22
|
+
return textOf(copy);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A control's accessible name, as assistive technology reads it: the text of the elements its
|
|
26
|
+
* `aria-labelledby` names, else its `aria-label`, else its `<label>`s' text (`for`, or one
|
|
27
|
+
* around it), else its `title`, else its `placeholder`.
|
|
28
|
+
*/
|
|
29
|
+
export function controlLabel(control) {
|
|
30
|
+
const labelledBy = referenced(control, 'aria-labelledby');
|
|
31
|
+
if (labelledBy)
|
|
32
|
+
return labelledBy;
|
|
33
|
+
const label = control.getAttribute('aria-label')?.trim();
|
|
34
|
+
if (label)
|
|
35
|
+
return label;
|
|
36
|
+
const labels = [...(control.labels ?? [])].map(labelText).filter(Boolean);
|
|
37
|
+
if (labels.length)
|
|
38
|
+
return labels.join(' ');
|
|
39
|
+
const title = control.getAttribute('title')?.trim();
|
|
40
|
+
if (title)
|
|
41
|
+
return title;
|
|
42
|
+
return control.getAttribute('placeholder')?.trim() ?? '';
|
|
43
|
+
}
|
|
44
|
+
/** The text of the elements a control's `aria-describedby` names, or `''`. */
|
|
45
|
+
export function controlDescription(control) {
|
|
46
|
+
return referenced(control, 'aria-describedby') ?? '';
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Whether a control is marked invalid: `aria-invalid` with any value but `false` (an empty
|
|
50
|
+
* `aria-invalid` counts as false, as in ARIA).
|
|
51
|
+
*/
|
|
52
|
+
export function isMarkedInvalid(control) {
|
|
53
|
+
const invalid = control.getAttribute('aria-invalid');
|
|
54
|
+
return invalid !== null && invalid !== '' && invalid !== 'false';
|
|
55
|
+
}
|