Skip to content

Commit ec7d164

Browse files
authored
test(multiple): switch to non-deprecated test assertion context API (#23326)
Passing in a context as the last parameter of the various test assertion functions is deprecated. These changes switch all of the tests to the new `.withContext` helper.
1 parent c03d8ac commit ec7d164

File tree

110 files changed

+4424
-3017
lines changed

Some content is hidden

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

110 files changed

+4424
-3017
lines changed

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ describe('Dialog', () => {
209209

210210
// beforeClose should emit before dialog container is destroyed
211211
const beforeCloseHandler = jasmine.createSpy('beforeClose callback').and.callFake(() => {
212-
expect(overlayContainerElement.querySelector('cdk-dialog-container'))
213-
.not.toBeNull('dialog container exists when beforeClose is called');
212+
expect(overlayContainerElement.querySelector('cdk-dialog-container')).not
213+
.withContext('dialog container exists when beforeClose is called').toBeNull();
214214
});
215215

216216
dialogRef.beforeClosed().subscribe(beforeCloseHandler);
@@ -261,15 +261,15 @@ describe('Dialog', () => {
261261
flushMicrotasks();
262262

263263
expect(overlayContainerElement.querySelectorAll('cdk-dialog-container').length)
264-
.toBe(1, 'Expected one open dialog.');
264+
.withContext('Expected one open dialog.').toBe(1);
265265

266266
dialogRef.close();
267267
flushMicrotasks();
268268
onPushFixture.detectChanges();
269269
tick(500);
270270

271271
expect(overlayContainerElement.querySelectorAll('cdk-dialog-container').length)
272-
.toBe(0, 'Expected no open dialogs.');
272+
.withContext('Expected no open dialogs.').toBe(0);
273273
}));
274274

275275
it('should close when clicking on the overlay backdrop', fakeAsync(() => {
@@ -403,8 +403,8 @@ describe('Dialog', () => {
403403

404404
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
405405

406-
expect(overlayPane.style.maxWidth).toBe('80vw',
407-
'Expected dialog to set a default max-width on overlay pane');
406+
expect(overlayPane.style.maxWidth)
407+
.withContext('Expected dialog to set a default max-width on overlay pane').toBe('80vw');
408408

409409
dialogRef.close();
410410

@@ -716,7 +716,8 @@ describe('Dialog', () => {
716716
viewContainerFixture.detectChanges();
717717
flush();
718718

719-
expect(dialogRef.componentInstance).toBeFalsy('Expected reference to have been cleared.');
719+
expect(dialogRef.componentInstance)
720+
.withContext('Expected reference to have been cleared.').toBeFalsy();
720721
}));
721722

722723
it('should assign a unique id to each dialog', () => {
@@ -892,7 +893,8 @@ describe('Dialog', () => {
892893
flushMicrotasks();
893894

894895
expect(document.activeElement!.tagName)
895-
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
896+
.withContext('Expected first tabbable element (input) in the dialog to be focused.')
897+
.toBe('INPUT');
896898
}));
897899

898900
it('should focus the dialog element on open', fakeAsync(() => {
@@ -907,7 +909,8 @@ describe('Dialog', () => {
907909
let container =
908910
overlayContainerElement.querySelector('cdk-dialog-container') as HTMLInputElement;
909911

910-
expect(document.activeElement).toBe(container, 'Expected container to be focused on open');
912+
expect(document.activeElement)
913+
.withContext('Expected container to be focused on open').toBe(container);
911914
}));
912915

913916
it('should focus the first header element on open', fakeAsync(() => {
@@ -923,7 +926,7 @@ describe('Dialog', () => {
923926
overlayContainerElement.querySelector('h1[tabindex="-1"]') as HTMLInputElement;
924927

925928
expect(document.activeElement)
926-
.toBe(firstHeader, 'Expected first header to be focused on open');
929+
.withContext('Expected first header to be focused on open').toBe(firstHeader);
927930
}));
928931

929932
it('should focus the first element that matches the css selector from autoFocus on open',
@@ -940,7 +943,7 @@ describe('Dialog', () => {
940943
overlayContainerElement.querySelector('p[tabindex="-1"]') as HTMLInputElement;
941944

942945
expect(document.activeElement)
943-
.toBe(firstParagraph, 'Expected first paragraph to be focused on open');
946+
.withContext('Expected first paragraph to be focused on open').toBe(firstParagraph);
944947
}));
945948

946949
it('should re-focus trigger element when dialog closes', fakeAsync(() => {
@@ -968,8 +971,9 @@ describe('Dialog', () => {
968971
viewContainerFixture.detectChanges();
969972
flush();
970973

971-
expect(document.activeElement!.id).toBe('dialog-trigger',
972-
'Expected that the trigger was refocused after the dialog is closed.');
974+
expect(document.activeElement!.id)
975+
.withContext('Expected that the trigger was refocused after the dialog is closed.')
976+
.toBe('dialog-trigger');
973977

974978
document.body.removeChild(button);
975979
}));
@@ -1027,14 +1031,14 @@ describe('Dialog', () => {
10271031
otherButton.focus();
10281032

10291033
expect(document.activeElement!.id)
1030-
.toBe('other-button', 'Expected focus to be on the alternate button.');
1034+
.withContext('Expected focus to be on the alternate button.').toBe('other-button');
10311035

10321036
flushMicrotasks();
10331037
viewContainerFixture.detectChanges();
10341038
flush();
10351039

10361040
expect(document.activeElement!.id)
1037-
.toBe('other-button', 'Expected focus to stay on the alternate button.');
1041+
.withContext('Expected focus to stay on the alternate button.').toBe('other-button');
10381042

10391043
body.removeChild(button);
10401044
body.removeChild(otherButton);
@@ -1065,8 +1069,9 @@ describe('Dialog', () => {
10651069
viewContainerFixture.detectChanges();
10661070
flushMicrotasks();
10671071

1068-
expect(document.activeElement!.id).toBe('input-to-be-focused',
1069-
'Expected that the trigger was refocused after the dialog is closed.');
1072+
expect(document.activeElement!.id)
1073+
.withContext('Expected that the trigger was refocused after the dialog is closed.')
1074+
.toBe('input-to-be-focused');
10701075

10711076
document.body.removeChild(button);
10721077
document.body.removeChild(input);
@@ -1081,7 +1086,7 @@ describe('Dialog', () => {
10811086
flushMicrotasks();
10821087

10831088
expect(document.activeElement!.tagName.toLowerCase())
1084-
.toBe('cdk-dialog-container', 'Expected dialog container to be focused.');
1089+
.withContext('Expected dialog container to be focused.').toBe('cdk-dialog-container');
10851090
}));
10861091

10871092
});
@@ -1154,14 +1159,14 @@ describe('Dialog with a parent Dialog', () => {
11541159
flush();
11551160

11561161
expect(overlayContainerElement.textContent)
1157-
.toContain('Pizza', 'Expected a dialog to be opened');
1162+
.withContext('Expected a dialog to be opened').toContain('Pizza');
11581163

11591164
childDialog.closeAll();
11601165
fixture.detectChanges();
11611166
flush();
11621167

11631168
expect(overlayContainerElement.textContent!.trim())
1164-
.toBe('', 'Expected closeAll on child Dialog to close dialog opened by parent');
1169+
.withContext('Expected closeAll on child Dialog to close dialog opened by parent').toBe('');
11651170
}));
11661171

11671172
it('should close dialogs opened by a child when calling closeAll on a parent Dialog',
@@ -1170,14 +1175,14 @@ describe('Dialog with a parent Dialog', () => {
11701175
fixture.detectChanges();
11711176

11721177
expect(overlayContainerElement.textContent)
1173-
.toContain('Pizza', 'Expected a dialog to be opened');
1178+
.withContext('Expected a dialog to be opened').toContain('Pizza');
11741179

11751180
parentDialog.closeAll();
11761181
fixture.detectChanges();
11771182
flush();
11781183

11791184
expect(overlayContainerElement.textContent!.trim())
1180-
.toBe('', 'Expected closeAll on parent Dialog to close dialog opened by child');
1185+
.withContext('Expected closeAll on parent Dialog to close dialog opened by child').toBe('');
11811186
}));
11821187

11831188
it('should not close the parent dialogs, when a child is destroyed', fakeAsync(() => {
@@ -1186,14 +1191,14 @@ describe('Dialog with a parent Dialog', () => {
11861191
flush();
11871192

11881193
expect(overlayContainerElement.textContent)
1189-
.toContain('Pizza', 'Expected a dialog to be opened');
1194+
.withContext('Expected a dialog to be opened').toContain('Pizza');
11901195

11911196
childDialog.ngOnDestroy();
11921197
fixture.detectChanges();
11931198
flush();
11941199

11951200
expect(overlayContainerElement.textContent)
1196-
.toContain('Pizza', 'Expected a dialog to remain opened');
1201+
.withContext('Expected a dialog to remain opened').toContain('Pizza');
11971202
}));
11981203

11991204
it('should close the top dialog via the escape key', fakeAsync(() => {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,23 @@ describe('CdkOption and CdkListbox', () => {
620620

621621
it('should be able to set the disabled state via setDisabledState', () => {
622622
expect(listboxInstance.disabled)
623-
.toBe(false, 'Expected the selection list to be enabled.');
623+
.withContext('Expected the selection list to be enabled.').toBe(false);
624624
expect(optionInstances.every(option => !option.disabled))
625-
.toBe(true, 'Expected every list option to be enabled.');
625+
.withContext('Expected every list option to be enabled.').toBe(true);
626626

627627
listboxInstance.setDisabledState(true);
628628
fixture.detectChanges();
629629

630630
expect(listboxInstance.disabled)
631-
.toBe(true, 'Expected the selection list to be disabled.');
631+
.withContext('Expected the selection list to be disabled.').toBe(true);
632632
for (const option of optionElements) {
633633
expect(option.getAttribute('aria-disabled')).toBe('true');
634634
}
635635
});
636636

637637
it('should be able to select options via writeValue', () => {
638638
expect(optionInstances.every(option => !option.disabled))
639-
.toBe(true, 'Expected every list option to be enabled.');
639+
.withContext('Expected every list option to be enabled.').toBe(true);
640640

641641
listboxInstance.writeValue('arc');
642642
fixture.detectChanges();
@@ -651,7 +651,7 @@ describe('CdkOption and CdkListbox', () => {
651651

652652
it('should be select multiple options by their values', () => {
653653
expect(optionInstances.every(option => !option.disabled))
654-
.toBe(true, 'Expected every list option to be enabled.');
654+
.withContext('Expected every list option to be enabled.').toBe(true);
655655

656656
testComponent.isMultiselectable = true;
657657
fixture.detectChanges();
@@ -676,7 +676,7 @@ describe('CdkOption and CdkListbox', () => {
676676
it('should be able to disable options from the control', () => {
677677
expect(testComponent.listbox.disabled).toBeFalse();
678678
expect(optionInstances.every(option => !option.disabled))
679-
.toBe(true, 'Expected every list option to be enabled.');
679+
.withContext('Expected every list option to be enabled.').toBe(true);
680680

681681
testComponent.form.disable();
682682
fixture.detectChanges();
@@ -690,7 +690,7 @@ describe('CdkOption and CdkListbox', () => {
690690
it('should be able to toggle disabled state after form control is disabled', () => {
691691
expect(testComponent.listbox.disabled).toBeFalse();
692692
expect(optionInstances.every(option => !option.disabled))
693-
.toBe(true, 'Expected every list option to be enabled.');
693+
.withContext('Expected every list option to be enabled.').toBe(true);
694694

695695
testComponent.form.disable();
696696
fixture.detectChanges();
@@ -705,7 +705,7 @@ describe('CdkOption and CdkListbox', () => {
705705

706706
expect(testComponent.listbox.disabled).toBeFalse();
707707
expect(optionInstances.every(option => !option.disabled))
708-
.toBe(true, 'Expected every list option to be enabled.');
708+
.withContext('Expected every list option to be enabled.').toBe(true);
709709
});
710710

711711
it('should be able to select options via setting the value in form control', () => {

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('CdkVirtualScrollViewport', () => {
2929
const contentWrapper =
3030
viewport.elementRef.nativeElement.querySelector('.cdk-virtual-scroll-content-wrapper')!;
3131
expect(contentWrapper.children.length)
32-
.toBe(4, 'should render 4 50px items to fill 200px space');
32+
.withContext('should render 4 50px items to fill 200px space').toBe(4);
3333
}));
3434

3535
it('should render extra content if first item is smaller than average', fakeAsync(() => {
@@ -38,8 +38,9 @@ describe('CdkVirtualScrollViewport', () => {
3838

3939
const contentWrapper =
4040
viewport.elementRef.nativeElement.querySelector('.cdk-virtual-scroll-content-wrapper')!;
41-
expect(contentWrapper.children.length).toBe(4,
42-
'should render 4 items to fill 200px space based on 50px estimate from first item');
41+
expect(contentWrapper.children.length)
42+
.withContext('should render 4 items to fill 200px space based on 50px ' +
43+
'estimate from first item').toBe(4);
4344
}));
4445

4546
it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {

src/cdk/a11y/aria-describer/aria-describer.spec.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,44 @@ describe('AriaDescriber', () => {
128128
const descriptionNode = fixture.nativeElement.querySelector('#description-with-existing-id');
129129

130130
expect(document.body.contains(descriptionNode))
131-
.toBe(true, 'Expected node to be inside the document to begin with.');
132-
expect(getMessagesContainer()).toBeNull('Expected no messages container on init.');
131+
.withContext('Expected node to be inside the document to begin with.')
132+
.toBe(true);
133+
expect(getMessagesContainer())
134+
.withContext('Expected no messages container on init.').toBeNull();
133135

134136
ariaDescriber.describe(component.element1, descriptionNode);
135137

136138
expectMessage(component.element1, 'Hello');
137139
expect(getMessagesContainer())
138-
.toBeNull('Expected no messages container after the element was described.');
140+
.withContext('Expected no messages container after the element was described.')
141+
.toBeNull();
139142

140143
ariaDescriber.removeDescription(component.element1, descriptionNode);
141144

142-
expect(document.body.contains(descriptionNode)).toBe(true,
143-
'Expected description node to still be in the DOM after it is no longer being used.');
145+
expect(document.body.contains(descriptionNode))
146+
.withContext('Expected description node to still be in the DOM after it is' +
147+
'no longer being used.').toBe(true);
144148
});
145149

146150
it('should keep nodes set as descriptions inside their original position in the DOM', () => {
147151
createFixture();
148152
const descriptionNode = fixture.nativeElement.querySelector('#description-with-existing-id');
149153
const initialParent = descriptionNode.parentNode;
150154

151-
expect(initialParent).toBeTruthy('Expected node to have a parent initially.');
155+
expect(initialParent).withContext('Expected node to have a parent initially.').toBeTruthy();
152156

153157
ariaDescriber.describe(component.element1, descriptionNode);
154158

155159
expectMessage(component.element1, 'Hello');
156-
expect(descriptionNode.parentNode).toBe(initialParent,
157-
'Expected node to stay inside the same parent when used as a description.');
160+
expect(descriptionNode.parentNode)
161+
.withContext('Expected node to stay inside the same parent when used as a description.')
162+
.toBe(initialParent);
158163

159164
ariaDescriber.removeDescription(component.element1, descriptionNode);
160165

161-
expect(descriptionNode.parentNode).toBe(initialParent,
162-
'Expected node to stay inside the same parent after not being used as a description.');
166+
expect(descriptionNode.parentNode)
167+
.withContext('Expected node to stay inside the same parent after not ' +
168+
'being used as a description.').toBe(initialParent);
163169
});
164170

165171
it('should be able to unregister messages while having others registered', () => {
@@ -247,7 +253,8 @@ describe('AriaDescriber', () => {
247253
const descriptionNode = fixture.nativeElement.querySelector('#description-with-existing-id');
248254

249255
expect(document.body.contains(descriptionNode))
250-
.toBe(true, 'Expected node to be inside the document to begin with.');
256+
.withContext('Expected node to be inside the document to begin with.')
257+
.toBe(true);
251258

252259
ariaDescriber.describe(component.element1, descriptionNode);
253260

@@ -257,8 +264,9 @@ describe('AriaDescriber', () => {
257264
ariaDescriber.ngOnDestroy();
258265

259266
expect(component.element1.hasAttribute(CDK_DESCRIBEDBY_HOST_ATTRIBUTE)).toBe(false);
260-
expect(document.body.contains(descriptionNode)).toBe(true,
261-
'Expected description node to still be in the DOM after it is no longer being used.');
267+
expect(document.body.contains(descriptionNode))
268+
.withContext('Expected description node to still be in the DOM after ' +
269+
'it is no longer being used.').toBe(true);
262270
});
263271

264272
it('should remove the aria-describedby attribute if there are no more messages', () => {

0 commit comments

Comments
 (0)