Skip to content

Commit 1d3c7d7

Browse files
committed
test(toasts): enhance test classes to bump coverage
1 parent fe1a453 commit 1d3c7d7

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/app/components/toast/toast.component.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DebugElement } from '@angular/core';
1+
import { DebugElement, ElementRef } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { EventTypes } from 'src/app/models/event-types';
44
import { ToastComponent } from './toast.component';
@@ -53,4 +53,23 @@ describe('ToastComponent', () => {
5353
expect(component.disposeEvent.emit).toHaveBeenCalledTimes(1);
5454
expect(component.toast.dispose).toHaveBeenCalledTimes(1);
5555
});
56+
57+
it('should subscribe to hidden.bs.toast event and call hide()', (done) => {
58+
// given
59+
component.type = EventTypes.Info;
60+
const element = document.createElement('div');
61+
component.toastEl = new ElementRef(element);
62+
spyOn(component, 'hide');
63+
64+
// when
65+
component.show();
66+
const event = new Event('hidden.bs.toast');
67+
element.dispatchEvent(event);
68+
69+
// then
70+
setTimeout(() => {
71+
expect(component.hide).toHaveBeenCalledTimes(1);
72+
done();
73+
}, 100);
74+
});
5675
});

src/app/components/toaster/toaster.component.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { Subject } from 'rxjs';
23
import { 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';
36
import { ToasterComponent } from './toaster.component';
47

58
describe('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

Comments
 (0)