9
9
dispatchKeyboardEvent ,
10
10
dispatchMouseEvent ,
11
11
} from '@angular/cdk/testing/private' ;
12
- import { Component , Type , ViewChild , Provider , Directive } from '@angular/core' ;
12
+ import { Component , Type , ViewChild , Provider , Directive , ViewEncapsulation } from '@angular/core' ;
13
13
import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
14
14
import {
15
15
FormControl ,
@@ -27,6 +27,7 @@ import {
27
27
import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
28
28
import { DEC , JAN , JUL , JUN , SEP } from '@angular/material/testing' ;
29
29
import { By } from '@angular/platform-browser' ;
30
+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
30
31
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing' ;
31
32
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
32
33
import { MAT_DIALOG_DEFAULT_OPTIONS , MatDialogConfig } from '@angular/material/dialog' ;
@@ -1139,6 +1140,33 @@ describe('MatDatepicker', () => {
1139
1140
expect ( document . activeElement ) . toBe ( toggle , 'Expected focus to be restored to toggle.' ) ;
1140
1141
} ) ;
1141
1142
1143
+ it ( 'should restore focus when placed inside a shadow root' , ( ) => {
1144
+ if ( ! _supportsShadowDom ( ) ) {
1145
+ return ;
1146
+ }
1147
+
1148
+ fixture . destroy ( ) ;
1149
+ TestBed . resetTestingModule ( ) ;
1150
+ fixture = createComponent ( DatepickerWithToggleInShadowDom , [ MatNativeDateModule ] ) ;
1151
+ fixture . detectChanges ( ) ;
1152
+ testComponent = fixture . componentInstance ;
1153
+
1154
+ const toggle = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
1155
+ fixture . componentInstance . touchUI = false ;
1156
+ fixture . detectChanges ( ) ;
1157
+
1158
+ toggle . focus ( ) ;
1159
+ spyOn ( toggle , 'focus' ) . and . callThrough ( ) ;
1160
+ fixture . componentInstance . datepicker . open ( ) ;
1161
+ fixture . detectChanges ( ) ;
1162
+ fixture . componentInstance . datepicker . close ( ) ;
1163
+ fixture . detectChanges ( ) ;
1164
+
1165
+ // We have to assert by looking at the `focus` method, because
1166
+ // `document.activeElement` will return the shadow root.
1167
+ expect ( toggle . focus ) . toHaveBeenCalled ( ) ;
1168
+ } ) ;
1169
+
1142
1170
it ( 'should allow for focus restoration to be disabled' , ( ) => {
1143
1171
let toggle = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
1144
1172
@@ -2352,6 +2380,17 @@ class DatepickerWithToggle {
2352
2380
}
2353
2381
2354
2382
2383
+ @Component ( {
2384
+ encapsulation : ViewEncapsulation . ShadowDom ,
2385
+ template : `
2386
+ <input [matDatepicker]="d">
2387
+ <mat-datepicker-toggle [for]="d" [aria-label]="ariaLabel"></mat-datepicker-toggle>
2388
+ <mat-datepicker #d [touchUi]="touchUI" [restoreFocus]="restoreFocus"></mat-datepicker>
2389
+ ` ,
2390
+ } )
2391
+ class DatepickerWithToggleInShadowDom extends DatepickerWithToggle { }
2392
+
2393
+
2355
2394
@Component ( {
2356
2395
template : `
2357
2396
<input [matDatepicker]="d">
0 commit comments