Skip to content

The agent that grows with you

fix(desktop): a checkbox reports whether it is ticked, not the string "on"

Brooklyn Nicholsonb3f6218aa58f
diff --git a/apps/desktop/src/lib/preview-act/naming.test.ts b/apps/desktop/src/lib/preview-act/naming.test.ts
index 3875062a03d..3c633ce40a0 100644
--- a/apps/desktop/src/lib/preview-act/naming.test.ts
+++ b/apps/desktop/src/lib/preview-act/naming.test.ts
@@ -121,18 +121,29 @@ describe('valueOf', () => {
expect(naming().valueOf(input)).toBe('shoes')
})
- // FIXME(checkbox): a checkbox's `.value` is the string "on" unless the page
- // sets one, and that branch is tested first — so both states read back the
- // same and the agent cannot tell a ticked box from an empty one. Pinned as-is
- // here so the extraction is provably behaviour-preserving; fixed next commit.
- it('reports a checkbox as the value the DOM gives it', () => {
+ // The DOM hands back "on" for an unset checkbox value, so reading the value
+ // first made both states identical to the agent.
+ it('reports a checkbox as its state rather than the value the DOM invents', () => {
const box = el('<input type="checkbox" />') as HTMLInputElement
- expect(naming().valueOf(box)).toBe('on')
+ expect(naming().valueOf(box)).toBe('unchecked')
box.checked = true
- expect(naming().valueOf(box)).toBe('on')
+ expect(naming().valueOf(box)).toBe('checked')
+ })
+
+ it('still reports a checkbox state when the page did set a value', () => {
+ const box = el('<input type="checkbox" value="weekly" />') as HTMLInputElement
+ box.checked = true
+
+ expect(naming().valueOf(box)).toBe('checked')
+ })
+
+ it('leaves a radio group readable the same way', () => {
+ const radio = el('<input type="radio" name="plan" value="pro" />') as HTMLInputElement
+
+ expect(naming().valueOf(radio)).toBe('unchecked')
})
})
diff --git a/apps/desktop/src/lib/preview-act/naming.ts b/apps/desktop/src/lib/preview-act/naming.ts
index afadb937f3f..8c5a018564e 100644
--- a/apps/desktop/src/lib/preview-act/naming.ts
+++ b/apps/desktop/src/lib/preview-act/naming.ts
@@ -184,12 +184,27 @@ export function namingKit(doc: Document): NamingKit {
const valueOf = (el: Element): string => {
const control = el as HTMLInputElement
- if (typeof control.value === 'string' && control.value) {
- return clamp(control.value, 60)
+ // Tickable controls answer with their state, and are asked FIRST: a
+ // checkbox's `.value` is the string "on" unless the page sets one, so
+ // reading the value first made a ticked box and an empty one identical to
+ // the agent — the one thing about a checkbox worth reporting. Gated on the
+ // input's TYPE, not on `checked` being defined, which it is (as false) on
+ // every input including text fields.
+ const kind = (control.type || '').toLowerCase()
+
+ if (kind === 'checkbox' || kind === 'radio') {
+ return control.checked ? 'checked' : 'unchecked'
}
- if (control.checked !== undefined && (el.tagName === 'INPUT' || el.getAttribute('role') === 'checkbox')) {
- return control.checked ? 'checked' : 'unchecked'
+ // The same control built out of a div, where the state lives in ARIA.
+ const role = el.getAttribute('role') || ''
+
+ if (role === 'checkbox' || role === 'radio' || role === 'switch') {
+ return el.getAttribute('aria-checked') === 'true' ? 'checked' : 'unchecked'
+ }
+
+ if (typeof control.value === 'string' && control.value) {
+ return clamp(control.value, 60)
}
return ''