File tree 4 files changed +24
-4
lines changed
4 files changed +24
-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
} ) ;
Original file line number Diff line number Diff line change @@ -1176,6 +1176,7 @@ describe('MDC-based MatMenu', () => {
1176
1176
fixture . detectChanges ( ) ;
1177
1177
tick ( 500 ) ;
1178
1178
fixture . detectChanges ( ) ;
1179
+ tick ( ) ;
1179
1180
1180
1181
expect ( fixture . componentInstance . items . length ) . toBe ( 0 ) ;
1181
1182
} ) ) ;
@@ -1201,6 +1202,7 @@ describe('MDC-based MatMenu', () => {
1201
1202
. toBe ( true ) ;
1202
1203
tick ( 500 ) ;
1203
1204
fixture . detectChanges ( ) ;
1205
+ tick ( ) ;
1204
1206
1205
1207
expect ( trigger . menuOpen ) . withContext ( 'Expected menu to be closed' ) . toBe ( false ) ;
1206
1208
} ) ) ;
Original file line number Diff line number Diff line change @@ -1171,6 +1171,7 @@ describe('MatMenu', () => {
1171
1171
fixture . detectChanges ( ) ;
1172
1172
tick ( 500 ) ;
1173
1173
fixture . detectChanges ( ) ;
1174
+ tick ( ) ;
1174
1175
1175
1176
expect ( fixture . componentInstance . items . length ) . toBe ( 0 ) ;
1176
1177
} ) ) ;
@@ -1196,6 +1197,7 @@ describe('MatMenu', () => {
1196
1197
. toBe ( true ) ;
1197
1198
tick ( 500 ) ;
1198
1199
fixture . detectChanges ( ) ;
1200
+ tick ( ) ;
1199
1201
1200
1202
expect ( trigger . menuOpen ) . withContext ( 'Expected menu to be closed' ) . toBe ( false ) ;
1201
1203
} ) ) ;
You can’t perform that action at this time.
0 commit comments