Skip to content

Commit 278e05d

Browse files
committed
refactor(multiple): avoid destroying and re-creating fixtures in tests (#34145)
We have some tests that were creating fixtures in `beforeEach` and assigning them to a variable. Then if a test needed a different fixture, we would destroy the previous one. This is unnecessary and can introduce bugs so these changes switch to creating the fixtures within the tests. (cherry picked from commit 58b4170)
1 parent a8808e2 commit 278e05d

20 files changed

Lines changed: 1551 additions & 928 deletions

File tree

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@ import {createOverlayRef} from '@angular/cdk/overlay';
1616
describe('LiveAnnouncer', () => {
1717
let announcer: LiveAnnouncer;
1818
let ariaLiveElement: Element;
19-
let fixture: ComponentFixture<TestApp>;
2019

2120
function wait(ms: number) {
2221
return new Promise(resolve => setTimeout(resolve, ms));
2322
}
2423

2524
describe('with default element', () => {
26-
beforeEach(async () => {
27-
announcer = TestBed.inject(LiveAnnouncer);
28-
ariaLiveElement = getLiveElement();
29-
fixture = TestBed.createComponent(TestApp);
30-
});
31-
3225
it('should correctly update the announce text', async () => {
26+
const fixture = createComponent();
3327
let buttonElement = fixture.debugElement.query(By.css('button'))!.nativeElement;
3428
buttonElement.click();
3529

@@ -40,6 +34,7 @@ describe('LiveAnnouncer', () => {
4034
});
4135

4236
it('should correctly update the politeness attribute', async () => {
37+
createComponent();
4338
announcer.announce('Hey Google', 'assertive');
4439

4540
// This flushes our 100ms timeout for the screenreaders.
@@ -50,6 +45,7 @@ describe('LiveAnnouncer', () => {
5045
});
5146

5247
it('should apply the aria-live value polite by default', async () => {
48+
createComponent();
5349
announcer.announce('Hey Google');
5450

5551
// This flushes our 100ms timeout for the screenreaders.
@@ -60,6 +56,7 @@ describe('LiveAnnouncer', () => {
6056
});
6157

6258
it('should be able to clear out the aria-live element manually', async () => {
59+
createComponent();
6360
announcer.announce('Hey Google');
6461
await wait(110);
6562
expect(ariaLiveElement.textContent).toBe('Hey Google');
@@ -69,6 +66,7 @@ describe('LiveAnnouncer', () => {
6966
});
7067

7168
it('should be able to clear out the aria-live element by setting a duration', async () => {
69+
createComponent();
7270
announcer.announce('Hey Google', 2000);
7371
await wait(110);
7472
expect(ariaLiveElement.textContent).toBe('Hey Google');
@@ -78,6 +76,7 @@ describe('LiveAnnouncer', () => {
7876
});
7977

8078
it('should clear the duration of previous messages when announcing a new one', async () => {
79+
createComponent();
8180
announcer.announce('Hey Google', 2000);
8281
await wait(100);
8382
expect(ariaLiveElement.textContent).toBe('Hey Google');
@@ -88,6 +87,7 @@ describe('LiveAnnouncer', () => {
8887
});
8988

9089
it('should remove the aria-live element from the DOM on destroy', async () => {
90+
createComponent();
9191
announcer.announce('Hey Google');
9292

9393
// This flushes our 100ms timeout for the screenreaders.
@@ -102,6 +102,7 @@ describe('LiveAnnouncer', () => {
102102
});
103103

104104
it('should return a promise that resolves after the text has been announced', async () => {
105+
createComponent();
105106
const spy = jasmine.createSpy('announce spy');
106107
announcer.announce('something').then(spy);
107108

@@ -111,6 +112,7 @@ describe('LiveAnnouncer', () => {
111112
});
112113

113114
it('should resolve the returned promise if another announcement is made before the timeout has expired', async () => {
115+
createComponent();
114116
const spy = jasmine.createSpy('announce spy');
115117
announcer.announce('something').then(spy);
116118
await wait(10);
@@ -121,15 +123,10 @@ describe('LiveAnnouncer', () => {
121123
});
122124

123125
it('should ensure that there is only one live element at a time', async () => {
124-
fixture.destroy();
125-
TestBed.resetTestingModule().configureTestingModule({});
126-
127126
const extraElement = document.createElement('div');
128127
extraElement.classList.add('cdk-live-announcer-element');
129128
document.body.appendChild(extraElement);
130-
announcer = TestBed.inject(LiveAnnouncer);
131-
ariaLiveElement = getLiveElement();
132-
fixture = TestBed.createComponent(TestApp);
129+
createComponent();
133130

134131
announcer.announce('Hey Google');
135132
await wait(110);
@@ -141,6 +138,7 @@ describe('LiveAnnouncer', () => {
141138
});
142139

143140
it('should clear any previous timers when a new one is started', async () => {
141+
createComponent();
144142
expect(ariaLiveElement.textContent).toBeFalsy();
145143

146144
announcer.announce('One');
@@ -157,6 +155,7 @@ describe('LiveAnnouncer', () => {
157155
});
158156

159157
it('should clear pending timeouts on destroy', async () => {
158+
createComponent();
160159
announcer.announce('Hey Google');
161160
announcer.ngOnDestroy();
162161

@@ -165,6 +164,7 @@ describe('LiveAnnouncer', () => {
165164
});
166165

167166
it('should add aria-owns to open aria-modal elements', async () => {
167+
const fixture = createComponent();
168168
const portal = new ComponentPortal(TestModal);
169169
const overlayRef = createOverlayRef(TestBed.inject(Injector));
170170
const componentRef = overlayRef.attach(portal);
@@ -185,6 +185,7 @@ describe('LiveAnnouncer', () => {
185185
});
186186

187187
it('should expand aria-owns of open aria-modal elements', async () => {
188+
const fixture = createComponent();
188189
const portal = new ComponentPortal(TestModal);
189190
const overlayRef = createOverlayRef(TestBed.inject(Injector));
190191
const componentRef = overlayRef.attach(portal);
@@ -208,6 +209,7 @@ describe('LiveAnnouncer', () => {
208209
});
209210

210211
it('should be able to announce safe HTML', async () => {
212+
const fixture = createComponent();
211213
const sanitizer = TestBed.inject(DomSanitizer);
212214
const message = sanitizer.bypassSecurityTrustHtml(
213215
'<span class="message" lang="fr">Bonjour</span>',
@@ -219,6 +221,12 @@ describe('LiveAnnouncer', () => {
219221

220222
expect(ariaLiveElement.querySelector('.message')?.textContent).toBe('Bonjour');
221223
});
224+
225+
function createComponent(): ComponentFixture<TestApp> {
226+
announcer = TestBed.inject(LiveAnnouncer);
227+
ariaLiveElement = getLiveElement();
228+
return TestBed.createComponent(TestApp);
229+
}
222230
});
223231

224232
describe('with a custom element', () => {

0 commit comments

Comments
 (0)