@@ -15,6 +15,7 @@ import {QueryList} from '@angular/core';
1515import { take } from 'rxjs/operators' ;
1616import { TreeKeyManager , TreeKeyManagerItem } from './tree-key-manager' ;
1717import { Observable , of as observableOf , Subscription } from 'rxjs' ;
18+ import { fakeAsync , tick } from '@angular/core/testing' ;
1819
1920class FakeBaseTreeKeyManagerItem {
2021 _isExpanded = false ;
@@ -942,344 +943,4 @@ describe('TreeKeyManager', () => {
942943 } ) ;
943944 } ) ;
944945 }
945- < < < << << HEAD
946- = === ===
947-
948- // describe('programmatic focus', () => {
949- // it('should setActiveItem()', () => {
950- // expect(keyManager.activeItemIndex)
951- // .withContext(`Expected first item of the list to be active.`)
952- // .toBe(0);
953- //
954- // keyManager.setActiveItem(1);
955- // expect(keyManager.activeItemIndex)
956- // .withContext(`Expected activeItemIndex to be updated when setActiveItem() was called.`)
957- // .toBe(1);
958- // });
959- //
960- // it('should be able to set the active item by reference', () => {
961- // expect(keyManager.activeItemIndex)
962- // .withContext(`Expected first item of the list to be active.`)
963- // .toBe(0);
964- //
965- // keyManager.setActiveItem(itemList.toArray()[2]);
966- // expect(keyManager.activeItemIndex)
967- // .withContext(`Expected activeItemIndex to be updated.`)
968- // .toBe(2);
969- // });
970- //
971- // it('should be able to set the active item without emitting an event', () => {
972- // const spy = jasmine.createSpy('change spy');
973- // const subscription = keyManager.change.subscribe(spy);
974- //
975- // expect(keyManager.activeItemIndex).toBe(0);
976- //
977- // keyManager.updateActiveItem(2);
978- //
979- // expect(keyManager.activeItemIndex).toBe(2);
980- // expect(spy).not.toHaveBeenCalled();
981- //
982- // subscription.unsubscribe();
983- // });
984- //
985- // it('should expose the active item correctly', () => {
986- // keyManager.onKeydown(fakeKeyEvents.downArrow);
987- //
988- // expect(keyManager.activeItemIndex)
989- // .withContext('Expected active item to be the second option.')
990- // .toBe(1);
991- // expect(keyManager.activeItem)
992- // .withContext('Expected the active item to match the second option.')
993- // .toBe(itemList.toArray()[1]);
994- //
995- // keyManager.onKeydown(fakeKeyEvents.downArrow);
996- // expect(keyManager.activeItemIndex)
997- // .withContext('Expected active item to be the third option.')
998- // .toBe(2);
999- // expect(keyManager.activeItem)
1000- // .withContext('Expected the active item ID to match the third option.')
1001- // .toBe(itemList.toArray()[2]);
1002- // });
1003- //
1004- // it('should setFirstItemActive()', () => {
1005- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1006- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1007- // expect(keyManager.activeItemIndex)
1008- // .withContext(`Expected last item of the list to be active.`)
1009- // .toBe(2);
1010- //
1011- // keyManager.setFirstItemActive();
1012- // expect(keyManager.activeItemIndex)
1013- // .withContext(`Expected setFirstItemActive() to set the active item to the first item.`)
1014- // .toBe(0);
1015- // });
1016- //
1017- // it('should set the active item to the second item if the first one is disabled', () => {
1018- // const items = itemList.toArray();
1019- // items[0].disabled = true;
1020- // itemList.reset(items);
1021- //
1022- // keyManager.setFirstItemActive();
1023- // expect(keyManager.activeItemIndex)
1024- // .withContext(`Expected the second item to be active if the first was disabled.`)
1025- // .toBe(1);
1026- // });
1027- //
1028- // it('should setLastItemActive()', () => {
1029- // expect(keyManager.activeItemIndex)
1030- // .withContext(`Expected first item of the list to be active.`)
1031- // .toBe(0);
1032- //
1033- // keyManager.setLastItemActive();
1034- // expect(keyManager.activeItemIndex)
1035- // .withContext(`Expected setLastItemActive() to set the active item to the last item.`)
1036- // .toBe(2);
1037- // });
1038- //
1039- // it('should set the active item to the second to last item if the last is disabled', () => {
1040- // const items = itemList.toArray();
1041- // items[2].disabled = true;
1042- // itemList.reset(items);
1043- //
1044- // keyManager.setLastItemActive();
1045- // expect(keyManager.activeItemIndex)
1046- // .withContext(`Expected the second to last item to be active if the last was disabled.`)
1047- // .toBe(1);
1048- // });
1049- //
1050- // it('should setNextItemActive()', () => {
1051- // expect(keyManager.activeItemIndex)
1052- // .withContext(`Expected first item of the list to be active.`)
1053- // .toBe(0);
1054- //
1055- // keyManager.setNextItemActive();
1056- // expect(keyManager.activeItemIndex)
1057- // .withContext(`Expected setNextItemActive() to set the active item to the next item.`)
1058- // .toBe(1);
1059- // });
1060- //
1061- // it('should set the active item to the next enabled item if next is disabled', () => {
1062- // const items = itemList.toArray();
1063- // items[1].disabled = true;
1064- // itemList.reset(items);
1065- //
1066- // expect(keyManager.activeItemIndex)
1067- // .withContext(`Expected first item of the list to be active.`)
1068- // .toBe(0);
1069- //
1070- // keyManager.setNextItemActive();
1071- // expect(keyManager.activeItemIndex)
1072- // .withContext(`Expected setNextItemActive() to only set enabled items as active.`)
1073- // .toBe(2);
1074- // });
1075- //
1076- // it('should setPreviousItemActive()', () => {
1077- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1078- // expect(keyManager.activeItemIndex)
1079- // .withContext(`Expected second item of the list to be active.`)
1080- // .toBe(1);
1081- //
1082- // keyManager.setPreviousItemActive();
1083- // expect(keyManager.activeItemIndex)
1084- // .withContext(`Expected setPreviousItemActive() to set the active item to the previous.`)
1085- // .toBe(0);
1086- // });
1087- //
1088- // it('should skip disabled items when setPreviousItemActive() is called', () => {
1089- // const items = itemList.toArray();
1090- // items[1].disabled = true;
1091- // itemList.reset(items);
1092- //
1093- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1094- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1095- // expect(keyManager.activeItemIndex)
1096- // .withContext(`Expected third item of the list to be active.`)
1097- // .toBe(2);
1098- //
1099- // keyManager.setPreviousItemActive();
1100- // expect(keyManager.activeItemIndex)
1101- // .withContext(`Expected setPreviousItemActive() to skip the disabled item.`)
1102- // .toBe(0);
1103- // });
1104- //
1105- // it('should not emit an event if the item did not change', () => {
1106- // const spy = jasmine.createSpy('change spy');
1107- // const subscription = keyManager.change.subscribe(spy);
1108- //
1109- // keyManager.setActiveItem(2);
1110- // keyManager.setActiveItem(2);
1111- //
1112- // expect(spy).toHaveBeenCalledTimes(1);
1113- //
1114- // subscription.unsubscribe();
1115- // });
1116- // });
1117- //
1118- // describe('wrap mode', () => {
1119- // it('should return itself to allow chaining', () => {
1120- // expect(keyManager.withWrap())
1121- // .withContext(`Expected withWrap() to return an instance of ListKeyManager.`)
1122- // .toEqual(keyManager);
1123- // });
1124- //
1125- // it('should wrap focus when arrow keying past items while in wrap mode', () => {
1126- // keyManager.withWrap();
1127- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1128- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1129- //
1130- // expect(keyManager.activeItemIndex).withContext('Expected last item to be active.').toBe(2);
1131- //
1132- // // this down arrow moves down past the end of the list
1133- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1134- // expect(keyManager.activeItemIndex)
1135- // .withContext('Expected active item to wrap to beginning.')
1136- // .toBe(0);
1137- //
1138- // // this up arrow moves up past the beginning of the list
1139- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1140- // expect(keyManager.activeItemIndex)
1141- // .withContext('Expected active item to wrap to end.')
1142- // .toBe(2);
1143- // });
1144- //
1145- // it('should set last item active when up arrow is pressed if no active item', () => {
1146- // keyManager.withWrap();
1147- // keyManager.setActiveItem(-1);
1148- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1149- //
1150- // expect(keyManager.activeItemIndex)
1151- // .withContext('Expected last item to be active on up arrow if no active item.')
1152- // .toBe(2);
1153- // expect(keyManager.setActiveItem).not.toHaveBeenCalledWith(0);
1154- // expect(keyManager.setActiveItem).toHaveBeenCalledWith(2);
1155- //
1156- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1157- // expect(keyManager.activeItemIndex)
1158- // .withContext('Expected active item to be 0 after wrapping back to beginning.')
1159- // .toBe(0);
1160- // expect(keyManager.setActiveItem).toHaveBeenCalledWith(0);
1161- // });
1162- //
1163- // // This test should pass if all items are disabled and the down arrow key got pressed.
1164- // // If the test setup crashes or this test times out, this test can be considered as failed.
1165- // it('should not get into an infinite loop if all items are disabled', () => {
1166- // keyManager.withWrap();
1167- // keyManager.setActiveItem(0);
1168- // const items = itemList.toArray();
1169- // items.forEach(item => (item.disabled = true));
1170- // itemList.reset(items);
1171- //
1172- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1173- // });
1174- //
1175- // it('should be able to disable wrapping', () => {
1176- // keyManager.withWrap();
1177- // keyManager.setFirstItemActive();
1178- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1179- //
1180- // expect(keyManager.activeItemIndex).toBe(itemList.length - 1);
1181- //
1182- // keyManager.withWrap(false);
1183- // keyManager.setFirstItemActive();
1184- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1185- //
1186- // expect(keyManager.activeItemIndex).toBe(0);
1187- // });
1188- // });
1189- //
1190- // describe('skip predicate', () => {
1191- // it('should skip disabled items by default', () => {
1192- // const items = itemList.toArray();
1193- // items[1].disabled = true;
1194- // itemList.reset(items);
1195- //
1196- // expect(keyManager.activeItemIndex).toBe(0);
1197- //
1198- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1199- //
1200- // expect(keyManager.activeItemIndex).toBe(2);
1201- // });
1202- //
1203- // it('should be able to skip items with a custom predicate', () => {
1204- // keyManager.skipPredicate(item => item.skipItem);
1205- //
1206- // const items = itemList.toArray();
1207- // items[1].skipItem = true;
1208- // itemList.reset(items);
1209- //
1210- // expect(keyManager.activeItemIndex).toBe(0);
1211- //
1212- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1213- //
1214- // expect(keyManager.activeItemIndex).toBe(2);
1215- // });
1216- // });
1217- //
1218- //
1219- // let keyManager: FocusKeyManager<FakeFocusable>;
1220- //
1221- // beforeEach(() => {
1222- // itemList.reset([new FakeFocusable(), new FakeFocusable(), new FakeFocusable()]);
1223- // keyManager = new FocusKeyManager<FakeFocusable>(itemList);
1224- //
1225- // // first item is already focused
1226- // keyManager.setFirstItemActive();
1227- //
1228- // spyOn(itemList.toArray()[0], 'focus');
1229- // spyOn(itemList.toArray()[1], 'focus');
1230- // spyOn(itemList.toArray()[2], 'focus');
1231- // });
1232- //
1233- // it('should focus subsequent items when down arrow is pressed', () => {
1234- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1235- //
1236- // expect(itemList.toArray()[0].focus).not.toHaveBeenCalled();
1237- // expect(itemList.toArray()[1].focus).toHaveBeenCalledTimes(1);
1238- // expect(itemList.toArray()[2].focus).not.toHaveBeenCalled();
1239- //
1240- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1241- // expect(itemList.toArray()[0].focus).not.toHaveBeenCalled();
1242- // expect(itemList.toArray()[1].focus).toHaveBeenCalledTimes(1);
1243- // expect(itemList.toArray()[2].focus).toHaveBeenCalledTimes(1);
1244- // });
1245- //
1246- // it('should focus previous items when up arrow is pressed', () => {
1247- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1248- //
1249- // expect(itemList.toArray()[0].focus).not.toHaveBeenCalled();
1250- // expect(itemList.toArray()[1].focus).toHaveBeenCalledTimes(1);
1251- //
1252- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1253- //
1254- // expect(itemList.toArray()[0].focus).toHaveBeenCalledTimes(1);
1255- // expect(itemList.toArray()[1].focus).toHaveBeenCalledTimes(1);
1256- // });
1257- //
1258- // it('should allow setting the focused item without calling focus', () => {
1259- // expect(keyManager.activeItemIndex)
1260- // .withContext(`Expected first item of the list to be active.`)
1261- // .toBe(0);
1262- //
1263- // keyManager.updateActiveItem(1);
1264- // expect(keyManager.activeItemIndex)
1265- // .withContext(`Expected activeItemIndex to update after calling updateActiveItem().`)
1266- // .toBe(1);
1267- // expect(itemList.toArray()[1].focus).not.toHaveBeenCalledTimes(1);
1268- // });
1269- //
1270- // it('should be able to set the focus origin', () => {
1271- // keyManager.setFocusOrigin('mouse');
1272- //
1273- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1274- // expect(itemList.toArray()[1].focus).toHaveBeenCalledWith('mouse');
1275- //
1276- // keyManager.onKeydown(fakeKeyEvents.downArrow);
1277- // expect(itemList.toArray()[2].focus).toHaveBeenCalledWith('mouse');
1278- //
1279- // keyManager.setFocusOrigin('keyboard');
1280- //
1281- // keyManager.onKeydown(fakeKeyEvents.upArrow);
1282- // expect(itemList.toArray()[1].focus).toHaveBeenCalledWith('keyboard');
1283- // });
1284- >>> >>> > ff6a4790f ( feat ( cdk / a11y ) : add tests for typeahead )
1285946} ) ;
0 commit comments