Skip to content

Commit 26bbeb2

Browse files
crisbetotinayuangao
authored andcommitted
build: fix safari test failures (#7785)
Fixes a few obscure test failures on iOS Safari.
1 parent 9673f63 commit 26bbeb2

File tree

3 files changed

+63
-99
lines changed

3 files changed

+63
-99
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, ViewChild} from '@angular/core';
22
import {By} from '@angular/platform-browser';
3-
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
44
import {Directionality} from '@angular/cdk/bidi';
55
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
66
import {ESCAPE} from '@angular/cdk/keycodes';
@@ -11,6 +11,7 @@ import {ConnectedOverlayPositionChange} from './position/connected-position';
1111

1212

1313
describe('Overlay directives', () => {
14+
let overlayContainer: OverlayContainer;
1415
let overlayContainerElement: HTMLElement;
1516
let fixture: ComponentFixture<ConnectedOverlayDirectiveTest>;
1617
let dir: {value: string};
@@ -20,12 +21,8 @@ describe('Overlay directives', () => {
2021
imports: [OverlayModule],
2122
declarations: [ConnectedOverlayDirectiveTest, ConnectedOverlayPropertyInitOrder],
2223
providers: [
23-
{provide: OverlayContainer, useFactory: () => {
24-
overlayContainerElement = document.createElement('div');
25-
return {getContainerElement: () => overlayContainerElement};
26-
}},
2724
{provide: Directionality, useFactory: () => {
28-
return dir = { value: 'ltr' };
25+
return dir = {value: 'ltr'};
2926
}}
3027
],
3128
});
@@ -36,6 +33,15 @@ describe('Overlay directives', () => {
3633
fixture.detectChanges();
3734
});
3835

36+
beforeEach(inject([OverlayContainer], (oc: OverlayContainer) => {
37+
overlayContainer = oc;
38+
overlayContainerElement = oc.getContainerElement();
39+
}));
40+
41+
afterEach(() => {
42+
overlayContainer.ngOnDestroy();
43+
});
44+
3945
/** Returns the current open overlay pane element. */
4046
function getPaneElement() {
4147
return overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {fakeAsync, flushMicrotasks, inject} from '@angular/core/testing';
21
import {GlobalPositionStrategy} from './global-position-strategy';
32
import {OverlayRef} from '../overlay-ref';
43

@@ -19,11 +18,9 @@ describe('GlobalPositonStrategy', () => {
1918
strategy.dispose();
2019
});
2120

22-
it('should position the element to the (top, left) with an offset', fakeAsyncTest(() => {
21+
it('should position the element to the (top, left) with an offset', () => {
2322
strategy.top('10px').left('40px').apply();
2423

25-
flushMicrotasks();
26-
2724
let elementStyle = element.style;
2825
let parentStyle = (element.parentNode as HTMLElement).style;
2926

@@ -34,13 +31,11 @@ describe('GlobalPositonStrategy', () => {
3431

3532
expect(parentStyle.justifyContent).toBe('flex-start');
3633
expect(parentStyle.alignItems).toBe('flex-start');
37-
}));
34+
});
3835

39-
it('should position the element to the (bottom, right) with an offset', fakeAsyncTest(() => {
36+
it('should position the element to the (bottom, right) with an offset', () => {
4037
strategy.bottom('70px').right('15em').apply();
4138

42-
flushMicrotasks();
43-
4439
let elementStyle = element.style;
4540
let parentStyle = (element.parentNode as HTMLElement).style;
4641

@@ -51,14 +46,11 @@ describe('GlobalPositonStrategy', () => {
5146

5247
expect(parentStyle.justifyContent).toBe('flex-end');
5348
expect(parentStyle.alignItems).toBe('flex-end');
54-
}));
49+
});
5550

56-
it('should overwrite previously applied positioning', fakeAsyncTest(() => {
51+
it('should overwrite previously applied positioning', () => {
5752
strategy.centerHorizontally().centerVertically().apply();
58-
flushMicrotasks();
59-
6053
strategy.top('10px').left('40%').apply();
61-
flushMicrotasks();
6254

6355
let elementStyle = element.style;
6456
let parentStyle = (element.parentNode as HTMLElement).style;
@@ -73,33 +65,27 @@ describe('GlobalPositonStrategy', () => {
7365

7466
strategy.bottom('70px').right('15em').apply();
7567

76-
flushMicrotasks();
77-
7868
expect(element.style.marginTop).toBe('');
7969
expect(element.style.marginLeft).toBe('');
8070
expect(element.style.marginBottom).toBe('70px');
8171
expect(element.style.marginRight).toBe('15em');
8272

8373
expect(parentStyle.justifyContent).toBe('flex-end');
8474
expect(parentStyle.alignItems).toBe('flex-end');
85-
}));
75+
});
8676

87-
it('should center the element', fakeAsyncTest(() => {
77+
it('should center the element', () => {
8878
strategy.centerHorizontally().centerVertically().apply();
8979

90-
flushMicrotasks();
91-
9280
let parentStyle = (element.parentNode as HTMLElement).style;
9381

9482
expect(parentStyle.justifyContent).toBe('center');
9583
expect(parentStyle.alignItems).toBe('center');
96-
}));
84+
});
9785

98-
it('should center the element with an offset', fakeAsyncTest(() => {
86+
it('should center the element with an offset', () => {
9987
strategy.centerHorizontally('10px').centerVertically('15px').apply();
10088

101-
flushMicrotasks();
102-
10389
let elementStyle = element.style;
10490
let parentStyle = (element.parentNode as HTMLElement).style;
10591

@@ -108,74 +94,56 @@ describe('GlobalPositonStrategy', () => {
10894

10995
expect(parentStyle.justifyContent).toBe('center');
11096
expect(parentStyle.alignItems).toBe('center');
111-
}));
97+
});
11298

113-
it('should make the element position: static', fakeAsyncTest(() => {
99+
it('should make the element position: static', () => {
114100
strategy.apply();
115101

116-
flushMicrotasks();
117-
118102
expect(element.style.position).toBe('static');
119-
}));
103+
});
120104

121-
it('should wrap the element in a `cdk-global-overlay-wrapper`', fakeAsyncTest(() => {
105+
it('should wrap the element in a `cdk-global-overlay-wrapper`', () => {
122106
strategy.apply();
123107

124-
flushMicrotasks();
125-
126108
let parent = element.parentNode as HTMLElement;
127109

128110
expect(parent.classList.contains('cdk-global-overlay-wrapper')).toBe(true);
129-
}));
111+
});
130112

131113

132-
it('should remove the parent wrapper from the DOM', fakeAsync(() => {
114+
it('should remove the parent wrapper from the DOM', () => {
133115
strategy.apply();
134116

135-
flushMicrotasks();
136-
137117
expect(document.body.contains(element.parentNode!)).toBe(true);
138118

139119
strategy.dispose();
140120

141121
expect(document.body.contains(element.parentNode!)).toBe(false);
142-
}));
122+
});
143123

144-
it('should set the element width', fakeAsync(() => {
124+
it('should set the element width', () => {
145125
strategy.width('100px').apply();
146126

147-
flushMicrotasks();
148-
149127
expect(element.style.width).toBe('100px');
150-
}));
128+
});
151129

152-
it('should set the element height', fakeAsync(() => {
130+
it('should set the element height', () => {
153131
strategy.height('100px').apply();
154132

155-
flushMicrotasks();
156-
157133
expect(element.style.height).toBe('100px');
158-
}));
134+
});
159135

160-
it('should reset the horizontal position and offset when the width is 100%', fakeAsync(() => {
136+
it('should reset the horizontal position and offset when the width is 100%', () => {
161137
strategy.centerHorizontally().width('100%').apply();
162138

163-
flushMicrotasks();
164-
165139
expect(element.style.marginLeft).toBe('0px');
166140
expect((element.parentNode as HTMLElement).style.justifyContent).toBe('flex-start');
167-
}));
141+
});
168142

169-
it('should reset the vertical position and offset when the height is 100%', fakeAsync(() => {
143+
it('should reset the vertical position and offset when the height is 100%', () => {
170144
strategy.centerVertically().height('100%').apply();
171145

172-
flushMicrotasks();
173-
174146
expect(element.style.marginTop).toBe('0px');
175147
expect((element.parentNode as HTMLElement).style.alignItems).toBe('flex-start');
176-
}));
148+
});
177149
});
178-
179-
function fakeAsyncTest(fn: () => void) {
180-
return inject([], fakeAsync(fn));
181-
}

0 commit comments

Comments
 (0)