Skip to content

Commit 96c9f55

Browse files
andrewseguinjelbourn
authored andcommitted
test(snackbar): fix tests expectations in dismissal subscription (#1664)
1 parent 4a0f820 commit 96c9f55

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
inject,
3-
async,
4-
ComponentFixture,
5-
TestBed
2+
inject,
3+
async,
4+
ComponentFixture,
5+
TestBed,
66
} from '@angular/core/testing';
77
import {
88
NgModule,
@@ -91,7 +91,7 @@ describe('MdSnackBar', () => {
9191
.toBe('BUTTON', 'Expected snack bar action label to be a <button>');
9292
expect(buttonElement.textContent)
9393
.toBe(simpleActionLabel,
94-
`Expected the snack bar action labe; to be '${simpleActionLabel}'`);
94+
`Expected the snack bar action label to be '${simpleActionLabel}'`);
9595
});
9696

9797
it('should open a simple message with no button', () => {
@@ -114,25 +114,28 @@ describe('MdSnackBar', () => {
114114
.toBeNull('Expected the query selection for action label to be null');
115115
});
116116

117-
it('should dismiss the snack bar and remove itself from the view', () => {
117+
it('should dismiss the snack bar and remove itself from the view', async(() => {
118118
let config = new MdSnackBarConfig(testViewContainerRef);
119-
let snackBarRef = snackBar.open(simpleMessage, null, config);
120-
let dismissed = true;
121-
snackBarRef.afterDismissed().subscribe(result => {
122-
dismissed = true;
123-
});
119+
let dismissObservableCompleted = false;
124120

121+
let snackBarRef = snackBar.open(simpleMessage, null, config);
125122
viewContainerFixture.detectChanges();
126123
expect(overlayContainerElement.childElementCount)
127124
.toBeGreaterThan(0, 'Expected overlay container element to have at least one child');
128125

129-
snackBarRef.dismiss();
130126
snackBarRef.afterDismissed().subscribe(null, null, () => {
131-
expect(dismissed).toBeTruthy('Expected the snack bar to be dismissed');
127+
dismissObservableCompleted = true;
128+
});
129+
130+
snackBarRef.dismiss();
131+
viewContainerFixture.detectChanges(); // Run through animations for dismissal
132+
133+
viewContainerFixture.whenStable().then(() => {
134+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
132135
expect(overlayContainerElement.childElementCount)
133136
.toBe(0, 'Expected the overlay container element to have no child elements');
134137
});
135-
});
138+
}));
136139

137140
it('should open a custom component', () => {
138141
let config = new MdSnackBarConfig(testViewContainerRef);
@@ -166,9 +169,10 @@ describe('MdSnackBar', () => {
166169
});
167170

168171
it(`should set the old snack bar animation state to complete and the new snack bar animation
169-
state to visible on entry of new snack bar`, () => {
172+
state to visible on entry of new snack bar`, async(() => {
170173
let config = new MdSnackBarConfig(testViewContainerRef);
171174
let snackBarRef = snackBar.open(simpleMessage, null, config);
175+
let dismissObservableCompleted = false;
172176

173177
viewContainerFixture.detectChanges();
174178
expect(snackBarRef.containerInstance.animationState)
@@ -179,12 +183,17 @@ describe('MdSnackBar', () => {
179183

180184
viewContainerFixture.detectChanges();
181185
snackBarRef.afterDismissed().subscribe(null, null, () => {
186+
dismissObservableCompleted = true;
187+
});
188+
189+
viewContainerFixture.whenStable().then(() => {
190+
expect(dismissObservableCompleted).toBe(true);
182191
expect(snackBarRef.containerInstance.animationState)
183192
.toBe('complete', `Expected the animation state would be 'complete'.`);
184193
expect(snackBarRef2.containerInstance.animationState)
185194
.toBe('visible', `Expected the animation state would be 'visible'.`);
186195
});
187-
});
196+
}));
188197
});
189198

190199
@Directive({selector: 'dir-with-view-container'})

0 commit comments

Comments
 (0)