Skip to content

Commit e14df64

Browse files
lacolacojelbourn
authored andcommitted
refactor: debugElement.query is nullable (#16917)
1 parent ff94f9d commit e14df64

File tree

61 files changed

+733
-701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+733
-701
lines changed

src/cdk-experimental/dialog/dialog.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ describe('Dialog', () => {
574574
it('should set the proper animation states', () => {
575575
let dialogRef = dialog.openFromComponent(PizzaMsg, { viewContainerRef: testViewContainerRef });
576576
let dialogContainer: CdkDialogContainer =
577-
viewContainerFixture.debugElement.query(By.directive(CdkDialogContainer)).componentInstance;
577+
viewContainerFixture.debugElement.query(By.directive(CdkDialogContainer))!
578+
.componentInstance;
578579

579580
expect(dialogContainer._state).toBe('enter');
580581

src/cdk/a11y/focus-monitor/focus-monitor.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('FocusMonitor', () => {
3131
fixture = TestBed.createComponent(PlainButton);
3232
fixture.detectChanges();
3333

34-
buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
34+
buttonElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
3535
focusMonitor = fm;
3636

3737
changeHandler = jasmine.createSpy('focus origin change handler');
@@ -264,7 +264,7 @@ describe('cdkMonitorFocus', () => {
264264
fixture.detectChanges();
265265

266266
spyOn(fixture.componentInstance, 'focusChanged');
267-
buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
267+
buttonElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
268268
patchElementFocus(buttonElement);
269269
});
270270

@@ -362,8 +362,8 @@ describe('cdkMonitorFocus', () => {
362362
fixture = TestBed.createComponent(ComplexComponentWithMonitorElementFocus);
363363
fixture.detectChanges();
364364

365-
parentElement = fixture.debugElement.query(By.css('div')).nativeElement;
366-
childElement = fixture.debugElement.query(By.css('button')).nativeElement;
365+
parentElement = fixture.debugElement.query(By.css('div'))!.nativeElement;
366+
childElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
367367

368368
patchElementFocus(parentElement);
369369
patchElementFocus(childElement);
@@ -395,8 +395,8 @@ describe('cdkMonitorFocus', () => {
395395
fixture = TestBed.createComponent(ComplexComponentWithMonitorSubtreeFocus);
396396
fixture.detectChanges();
397397

398-
parentElement = fixture.debugElement.query(By.css('div')).nativeElement;
399-
childElement = fixture.debugElement.query(By.css('button')).nativeElement;
398+
parentElement = fixture.debugElement.query(By.css('div'))!.nativeElement;
399+
childElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
400400

401401
patchElementFocus(parentElement);
402402
patchElementFocus(childElement);
@@ -431,8 +431,8 @@ describe('cdkMonitorFocus', () => {
431431
TestBed.createComponent(ComplexComponentWithMonitorSubtreeFocusAndMonitorElementFocus);
432432
fixture.detectChanges();
433433

434-
parentElement = fixture.debugElement.query(By.css('div')).nativeElement;
435-
childElement = fixture.debugElement.query(By.css('button')).nativeElement;
434+
parentElement = fixture.debugElement.query(By.css('div'))!.nativeElement;
435+
childElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
436436

437437
patchElementFocus(parentElement);
438438
patchElementFocus(childElement);

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('LiveAnnouncer', () => {
3535
});
3636

3737
it('should correctly update the announce text', fakeAsync(() => {
38-
let buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
38+
let buttonElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
3939
buttonElement.click();
4040

4141
// This flushes our 100ms timeout for the screenreaders.

src/cdk/accordion/accordion-item.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('CdkAccordionItem', () => {
2727
beforeEach(() => {
2828
fixture = TestBed.createComponent(SingleItem);
2929
item = fixture.debugElement
30-
.query(By.directive(CdkAccordionItem))
30+
.query(By.directive(CdkAccordionItem))!
3131
.injector.get<CdkAccordionItem>(CdkAccordionItem);
3232
});
3333

src/cdk/bidi/directionality.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Directionality', () => {
7575
it('should provide itself as Directionality', () => {
7676
const fixture = TestBed.createComponent(ElementWithDir);
7777
const injectedDirectionality =
78-
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
78+
fixture.debugElement.query(By.directive(InjectsDirectionality))!.componentInstance.dir;
7979

8080
fixture.detectChanges();
8181

@@ -85,7 +85,7 @@ describe('Directionality', () => {
8585
it('should emit a change event when the value changes', fakeAsync(() => {
8686
const fixture = TestBed.createComponent(ElementWithDir);
8787
const injectedDirectionality =
88-
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
88+
fixture.debugElement.query(By.directive(InjectsDirectionality))!.componentInstance.dir;
8989

9090
fixture.detectChanges();
9191

@@ -108,7 +108,7 @@ describe('Directionality', () => {
108108
it('should complete the change stream on destroy', fakeAsync(() => {
109109
const fixture = TestBed.createComponent(ElementWithDir);
110110
const dir =
111-
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
111+
fixture.debugElement.query(By.directive(InjectsDirectionality))!.componentInstance.dir;
112112
const spy = jasmine.createSpy('complete spy');
113113
const subscription = dir.change.subscribe(undefined, undefined, spy);
114114

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ describe('Overlay directives', () => {
294294
});
295295

296296
it('should set the offsetY', () => {
297-
const trigger = fixture.debugElement.query(By.css('button')).nativeElement;
297+
const trigger = fixture.debugElement.query(By.css('button'))!.nativeElement;
298298
trigger.style.position = 'absolute';
299299
trigger.style.top = '30px';
300300
trigger.style.height = '20px';

src/cdk/text-field/autosize.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('CdkTextareaAutosize', () => {
3737

3838
textarea = fixture.nativeElement.querySelector('textarea');
3939
autosize = fixture.debugElement.query(
40-
By.directive(CdkTextareaAutosize)).injector.get<CdkTextareaAutosize>(CdkTextareaAutosize);
40+
By.directive(CdkTextareaAutosize))!.injector.get<CdkTextareaAutosize>(CdkTextareaAutosize);
4141
});
4242

4343
it('should resize the textarea based on its content', () => {
@@ -173,7 +173,7 @@ describe('CdkTextareaAutosize', () => {
173173
// detection should be triggered after a multiline content is set.
174174
fixture = TestBed.createComponent(AutosizeTextAreaWithContent);
175175
textarea = fixture.nativeElement.querySelector('textarea');
176-
autosize = fixture.debugElement.query(By.css('textarea'))
176+
autosize = fixture.debugElement.query(By.css('textarea'))!
177177
.injector.get<CdkTextareaAutosize>(CdkTextareaAutosize);
178178

179179
fixture.componentInstance.content = `
@@ -239,7 +239,7 @@ describe('CdkTextareaAutosize', () => {
239239
it('should not trigger a resize when it is disabled', fakeAsync(() => {
240240
const fixtureWithoutAutosize = TestBed.createComponent(AutosizeTextareaWithoutAutosize);
241241
textarea = fixtureWithoutAutosize.nativeElement.querySelector('textarea');
242-
autosize = fixtureWithoutAutosize.debugElement.query(By.css('textarea'))
242+
autosize = fixtureWithoutAutosize.debugElement.query(By.css('textarea'))!
243243
.injector.get<CdkTextareaAutosize>(CdkTextareaAutosize);
244244

245245
fixtureWithoutAutosize.detectChanges();

src/google-maps/google-map/google-map.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('GoogleMap', () => {
6969
const fixture = TestBed.createComponent(TestApp);
7070
fixture.detectChanges();
7171

72-
const container = fixture.debugElement.query(By.css('div'));
72+
const container = fixture.debugElement.query(By.css('div'))!;
7373
expect(container.nativeElement.style.height).toBe(DEFAULT_HEIGHT);
7474
expect(container.nativeElement.style.width).toBe(DEFAULT_WIDTH);
7575
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, DEFAULT_OPTIONS);
@@ -84,7 +84,7 @@ describe('GoogleMap', () => {
8484
fixture.componentInstance.width = '400px';
8585
fixture.detectChanges();
8686

87-
const container = fixture.debugElement.query(By.css('div'));
87+
const container = fixture.debugElement.query(By.css('div'))!;
8888
expect(container.nativeElement.style.height).toBe('750px');
8989
expect(container.nativeElement.style.width).toBe('400px');
9090
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, DEFAULT_OPTIONS);
@@ -107,7 +107,7 @@ describe('GoogleMap', () => {
107107
fixture.componentInstance.zoom = options.zoom;
108108
fixture.detectChanges();
109109

110-
const container = fixture.debugElement.query(By.css('div'));
110+
const container = fixture.debugElement.query(By.css('div'))!;
111111
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, options);
112112

113113
fixture.componentInstance.center = {lat: 8, lng: 9};
@@ -126,7 +126,7 @@ describe('GoogleMap', () => {
126126
fixture.componentInstance.options = options;
127127
fixture.detectChanges();
128128

129-
const container = fixture.debugElement.query(By.css('div'));
129+
const container = fixture.debugElement.query(By.css('div'))!;
130130
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, options);
131131

132132
fixture.componentInstance.options = {...options, heading: 170};
@@ -147,7 +147,7 @@ describe('GoogleMap', () => {
147147
fixture.componentInstance.options = inputOptions;
148148
fixture.detectChanges();
149149

150-
const container = fixture.debugElement.query(By.css('div'));
150+
const container = fixture.debugElement.query(By.css('div'))!;
151151
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, correctedOptions);
152152
});
153153

src/material-experimental/mdc-checkbox/checkbox.spec.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('MatCheckbox', () => {
4444
fixture = createComponent(SingleCheckbox);
4545
fixture.detectChanges();
4646

47-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
47+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
4848
checkboxNativeElement = checkboxDebugElement.nativeElement;
4949
checkboxInstance = checkboxDebugElement.componentInstance;
5050
testComponent = fixture.debugElement.componentInstance;
@@ -480,7 +480,7 @@ describe('MatCheckbox', () => {
480480
fixture = createComponent(SingleCheckbox);
481481
fixture.detectChanges();
482482

483-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
483+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
484484
checkboxNativeElement = checkboxDebugElement.nativeElement;
485485
checkboxInstance = checkboxDebugElement.componentInstance;
486486
testComponent = fixture.debugElement.componentInstance;
@@ -512,7 +512,7 @@ describe('MatCheckbox', () => {
512512
fixture = createComponent(SingleCheckbox);
513513
fixture.detectChanges();
514514

515-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
515+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
516516
checkboxNativeElement = checkboxDebugElement.nativeElement;
517517
checkboxInstance = checkboxDebugElement.componentInstance;
518518
testComponent = fixture.debugElement.componentInstance;
@@ -564,7 +564,7 @@ describe('MatCheckbox', () => {
564564
fixture = createComponent(CheckboxWithChangeEvent);
565565
fixture.detectChanges();
566566

567-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
567+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
568568
checkboxNativeElement = checkboxDebugElement.nativeElement;
569569
checkboxInstance = checkboxDebugElement.componentInstance;
570570
testComponent = fixture.debugElement.componentInstance;
@@ -614,7 +614,7 @@ describe('MatCheckbox', () => {
614614

615615
it('should use the provided aria-label', fakeAsync(() => {
616616
fixture = createComponent(CheckboxWithAriaLabel);
617-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
617+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
618618
checkboxNativeElement = checkboxDebugElement.nativeElement;
619619
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
620620

@@ -638,7 +638,7 @@ describe('MatCheckbox', () => {
638638

639639
it('should use the provided aria-labelledby', fakeAsync(() => {
640640
fixture = createComponent(CheckboxWithAriaLabelledby);
641-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
641+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
642642
checkboxNativeElement = checkboxDebugElement.nativeElement;
643643
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
644644

@@ -648,7 +648,7 @@ describe('MatCheckbox', () => {
648648

649649
it('should not assign aria-labelledby if none is provided', fakeAsync(() => {
650650
fixture = createComponent(SingleCheckbox);
651-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
651+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
652652
checkboxNativeElement = checkboxDebugElement.nativeElement;
653653
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
654654

@@ -668,7 +668,7 @@ describe('MatCheckbox', () => {
668668
fixture.detectChanges();
669669

670670
testComponent = fixture.debugElement.componentInstance;
671-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
671+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
672672
checkboxNativeElement = checkboxDebugElement.nativeElement;
673673
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
674674
});
@@ -698,7 +698,8 @@ describe('MatCheckbox', () => {
698698
fixture.detectChanges();
699699

700700
const checkbox =
701-
fixture.debugElement.query(By.directive(MatCheckbox)).componentInstance as MatCheckbox;
701+
fixture.debugElement.query(By.directive(MatCheckbox))!
702+
.componentInstance as MatCheckbox;
702703

703704
expect(checkbox.tabIndex)
704705
.toBe(5, 'Expected tabIndex property to have been set based on the native attribute');
@@ -708,7 +709,7 @@ describe('MatCheckbox', () => {
708709
fixture = createComponent(CheckboxWithTabindexAttr);
709710
fixture.detectChanges();
710711

711-
const checkbox = fixture.debugElement.query(By.directive(MatCheckbox)).nativeElement;
712+
const checkbox = fixture.debugElement.query(By.directive(MatCheckbox))!.nativeElement;
712713
expect(checkbox.getAttribute('tabindex')).toBeFalsy();
713714
}));
714715
});
@@ -743,7 +744,7 @@ describe('MatCheckbox', () => {
743744
fixture.componentInstance.isRequired = false;
744745
fixture.detectChanges();
745746

746-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
747+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
747748
checkboxNativeElement = checkboxDebugElement.nativeElement;
748749
checkboxInstance = checkboxDebugElement.componentInstance;
749750
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
@@ -785,7 +786,7 @@ describe('MatCheckbox', () => {
785786
fixture = createComponent(CheckboxWithNgModelAndOnPush);
786787
fixture.detectChanges();
787788

788-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
789+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
789790
checkboxNativeElement = checkboxDebugElement.nativeElement;
790791
checkboxInstance = checkboxDebugElement.componentInstance;
791792
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
@@ -860,7 +861,7 @@ describe('MatCheckbox', () => {
860861
});
861862

862863
it('should forward name value to input element', fakeAsync(() => {
863-
let checkboxElement = fixture.debugElement.query(By.directive(MatCheckbox));
864+
let checkboxElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
864865
let inputElement = <HTMLInputElement>checkboxElement.nativeElement.querySelector('input');
865866

866867
expect(inputElement.getAttribute('name')).toBe('test-name');
@@ -877,7 +878,7 @@ describe('MatCheckbox', () => {
877878
fixture = createComponent(CheckboxWithFormControl);
878879
fixture.detectChanges();
879880

880-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
881+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
881882
checkboxInstance = checkboxDebugElement.componentInstance;
882883
testComponent = fixture.debugElement.componentInstance;
883884
inputElement = <HTMLInputElement>checkboxDebugElement.nativeElement.querySelector('input');
@@ -905,8 +906,8 @@ describe('MatCheckbox', () => {
905906

906907
beforeEach(() => {
907908
fixture = createComponent(CheckboxWithoutLabel);
908-
const checkboxDebugEl = fixture.debugElement.query(By.directive(MatCheckbox));
909-
checkboxInnerContainer = checkboxDebugEl.query(By.css('.mdc-form-field')).nativeElement;
909+
const checkboxDebugEl = fixture.debugElement.query(By.directive(MatCheckbox))!;
910+
checkboxInnerContainer = checkboxDebugEl.query(By.css('.mdc-form-field'))!.nativeElement;
910911
});
911912

912913
it('should not add the "name" attribute if it is not passed in', fakeAsync(() => {

0 commit comments

Comments
 (0)