@@ -7,11 +7,18 @@ import {
77 flushMicrotasks ,
88 tick
99} from '@angular/core/testing' ;
10- import { NgModule , Component , Directive , ViewChild , ViewContainerRef } from '@angular/core' ;
10+ import { NgModule , Component , Directive , ViewChild , ViewContainerRef , Inject } from '@angular/core' ;
1111import { CommonModule } from '@angular/common' ;
1212import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
13- import { MdSnackBarModule , MdSnackBar , MdSnackBarConfig , SimpleSnackBar } from './index' ;
1413import { OverlayContainer , LiveAnnouncer } from '../core' ;
14+ import {
15+ MdSnackBarModule ,
16+ MdSnackBar ,
17+ MdSnackBarConfig ,
18+ MdSnackBarRef ,
19+ SimpleSnackBar ,
20+ MD_SNACK_BAR_DATA ,
21+ } from './index' ;
1522
1623
1724// TODO(josephperrott): Update tests to mock waiting for time to complete for animations.
@@ -174,17 +181,6 @@ describe('MdSnackBar', () => {
174181 } ) ;
175182 } ) ) ;
176183
177- it ( 'should open a custom component' , ( ) => {
178- let config = { viewContainerRef : testViewContainerRef } ;
179- let snackBarRef = snackBar . openFromComponent ( BurritosNotification , config ) ;
180-
181- expect ( snackBarRef . instance instanceof BurritosNotification )
182- . toBe ( true , 'Expected the snack bar content component to be BurritosNotification' ) ;
183- expect ( overlayContainerElement . textContent ! . trim ( ) )
184- . toBe ( 'Burritos are on the way.' ,
185- `Expected the overlay text content to be 'Burritos are on the way'` ) ;
186- } ) ;
187-
188184 it ( 'should set the animation state to visible on entry' , ( ) => {
189185 let config = { viewContainerRef : testViewContainerRef } ;
190186 let snackBarRef = snackBar . open ( simpleMessage , undefined , config ) ;
@@ -361,6 +357,37 @@ describe('MdSnackBar', () => {
361357 expect ( pane . getAttribute ( 'dir' ) ) . toBe ( 'rtl' , 'Expected the pane to be in RTL mode.' ) ;
362358 } ) ;
363359
360+ describe ( 'with custom component' , ( ) => {
361+ it ( 'should open a custom component' , ( ) => {
362+ const snackBarRef = snackBar . openFromComponent ( BurritosNotification ) ;
363+
364+ expect ( snackBarRef . instance instanceof BurritosNotification )
365+ . toBe ( true , 'Expected the snack bar content component to be BurritosNotification' ) ;
366+ expect ( overlayContainerElement . textContent ! . trim ( ) )
367+ . toBe ( 'Burritos are on the way.' , 'Expected component to have the proper text.' ) ;
368+ } ) ;
369+
370+ it ( 'should inject the snack bar reference into the component' , ( ) => {
371+ const snackBarRef = snackBar . openFromComponent ( BurritosNotification ) ;
372+
373+ expect ( snackBarRef . instance . snackBarRef )
374+ . toBe ( snackBarRef , 'Expected component to have an injected snack bar reference.' ) ;
375+ } ) ;
376+
377+ it ( 'should be able to inject arbitrary user data' , ( ) => {
378+ const snackBarRef = snackBar . openFromComponent ( BurritosNotification , {
379+ data : {
380+ burritoType : 'Chimichanga'
381+ }
382+ } ) ;
383+
384+ expect ( snackBarRef . instance . data ) . toBeTruthy ( 'Expected component to have a data object.' ) ;
385+ expect ( snackBarRef . instance . data . burritoType )
386+ . toBe ( 'Chimichanga' , 'Expected the injected data object to be the one the user provided.' ) ;
387+ } ) ;
388+
389+ } ) ;
390+
364391} ) ;
365392
366393describe ( 'MdSnackBar with parent MdSnackBar' , ( ) => {
@@ -453,7 +480,11 @@ class ComponentWithChildViewContainer {
453480
454481/** Simple component for testing ComponentPortal. */
455482@Component ( { template : '<p>Burritos are on the way.</p>' } )
456- class BurritosNotification { }
483+ class BurritosNotification {
484+ constructor (
485+ public snackBarRef : MdSnackBarRef < BurritosNotification > ,
486+ @Inject ( MD_SNACK_BAR_DATA ) public data : any ) { }
487+ }
457488
458489
459490@Component ( {
0 commit comments