@@ -10,7 +10,7 @@ import {MatListModule, MatListOption, MatSelectionList} from './index';
10
10
describe ( 'MatSelectionList' , ( ) => {
11
11
describe ( 'with list option' , ( ) => {
12
12
let fixture : ComponentFixture < SelectionListWithListOptions > ;
13
- let listOption : DebugElement [ ] ;
13
+ let listOptions : DebugElement [ ] ;
14
14
let listItemEl : DebugElement ;
15
15
let selectionList : DebugElement ;
16
16
@@ -33,14 +33,14 @@ describe('MatSelectionList', () => {
33
33
fixture = TestBed . createComponent ( SelectionListWithListOptions ) ;
34
34
fixture . detectChanges ( ) ;
35
35
36
- listOption = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
36
+ listOptions = fixture . debugElement . queryAll ( By . directive ( MatListOption ) ) ;
37
37
listItemEl = fixture . debugElement . query ( By . css ( '.mat-list-item' ) ) ;
38
38
selectionList = fixture . debugElement . query ( By . directive ( MatSelectionList ) ) ;
39
39
} ) ) ;
40
40
41
41
it ( 'should add and remove focus class on focus/blur' , ( ) => {
42
42
// Use the second list item, because the first one is always disabled.
43
- const listItem = listOption [ 1 ] . nativeElement ;
43
+ const listItem = listOptions [ 1 ] . nativeElement ;
44
44
45
45
expect ( listItem . classList ) . not . toContain ( 'mat-list-item-focus' ) ;
46
46
@@ -57,35 +57,35 @@ describe('MatSelectionList', () => {
57
57
const optionValues = [ 'inbox' , 'starred' , 'sent-mail' , 'drafts' ] ;
58
58
59
59
optionValues . forEach ( ( optionValue , index ) => {
60
- expect ( listOption [ index ] . componentInstance . value ) . toBe ( optionValue ) ;
60
+ expect ( listOptions [ index ] . componentInstance . value ) . toBe ( optionValue ) ;
61
61
} ) ;
62
62
} ) ;
63
63
64
64
it ( 'should be able to dispatch one selected item' , ( ) => {
65
- let testListItem = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
65
+ let testListItem = listOptions [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
66
66
let selectList =
67
67
selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
68
68
69
69
expect ( selectList . selected . length ) . toBe ( 0 ) ;
70
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
70
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
71
71
72
72
testListItem . toggle ( ) ;
73
73
fixture . detectChanges ( ) ;
74
74
75
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
76
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
75
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
76
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
77
77
expect ( selectList . selected . length ) . toBe ( 1 ) ;
78
78
} ) ;
79
79
80
80
it ( 'should be able to dispatch multiple selected items' , ( ) => {
81
- let testListItem = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
82
- let testListItem2 = listOption [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
81
+ let testListItem = listOptions [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
82
+ let testListItem2 = listOptions [ 1 ] . injector . get < MatListOption > ( MatListOption ) ;
83
83
let selectList =
84
84
selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
85
85
86
86
expect ( selectList . selected . length ) . toBe ( 0 ) ;
87
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
88
- expect ( listOption [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
87
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
88
+ expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'false' ) ;
89
89
90
90
testListItem . toggle ( ) ;
91
91
fixture . detectChanges ( ) ;
@@ -94,14 +94,14 @@ describe('MatSelectionList', () => {
94
94
fixture . detectChanges ( ) ;
95
95
96
96
expect ( selectList . selected . length ) . toBe ( 2 ) ;
97
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
98
- expect ( listOption [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
99
- expect ( listOption [ 1 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
100
- expect ( listOption [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
97
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
98
+ expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( 'true' ) ;
99
+ expect ( listOptions [ 1 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
100
+ expect ( listOptions [ 2 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
101
101
} ) ;
102
102
103
103
it ( 'should be able to deselect an option' , ( ) => {
104
- let testListItem = listOption [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
104
+ let testListItem = listOptions [ 2 ] . injector . get < MatListOption > ( MatListOption ) ;
105
105
let selectList =
106
106
selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
107
107
@@ -119,12 +119,12 @@ describe('MatSelectionList', () => {
119
119
} ) ;
120
120
121
121
it ( 'should not allow selection of disabled items' , ( ) => {
122
- let testListItem = listOption [ 0 ] . injector . get < MatListOption > ( MatListOption ) ;
122
+ let testListItem = listOptions [ 0 ] . injector . get < MatListOption > ( MatListOption ) ;
123
123
let selectList =
124
124
selectionList . injector . get < MatSelectionList > ( MatSelectionList ) . selectedOptions ;
125
125
126
126
expect ( selectList . selected . length ) . toBe ( 0 ) ;
127
- expect ( listOption [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
127
+ expect ( listOptions [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
128
128
129
129
testListItem . _handleClick ( ) ;
130
130
fixture . detectChanges ( ) ;
@@ -133,18 +133,18 @@ describe('MatSelectionList', () => {
133
133
} ) ;
134
134
135
135
it ( 'should be able to un-disable disabled items' , ( ) => {
136
- let testListItem = listOption [ 0 ] . injector . get < MatListOption > ( MatListOption ) ;
136
+ let testListItem = listOptions [ 0 ] . injector . get < MatListOption > ( MatListOption ) ;
137
137
138
- expect ( listOption [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
138
+ expect ( listOptions [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'true' ) ;
139
139
140
140
testListItem . disabled = false ;
141
141
fixture . detectChanges ( ) ;
142
142
143
- expect ( listOption [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
143
+ expect ( listOptions [ 0 ] . nativeElement . getAttribute ( 'aria-disabled' ) ) . toBe ( 'false' ) ;
144
144
} ) ;
145
145
146
146
it ( 'should be able to use keyboard select with SPACE' , ( ) => {
147
- let testListItem = listOption [ 1 ] . nativeElement as HTMLElement ;
147
+ let testListItem = listOptions [ 1 ] . nativeElement as HTMLElement ;
148
148
let SPACE_EVENT : KeyboardEvent =
149
149
createKeyboardEvent ( 'keydown' , SPACE , testListItem ) ;
150
150
let selectList =
@@ -162,7 +162,7 @@ describe('MatSelectionList', () => {
162
162
it ( 'should restore focus if active option is destroyed' , ( ) => {
163
163
const manager = selectionList . componentInstance . _keyManager ;
164
164
165
- listOption [ 3 ] . componentInstance . _handleFocus ( ) ;
165
+ listOptions [ 3 ] . componentInstance . _handleFocus ( ) ;
166
166
167
167
expect ( manager . activeItemIndex ) . toBe ( 3 ) ;
168
168
@@ -173,12 +173,12 @@ describe('MatSelectionList', () => {
173
173
} ) ;
174
174
175
175
it ( 'should focus previous item when press UP ARROW' , ( ) => {
176
- let testListItem = listOption [ 2 ] . nativeElement as HTMLElement ;
176
+ let testListItem = listOptions [ 2 ] . nativeElement as HTMLElement ;
177
177
let UP_EVENT : KeyboardEvent =
178
178
createKeyboardEvent ( 'keydown' , UP_ARROW , testListItem ) ;
179
179
let manager = selectionList . componentInstance . _keyManager ;
180
180
181
- dispatchFakeEvent ( listOption [ 2 ] . nativeElement , 'focus' ) ;
181
+ dispatchFakeEvent ( listOptions [ 2 ] . nativeElement , 'focus' ) ;
182
182
expect ( manager . activeItemIndex ) . toEqual ( 2 ) ;
183
183
184
184
selectionList . componentInstance . _keydown ( UP_EVENT ) ;
@@ -189,12 +189,12 @@ describe('MatSelectionList', () => {
189
189
} ) ;
190
190
191
191
it ( 'should focus next item when press DOWN ARROW' , ( ) => {
192
- let testListItem = listOption [ 2 ] . nativeElement as HTMLElement ;
192
+ let testListItem = listOptions [ 2 ] . nativeElement as HTMLElement ;
193
193
let DOWN_EVENT : KeyboardEvent =
194
194
createKeyboardEvent ( 'keydown' , DOWN_ARROW , testListItem ) ;
195
195
let manager = selectionList . componentInstance . _keyManager ;
196
196
197
- dispatchFakeEvent ( listOption [ 2 ] . nativeElement , 'focus' ) ;
197
+ dispatchFakeEvent ( listOptions [ 2 ] . nativeElement , 'focus' ) ;
198
198
expect ( manager . activeItemIndex ) . toEqual ( 2 ) ;
199
199
200
200
selectionList . componentInstance . _keydown ( DOWN_EVENT ) ;
@@ -226,6 +226,20 @@ describe('MatSelectionList', () => {
226
226
227
227
expect ( list . options . toArray ( ) . every ( option => option . selected ) ) . toBe ( false ) ;
228
228
} ) ;
229
+
230
+ it ( 'should update the list value when an item is selected programmatically' , ( ) => {
231
+ const list : MatSelectionList = selectionList . componentInstance ;
232
+
233
+ expect ( list . selectedOptions . isEmpty ( ) ) . toBe ( true ) ;
234
+
235
+ listOptions [ 0 ] . componentInstance . selected = true ;
236
+ listOptions [ 2 ] . componentInstance . selected = true ;
237
+ fixture . detectChanges ( ) ;
238
+
239
+ expect ( list . selectedOptions . isEmpty ( ) ) . toBe ( false ) ;
240
+ expect ( list . selectedOptions . isSelected ( listOptions [ 0 ] . componentInstance ) ) . toBe ( true ) ;
241
+ expect ( list . selectedOptions . isSelected ( listOptions [ 2 ] . componentInstance ) ) . toBe ( true ) ;
242
+ } ) ;
229
243
} ) ;
230
244
231
245
describe ( 'with list option selected' , ( ) => {
0 commit comments