File tree 2 files changed +20
-4
lines changed
2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -82,15 +82,28 @@ describe('Key managers', () => {
82
82
spyOn ( keyManager , 'setActiveItem' ) . and . callThrough ( ) ;
83
83
} ) ;
84
84
85
- it ( 'should maintain the active item if the amount of items changes' , ( ) => {
85
+ it ( 'should maintain the active item if the amount of items changes' , fakeAsync ( ( ) => {
86
86
expect ( keyManager . activeItemIndex ) . toBe ( 0 ) ;
87
87
expect ( keyManager . activeItem ! . getLabel ( ) ) . toBe ( 'one' ) ;
88
88
itemList . reset ( [ new FakeFocusable ( 'zero' ) , ...itemList . toArray ( ) ] ) ;
89
89
itemList . notifyOnChanges ( ) ;
90
+ tick ( ) ;
90
91
91
92
expect ( keyManager . activeItemIndex ) . toBe ( 1 ) ;
92
93
expect ( keyManager . activeItem ! . getLabel ( ) ) . toBe ( 'one' ) ;
93
- } ) ;
94
+ } ) ) ;
95
+
96
+ it ( 'should keep the active item in sync if the active item is removed' , fakeAsync ( ( ) => {
97
+ expect ( keyManager . activeItemIndex ) . toBe ( 0 ) ;
98
+ expect ( keyManager . activeItem ! . getLabel ( ) ) . toBe ( 'one' ) ;
99
+
100
+ itemList . reset ( itemList . toArray ( ) . slice ( 1 ) ) ;
101
+ itemList . notifyOnChanges ( ) ;
102
+ tick ( ) ;
103
+
104
+ expect ( keyManager . activeItemIndex ) . toBe ( 0 ) ;
105
+ expect ( keyManager . activeItem ! . getLabel ( ) ) . toBe ( 'two' ) ;
106
+ } ) ) ;
94
107
95
108
it ( 'should start off the activeItem as null' , ( ) => {
96
109
expect ( new ListKeyManager ( [ ] ) . activeItem ) . toBeNull ( ) ;
Original file line number Diff line number Diff line change @@ -70,8 +70,11 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
70
70
const itemArray = newItems . toArray ( ) ;
71
71
const newIndex = itemArray . indexOf ( this . _activeItem ) ;
72
72
73
- if ( newIndex > - 1 && newIndex !== this . _activeItemIndex ) {
74
- this . _activeItemIndex = newIndex ;
73
+ if ( newIndex !== this . _activeItemIndex ) {
74
+ // Timeout is required to avoid "changed after checked" errors.
75
+ setTimeout ( ( ) => {
76
+ this . updateActiveItem ( newIndex > - 1 ? newIndex : this . _activeItemIndex ) ;
77
+ } , 0 ) ;
75
78
}
76
79
}
77
80
} ) ;
You can’t perform that action at this time.
0 commit comments