Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
2.5 kB · 81 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import { describe, expect, it } from 'vitest'import { wireTypeaheadSubmit } from './typeahead'
function setup() { const input = Object.assign(new EventTarget(), { value: '' }) const host = new EventTarget() let submits = 0 wireTypeaheadSubmit(input, { requestSubmit: () => submits++ }, host) const press = (key: string) => input.dispatchEvent(Object.assign(new Event('keydown'), { key })) const type = (value = 'x') => { input.value = value input.dispatchEvent(new Event('input')) } // A pointer pick: the component fills the input from its shadow-root // pointerup listener (no 'input' event), then the event reaches the host. const clickSuggestion = (handle: string) => { input.value = handle host.dispatchEvent(new Event('pointerup')) } // A pointerup that reaches the host without picking anything, e.g. on the // dropdown's padding. const clickHostChrome = () => host.dispatchEvent(new Event('pointerup')) return { press, type, clickSuggestion, clickHostChrome, submits: () => submits }}
describe('wireTypeaheadSubmit', () => { it('submits on Enter when the user has not navigated the suggestions', () => { const t = setup() t.type() t.press('Enter') expect(t.submits()).toBe(1) })
it('lets the first Enter after arrow navigation pick the suggestion, then submits', () => { const t = setup() t.type() t.press('ArrowDown') t.press('Enter') expect(t.submits()).toBe(0) t.press('Enter') expect(t.submits()).toBe(1) })
it('submits after Escape dismisses the menu', () => { const t = setup() t.press('ArrowDown') t.press('Escape') t.press('Enter') expect(t.submits()).toBe(1) })
it('treats further typing as leaving the suggestion highlight', () => { const t = setup() t.press('ArrowDown') t.type() t.press('Enter') expect(t.submits()).toBe(1) })
it('submits when clicking a suggestion fills in an account', () => { const t = setup() t.type('ali') t.clickSuggestion('alice.example.com') expect(t.submits()).toBe(1) })
it('does not submit a pointerup that picked nothing', () => { const t = setup() t.type('ali') t.clickHostChrome() expect(t.submits()).toBe(0) })
it('does not resubmit the same pick twice', () => { const t = setup() t.type('ali') t.clickSuggestion('alice.example.com') t.clickHostChrome() expect(t.submits()).toBe(1) })})