@@ -82,6 +82,11 @@ describe('Key managers', () => {
82
82
spyOn ( keyManager , 'setActiveItem' ) . and . callThrough ( ) ;
83
83
} ) ;
84
84
85
+ afterEach ( ( ) => {
86
+ keyManager . destroy ( ) ;
87
+ keyManager = null ! ;
88
+ } ) ;
89
+
85
90
it ( 'should maintain the active item if the amount of items changes' , ( ) => {
86
91
expect ( keyManager . activeItemIndex ) . toBe ( 0 ) ;
87
92
expect ( keyManager . activeItem ! . getLabel ( ) ) . toBe ( 'one' ) ;
@@ -110,6 +115,14 @@ describe('Key managers', () => {
110
115
expect ( spy ) . toHaveBeenCalled ( ) ;
111
116
} ) ;
112
117
118
+ it ( 'should complete the tabOut stream on destroy' , ( ) => {
119
+ const spy = jasmine . createSpy ( 'complete spy' ) ;
120
+ keyManager . tabOut . pipe ( take ( 1 ) ) . subscribe ( { complete : spy } ) ;
121
+ keyManager . destroy ( ) ;
122
+
123
+ expect ( spy ) . toHaveBeenCalled ( ) ;
124
+ } ) ;
125
+
113
126
it ( 'should emit tabOut when the tab key is pressed with a modifier' , ( ) => {
114
127
const spy = jasmine . createSpy ( 'tabOut spy' ) ;
115
128
keyManager . tabOut . pipe ( take ( 1 ) ) . subscribe ( spy ) ;
@@ -122,27 +135,33 @@ describe('Key managers', () => {
122
135
123
136
it ( 'should emit an event whenever the active item changes' , ( ) => {
124
137
const spy = jasmine . createSpy ( 'change spy' ) ;
125
- const subscription = keyManager . change . subscribe ( spy ) ;
138
+ keyManager . change . subscribe ( spy ) ;
126
139
127
140
keyManager . onKeydown ( fakeKeyEvents . downArrow ) ;
128
141
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
129
142
130
143
keyManager . onKeydown ( fakeKeyEvents . upArrow ) ;
131
144
expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
132
-
133
- subscription . unsubscribe ( ) ;
134
145
} ) ;
135
146
136
147
it ( 'should emit if the active item changed, but not the active index' , ( ) => {
137
148
const spy = jasmine . createSpy ( 'change spy' ) ;
138
- const subscription = keyManager . change . subscribe ( spy ) ;
149
+ keyManager . change . subscribe ( spy ) ;
139
150
140
151
keyManager . setActiveItem ( 0 ) ;
141
152
itemList . reset ( [ new FakeFocusable ( 'zero' ) , ...itemList . toArray ( ) ] ) ;
142
153
keyManager . setActiveItem ( 0 ) ;
143
154
144
155
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
145
- subscription . unsubscribe ( ) ;
156
+ } ) ;
157
+
158
+ it ( 'should complete the change stream on destroy' , ( ) => {
159
+ const spy = jasmine . createSpy ( 'change spy' ) ;
160
+
161
+ keyManager . change . subscribe ( { complete : spy } ) ;
162
+ keyManager . destroy ( ) ;
163
+
164
+ expect ( spy ) . toHaveBeenCalled ( ) ;
146
165
} ) ;
147
166
148
167
it ( 'should activate the first item when pressing down on a clean key manager' , ( ) => {
@@ -448,7 +467,7 @@ describe('Key managers', () => {
448
467
) {
449
468
const initialActiveIndex = keyManager . activeItemIndex ;
450
469
const spy = jasmine . createSpy ( 'change spy' ) ;
451
- const subscription = keyManager . change . subscribe ( spy ) ;
470
+ keyManager . change . subscribe ( spy ) ;
452
471
453
472
expect ( context . nextKeyEvent . defaultPrevented ) . toBe ( false ) ;
454
473
expect ( context . prevKeyEvent . defaultPrevented ) . toBe ( false ) ;
@@ -465,8 +484,6 @@ describe('Key managers', () => {
465
484
expect ( context . prevKeyEvent . defaultPrevented ) . toBe ( false ) ;
466
485
expect ( keyManager . activeItemIndex ) . toBe ( initialActiveIndex ) ;
467
486
expect ( spy ) . not . toHaveBeenCalled ( ) ;
468
-
469
- subscription . unsubscribe ( ) ;
470
487
}
471
488
} ) ;
472
489
@@ -495,16 +512,14 @@ describe('Key managers', () => {
495
512
496
513
it ( 'should be able to set the active item without emitting an event' , ( ) => {
497
514
const spy = jasmine . createSpy ( 'change spy' ) ;
498
- const subscription = keyManager . change . subscribe ( spy ) ;
515
+ keyManager . change . subscribe ( spy ) ;
499
516
500
517
expect ( keyManager . activeItemIndex ) . toBe ( 0 ) ;
501
518
502
519
keyManager . updateActiveItem ( 2 ) ;
503
520
504
521
expect ( keyManager . activeItemIndex ) . toBe ( 2 ) ;
505
522
expect ( spy ) . not . toHaveBeenCalled ( ) ;
506
-
507
- subscription . unsubscribe ( ) ;
508
523
} ) ;
509
524
510
525
it ( 'should expose the active item correctly' , ( ) => {
@@ -629,14 +644,12 @@ describe('Key managers', () => {
629
644
630
645
it ( 'should not emit an event if the item did not change' , ( ) => {
631
646
const spy = jasmine . createSpy ( 'change spy' ) ;
632
- const subscription = keyManager . change . subscribe ( spy ) ;
647
+ keyManager . change . subscribe ( spy ) ;
633
648
634
649
keyManager . setActiveItem ( 2 ) ;
635
650
keyManager . setActiveItem ( 2 ) ;
636
651
637
652
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
638
-
639
- subscription . unsubscribe ( ) ;
640
653
} ) ;
641
654
} ) ;
642
655
@@ -935,6 +948,16 @@ describe('Key managers', () => {
935
948
936
949
expect ( keyManager . isTyping ( ) ) . toBe ( false ) ;
937
950
} ) ) ;
951
+
952
+ it ( 'should reset isTyping if the key manager is destroyed' , fakeAsync ( ( ) => {
953
+ expect ( keyManager . isTyping ( ) ) . toBe ( false ) ;
954
+
955
+ keyManager . onKeydown ( createKeyboardEvent ( 'keydown' , 79 , 'o' ) ) ; // types "o"
956
+ expect ( keyManager . isTyping ( ) ) . toBe ( true ) ;
957
+
958
+ keyManager . destroy ( ) ;
959
+ expect ( keyManager . isTyping ( ) ) . toBe ( false ) ;
960
+ } ) ) ;
938
961
} ) ;
939
962
} ) ;
940
963
@@ -953,6 +976,11 @@ describe('Key managers', () => {
953
976
spyOn ( itemList . toArray ( ) [ 2 ] , 'focus' ) ;
954
977
} ) ;
955
978
979
+ afterEach ( ( ) => {
980
+ keyManager . destroy ( ) ;
981
+ keyManager = null ! ;
982
+ } ) ;
983
+
956
984
it ( 'should focus subsequent items when down arrow is pressed' , ( ) => {
957
985
keyManager . onKeydown ( fakeKeyEvents . downArrow ) ;
958
986
0 commit comments