|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {SimpleComboboxPattern, SimpleComboboxPopupPattern} from './simple-combobox'; |
| 3 | +import {signal} from '../behaviors/signal-like/signal-like'; |
| 4 | +import {createKeyboardEvent} from '@angular/cdk/testing/private'; |
| 5 | + |
| 6 | +@Component({template: ''}) |
| 7 | +class DummyComponent {} |
| 8 | + |
| 9 | +describe('SimpleComboboxPattern', () => { |
| 10 | + function setup( |
| 11 | + inputs: Partial<{ |
| 12 | + disabled: boolean; |
| 13 | + alwaysExpanded: boolean; |
| 14 | + inlineSuggestion: string; |
| 15 | + popupType: 'listbox' | 'tree' | 'grid' | 'dialog'; |
| 16 | + }> = {}, |
| 17 | + ) { |
| 18 | + const element = document.createElement('input'); |
| 19 | + const value = signal(''); |
| 20 | + const expanded = signal(false); |
| 21 | + const alwaysExpanded = signal(inputs.alwaysExpanded ?? false); |
| 22 | + const disabled = signal(inputs.disabled ?? false); |
| 23 | + const inlineSuggestion = signal<string | undefined>(inputs.inlineSuggestion); |
| 24 | + |
| 25 | + // Mock a generic popup pattern |
| 26 | + const popupId = signal('popup-1'); |
| 27 | + const activeDescendant = signal<string | undefined>('item-1'); |
| 28 | + const controlTarget = document.createElement('div'); |
| 29 | + const popupType = signal<'listbox' | 'tree' | 'grid' | 'dialog'>(inputs.popupType ?? 'listbox'); |
| 30 | + |
| 31 | + const popup = new SimpleComboboxPopupPattern({ |
| 32 | + popupType, |
| 33 | + controlTarget: signal(controlTarget), |
| 34 | + activeDescendant, |
| 35 | + popupId, |
| 36 | + }); |
| 37 | + |
| 38 | + const pattern = new SimpleComboboxPattern({ |
| 39 | + alwaysExpanded, |
| 40 | + value, |
| 41 | + element: signal(element), |
| 42 | + popup: signal(popup), |
| 43 | + inlineSuggestion, |
| 44 | + disabled, |
| 45 | + expanded, |
| 46 | + expandable: signal(true), |
| 47 | + }); |
| 48 | + |
| 49 | + return { |
| 50 | + pattern, |
| 51 | + element, |
| 52 | + value, |
| 53 | + expanded, |
| 54 | + alwaysExpanded, |
| 55 | + inlineSuggestion, |
| 56 | + disabled, |
| 57 | + popup, |
| 58 | + controlTarget, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + describe('Aria-autocomplete calculation', () => { |
| 63 | + it('should return "list" when only popup is present', () => { |
| 64 | + const {pattern} = setup(); |
| 65 | + expect(pattern.autocomplete()).toBe('list'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return "both" when popup and inline suggestion are present', () => { |
| 69 | + const {pattern} = setup({inlineSuggestion: 'suggestion'}); |
| 70 | + expect(pattern.autocomplete()).toBe('both'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return "none" when only dialog popup is present', () => { |
| 74 | + const {pattern} = setup({popupType: 'dialog'}); |
| 75 | + expect(pattern.autocomplete()).toBe('none'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return "inline" when dialog popup and inline suggestion are present', () => { |
| 79 | + const {pattern} = setup({popupType: 'dialog', inlineSuggestion: 'suggestion'}); |
| 80 | + expect(pattern.autocomplete()).toBe('inline'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('Expansion via Keyboard', () => { |
| 85 | + it('should open on ArrowDown when collapsed', () => { |
| 86 | + const {pattern, expanded} = setup(); |
| 87 | + expect(expanded()).toBe(false); |
| 88 | + |
| 89 | + pattern.onKeydown(createKeyboardEvent('keydown', 40, 'ArrowDown')); |
| 90 | + expect(expanded()).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should close on Escape when expanded', () => { |
| 94 | + const {pattern, expanded} = setup(); |
| 95 | + expanded.set(true); |
| 96 | + |
| 97 | + pattern.onKeydown(createKeyboardEvent('keydown', 27, 'Escape')); |
| 98 | + expect(expanded()).toBe(false); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Input handling', () => { |
| 103 | + it('should update value and expand on input', () => { |
| 104 | + const {pattern, element, value, expanded} = setup(); |
| 105 | + expect(expanded()).toBe(false); |
| 106 | + |
| 107 | + element.value = 'hello'; |
| 108 | + pattern.onInput({target: element} as unknown as Event); |
| 109 | + |
| 110 | + expect(value()).toBe('hello'); |
| 111 | + expect(expanded()).toBe(true); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('Focus handling', () => { |
| 116 | + it('should track focus state', () => { |
| 117 | + const {pattern} = setup(); |
| 118 | + |
| 119 | + pattern.onFocusin(); |
| 120 | + expect(pattern.isFocused()).toBe(true); |
| 121 | + |
| 122 | + pattern.onFocusout(new FocusEvent('focusout')); |
| 123 | + expect(pattern.isFocused()).toBe(false); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('Inline Suggestion / Highlighting', () => { |
| 128 | + it('should insert the inline suggestion into the input and select the remaining text', () => { |
| 129 | + const {pattern, element, value, expanded, inlineSuggestion} = setup(); |
| 130 | + |
| 131 | + value.set('App'); |
| 132 | + inlineSuggestion.set('Apple'); |
| 133 | + expanded.set(true); |
| 134 | + pattern.isFocused.set(true); |
| 135 | + |
| 136 | + pattern.highlightEffect(); |
| 137 | + |
| 138 | + expect(element.value).toBe('Apple'); |
| 139 | + expect(element.selectionStart).toBe(3); |
| 140 | + expect(element.selectionEnd).toBe(5); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should not highlight when deleting text', () => { |
| 144 | + const {pattern, element, value, expanded, inlineSuggestion} = setup(); |
| 145 | + |
| 146 | + value.set('App'); |
| 147 | + inlineSuggestion.set('Apple'); |
| 148 | + expanded.set(true); |
| 149 | + pattern.isFocused.set(true); |
| 150 | + |
| 151 | + const deleteEvent = new InputEvent('input', {inputType: 'deleteContentBackward'}); |
| 152 | + Object.defineProperty(deleteEvent, 'target', {value: element}); |
| 153 | + pattern.onInput(deleteEvent as Event); |
| 154 | + |
| 155 | + expect(pattern.isDeleting()).toBe(true); |
| 156 | + |
| 157 | + pattern.highlightEffect(); |
| 158 | + |
| 159 | + expect(element.value).not.toBe('Apple'); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('Select-only combobox behavior', () => { |
| 164 | + function setupSelectOnly() { |
| 165 | + const selectOnlyElement = document.createElement('div'); |
| 166 | + const {pattern, expanded, controlTarget} = setup(); |
| 167 | + |
| 168 | + // Override element to be select-only |
| 169 | + pattern.inputs.element = signal(selectOnlyElement); |
| 170 | + |
| 171 | + return {pattern, expanded, selectOnlyElement, controlTarget}; |
| 172 | + } |
| 173 | + |
| 174 | + it('should toggle expansion on click', () => { |
| 175 | + const {pattern, expanded} = setupSelectOnly(); |
| 176 | + expect(expanded()).toBe(false); |
| 177 | + |
| 178 | + pattern.onClick(new PointerEvent('click')); |
| 179 | + expect(expanded()).toBe(true); |
| 180 | + |
| 181 | + pattern.onClick(new PointerEvent('click')); |
| 182 | + expect(expanded()).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should open on Enter or Space when collapsed', () => { |
| 186 | + const {pattern, expanded} = setupSelectOnly(); |
| 187 | + |
| 188 | + pattern.onKeydown(createKeyboardEvent('keydown', 13, 'Enter')); |
| 189 | + expect(expanded()).toBe(true); |
| 190 | + |
| 191 | + expanded.set(false); |
| 192 | + |
| 193 | + pattern.onKeydown(createKeyboardEvent('keydown', 32, ' ')); |
| 194 | + expect(expanded()).toBe(true); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('alwaysExpanded behavior', () => { |
| 199 | + it('should stay open on Escape when alwaysExpanded is true', () => { |
| 200 | + const {pattern, expanded} = setup({alwaysExpanded: true}); |
| 201 | + expanded.set(true); |
| 202 | + |
| 203 | + pattern.onKeydown(createKeyboardEvent('keydown', 27, 'Escape')); |
| 204 | + expect(expanded()).toBe(true); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + describe('Blur behavior', () => { |
| 209 | + it('should close when focus leaves both combobox and popup', () => { |
| 210 | + const {pattern, expanded} = setup(); |
| 211 | + expanded.set(true); |
| 212 | + pattern.isFocused.set(false); |
| 213 | + pattern.inputs.popup()!.isFocused.set(false); |
| 214 | + |
| 215 | + pattern.closePopupOnBlurEffect(); |
| 216 | + expect(expanded()).toBe(false); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should remain open if popup is focused', () => { |
| 220 | + const {pattern, expanded} = setup(); |
| 221 | + expanded.set(true); |
| 222 | + pattern.isFocused.set(false); |
| 223 | + pattern.inputs.popup()!.isFocused.set(true); |
| 224 | + |
| 225 | + pattern.closePopupOnBlurEffect(); |
| 226 | + expect(expanded()).toBe(true); |
| 227 | + }); |
| 228 | + }); |
| 229 | +}); |
0 commit comments