|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {computed, signal} from '@angular/core'; |
10 | | -import {SignalLike} from '../signal-like/signal-like'; |
11 | | -import {ListNavigation, ListNavigationInputs} from '../list-navigation/list-navigation'; |
| 9 | +import {Signal, signal, WritableSignal} from '@angular/core'; |
12 | 10 | import {ListFocus, ListFocusInputs, ListFocusItem} from './list-focus'; |
13 | 11 |
|
14 | | -describe('List Focus', () => { |
15 | | - interface TestItem extends ListFocusItem { |
16 | | - tabindex: SignalLike<-1 | 0>; |
17 | | - } |
18 | | - |
19 | | - function getItems(length: number): SignalLike<TestItem[]> { |
20 | | - return signal( |
21 | | - Array.from({length}).map((_, i) => ({ |
22 | | - index: signal(i), |
| 12 | +type TestItem = ListFocusItem & { |
| 13 | + disabled: WritableSignal<boolean>; |
| 14 | +}; |
| 15 | + |
| 16 | +type TestInputs = Partial<ListFocusInputs<ListFocusItem>> & { |
| 17 | + numItems?: number; |
| 18 | +}; |
| 19 | + |
| 20 | +export function getListFocus(inputs: TestInputs = {}): ListFocus<ListFocusItem> { |
| 21 | + return new ListFocus({ |
| 22 | + activeIndex: signal(0), |
| 23 | + disabled: signal(false), |
| 24 | + skipDisabled: signal(false), |
| 25 | + focusMode: signal('roving'), |
| 26 | + items: getItems(inputs.numItems ?? 5), |
| 27 | + ...inputs, |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function getItems(length: number): Signal<ListFocusItem[]> { |
| 32 | + return signal( |
| 33 | + Array.from({length}).map((_, i) => { |
| 34 | + return { |
23 | 35 | id: signal(`${i}`), |
24 | | - tabindex: signal(-1), |
25 | 36 | disabled: signal(false), |
26 | 37 | element: signal({focus: () => {}} as HTMLElement), |
27 | | - })), |
28 | | - ); |
29 | | - } |
30 | | - |
31 | | - function getNavigation<T extends TestItem>( |
32 | | - items: SignalLike<T[]>, |
33 | | - args: Partial<ListNavigationInputs<T>> = {}, |
34 | | - ): ListNavigation<T> { |
35 | | - return new ListNavigation({ |
36 | | - items, |
37 | | - wrap: signal(false), |
38 | | - activeIndex: signal(0), |
39 | | - skipDisabled: signal(false), |
40 | | - textDirection: signal('ltr'), |
41 | | - orientation: signal('vertical'), |
42 | | - ...args, |
43 | | - }); |
44 | | - } |
45 | | - |
46 | | - function getFocus<T extends TestItem>( |
47 | | - navigation: ListNavigation<T>, |
48 | | - args: Partial<ListFocusInputs<T>> = {}, |
49 | | - ): ListFocus<T> { |
50 | | - return new ListFocus({ |
51 | | - navigation, |
52 | | - focusMode: signal('roving'), |
53 | | - ...args, |
54 | | - }); |
55 | | - } |
| 38 | + }; |
| 39 | + }), |
| 40 | + ); |
| 41 | +} |
56 | 42 |
|
| 43 | +describe('List Focus', () => { |
57 | 44 | describe('roving', () => { |
| 45 | + let focusManager: ListFocus<ListFocusItem>; |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + focusManager = getListFocus({focusMode: signal('roving')}); |
| 49 | + }); |
| 50 | + |
58 | 51 | it('should set the list tabindex to -1', () => { |
59 | | - const items = getItems(5); |
60 | | - const nav = getNavigation(items); |
61 | | - const focus = getFocus(nav); |
62 | | - const tabindex = computed(() => focus.getListTabindex()); |
63 | | - expect(tabindex()).toBe(-1); |
| 52 | + expect(focusManager.getListTabindex()).toBe(-1); |
64 | 53 | }); |
65 | 54 |
|
66 | 55 | it('should set the activedescendant to undefined', () => { |
67 | | - const items = getItems(5); |
68 | | - const nav = getNavigation(items); |
69 | | - const focus = getFocus(nav); |
70 | | - expect(focus.getActiveDescendant()).toBeUndefined(); |
| 56 | + expect(focusManager.getActiveDescendant()).toBeUndefined(); |
71 | 57 | }); |
72 | 58 |
|
73 | | - it('should set the first items tabindex to 0', () => { |
74 | | - const items = getItems(5); |
75 | | - const nav = getNavigation(items); |
76 | | - const focus = getFocus(nav); |
77 | | - |
78 | | - items().forEach(i => { |
79 | | - i.tabindex = computed(() => focus.getItemTabindex(i)); |
80 | | - }); |
81 | | - |
82 | | - expect(items()[0].tabindex()).toBe(0); |
83 | | - expect(items()[1].tabindex()).toBe(-1); |
84 | | - expect(items()[2].tabindex()).toBe(-1); |
85 | | - expect(items()[3].tabindex()).toBe(-1); |
86 | | - expect(items()[4].tabindex()).toBe(-1); |
| 59 | + it('should set the tabindex based on the active index', () => { |
| 60 | + const items = focusManager.inputs.items() as TestItem[]; |
| 61 | + focusManager.inputs.activeIndex.set(2); |
| 62 | + expect(focusManager.getItemTabindex(items[0])).toBe(-1); |
| 63 | + expect(focusManager.getItemTabindex(items[1])).toBe(-1); |
| 64 | + expect(focusManager.getItemTabindex(items[2])).toBe(0); |
| 65 | + expect(focusManager.getItemTabindex(items[3])).toBe(-1); |
| 66 | + expect(focusManager.getItemTabindex(items[4])).toBe(-1); |
87 | 67 | }); |
| 68 | + }); |
88 | 69 |
|
89 | | - it('should update the tabindex of the active item when navigating', () => { |
90 | | - const items = getItems(5); |
91 | | - const nav = getNavigation(items); |
92 | | - const focus = getFocus(nav); |
93 | | - |
94 | | - items().forEach(i => { |
95 | | - i.tabindex = computed(() => focus.getItemTabindex(i)); |
96 | | - }); |
97 | | - |
98 | | - nav.next(); |
| 70 | + describe('activedescendant', () => { |
| 71 | + let focusManager: ListFocus<ListFocusItem>; |
99 | 72 |
|
100 | | - expect(items()[0].tabindex()).toBe(-1); |
101 | | - expect(items()[1].tabindex()).toBe(0); |
102 | | - expect(items()[2].tabindex()).toBe(-1); |
103 | | - expect(items()[3].tabindex()).toBe(-1); |
104 | | - expect(items()[4].tabindex()).toBe(-1); |
| 73 | + beforeEach(() => { |
| 74 | + focusManager = getListFocus({focusMode: signal('activedescendant')}); |
105 | 75 | }); |
106 | | - }); |
107 | 76 |
|
108 | | - describe('activedescendant', () => { |
109 | 77 | it('should set the list tabindex to 0', () => { |
110 | | - const items = getItems(5); |
111 | | - const nav = getNavigation(items); |
112 | | - const focus = getFocus(nav, { |
113 | | - focusMode: signal('activedescendant'), |
114 | | - }); |
115 | | - const tabindex = computed(() => focus.getListTabindex()); |
116 | | - expect(tabindex()).toBe(0); |
| 78 | + expect(focusManager.getListTabindex()).toBe(0); |
117 | 79 | }); |
118 | 80 |
|
119 | 81 | it('should set the activedescendant to the active items id', () => { |
120 | | - const items = getItems(5); |
121 | | - const nav = getNavigation(items); |
122 | | - const focus = getFocus(nav, { |
123 | | - focusMode: signal('activedescendant'), |
124 | | - }); |
125 | | - expect(focus.getActiveDescendant()).toBe(items()[0].id()); |
| 82 | + expect(focusManager.getActiveDescendant()).toBe(focusManager.inputs.items()[0].id()); |
126 | 83 | }); |
127 | 84 |
|
128 | 85 | it('should set the tabindex of all items to -1', () => { |
129 | | - const items = getItems(5); |
130 | | - const nav = getNavigation(items); |
131 | | - const focus = getFocus(nav, { |
132 | | - focusMode: signal('activedescendant'), |
133 | | - }); |
134 | | - |
135 | | - items().forEach(i => { |
136 | | - i.tabindex = computed(() => focus.getItemTabindex(i)); |
137 | | - }); |
138 | | - |
139 | | - expect(items()[0].tabindex()).toBe(-1); |
140 | | - expect(items()[1].tabindex()).toBe(-1); |
141 | | - expect(items()[2].tabindex()).toBe(-1); |
142 | | - expect(items()[3].tabindex()).toBe(-1); |
143 | | - expect(items()[4].tabindex()).toBe(-1); |
| 86 | + const items = focusManager.inputs.items() as TestItem[]; |
| 87 | + focusManager.inputs.activeIndex.set(0); |
| 88 | + expect(focusManager.getItemTabindex(items[0])).toBe(-1); |
| 89 | + expect(focusManager.getItemTabindex(items[1])).toBe(-1); |
| 90 | + expect(focusManager.getItemTabindex(items[2])).toBe(-1); |
| 91 | + expect(focusManager.getItemTabindex(items[3])).toBe(-1); |
| 92 | + expect(focusManager.getItemTabindex(items[4])).toBe(-1); |
144 | 93 | }); |
145 | 94 |
|
146 | 95 | it('should update the activedescendant of the list when navigating', () => { |
147 | | - const items = getItems(5); |
148 | | - const nav = getNavigation(items); |
149 | | - const focus = getFocus(nav, { |
150 | | - focusMode: signal('activedescendant'), |
151 | | - }); |
152 | | - |
153 | | - nav.next(); |
154 | | - expect(focus.getActiveDescendant()).toBe(items()[1].id()); |
| 96 | + focusManager.inputs.activeIndex.set(1); |
| 97 | + expect(focusManager.getActiveDescendant()).toBe(focusManager.inputs.items()[1].id()); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('#isFocusable', () => { |
| 102 | + it('should return true for enabled items', () => { |
| 103 | + const focusManager = getListFocus({skipDisabled: signal(true)}); |
| 104 | + const items = focusManager.inputs.items() as TestItem[]; |
| 105 | + expect(focusManager.isFocusable(items[0])).toBeTrue(); |
| 106 | + expect(focusManager.isFocusable(items[1])).toBeTrue(); |
| 107 | + expect(focusManager.isFocusable(items[2])).toBeTrue(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should return false for disabled items', () => { |
| 111 | + const focusManager = getListFocus({skipDisabled: signal(true)}); |
| 112 | + const items = focusManager.inputs.items() as TestItem[]; |
| 113 | + items[1].disabled.set(true); |
| 114 | + |
| 115 | + expect(focusManager.isFocusable(items[0])).toBeTrue(); |
| 116 | + expect(focusManager.isFocusable(items[1])).toBeFalse(); |
| 117 | + expect(focusManager.isFocusable(items[2])).toBeTrue(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return true for disabled items if skip disabled is false', () => { |
| 121 | + const focusManager = getListFocus({skipDisabled: signal(false)}); |
| 122 | + const items = focusManager.inputs.items() as TestItem[]; |
| 123 | + items[1].disabled.set(true); |
| 124 | + |
| 125 | + expect(focusManager.isFocusable(items[0])).toBeTrue(); |
| 126 | + expect(focusManager.isFocusable(items[1])).toBeTrue(); |
| 127 | + expect(focusManager.isFocusable(items[2])).toBeTrue(); |
155 | 128 | }); |
156 | 129 | }); |
157 | 130 | }); |
0 commit comments