11import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2+ import { Subject } from 'rxjs' ;
23import { EventTypes } from 'src/app/models/event-types' ;
4+ import { ToastEvent } from 'src/app/models/toast-event' ;
5+ import { ToastService } from 'src/app/services/toast.service' ;
36import { ToasterComponent } from './toaster.component' ;
47
58describe ( 'ToasterComponent' , ( ) => {
69 let component : ToasterComponent ;
710 let fixture : ComponentFixture < ToasterComponent > ;
11+ let toastEventsSubject : Subject < ToastEvent > ;
812
913 beforeEach ( async ( ) => {
14+ // create subject for observable
15+ toastEventsSubject = new Subject < ToastEvent > ( ) ;
16+
17+ // create toast service mock
18+ const toastServiceSpy = jasmine . createSpyObj ( 'ToastService' , [ ] , {
19+ toastEvents : toastEventsSubject . asObservable ( ) ,
20+ } ) ;
21+
1022 await TestBed . configureTestingModule ( {
1123 imports : [ ToasterComponent ] ,
24+ providers : [ { provide : ToastService , useValue : toastServiceSpy } ] ,
1225 } ) . compileComponents ( ) ;
1326 } ) ;
1427
@@ -34,4 +47,20 @@ describe('ToasterComponent', () => {
3447 // then
3548 expect ( component . currentToasts ) . toEqual ( [ ] ) ;
3649 } ) ;
50+
51+ it ( 'should add new toast to currentToasts array when event is emitted' , ( ) => {
52+ // given
53+ component . subscribeToToasts ( ) ;
54+ const mockToast : ToastEvent = {
55+ type : EventTypes . Success ,
56+ title : 'success' ,
57+ message : 'success' ,
58+ } ;
59+
60+ // when
61+ toastEventsSubject . next ( mockToast ) ;
62+
63+ // then
64+ expect ( component . currentToasts [ 0 ] ) . toEqual ( mockToast ) ;
65+ } ) ;
3766} ) ;
0 commit comments