Skip to content

Commit 8227f8f

Browse files
committed
refactor(toast): emit onShown after toggle onOpened fired
1 parent 0037eb6 commit 8227f8f

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

projects/igniteui-angular/src/lib/toast/toast.component.spec.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,23 @@ describe('IgxToast', () => {
5050
expect(domToast.id).toBe('customToast');
5151
});
5252

53-
it('should auto hide 1 second after it\'s open', fakeAsync(() => {
53+
it('should auto hide after it\'s open', fakeAsync(() => {
54+
spyOn(toast.onHiding, 'emit');
5455
toast.displayTime = 1000;
5556

5657
toast.show();
57-
58-
expect(toast.isVisible).toBeTruthy();
59-
expect(toast.autoHide).toBeTruthy();
60-
6158
tick(1000);
62-
63-
expect(toast.isVisible).toBeFalsy();
59+
expect(toast.onHiding.emit).toHaveBeenCalled();
6460
}));
6561

6662
it('should not auto hide after it\'s open', fakeAsync(() => {
63+
spyOn(toast.onHiding, 'emit');
6764
toast.displayTime = 1000;
6865
toast.autoHide = false;
6966

7067
toast.show();
71-
72-
expect(toast.isVisible).toBeTruthy();
73-
expect(toast.autoHide).toBeFalsy();
74-
7568
tick(1000);
76-
77-
expect(toast.isVisible).toBeTruthy();
69+
expect(toast.onHiding.emit).not.toHaveBeenCalled();
7870
}));
7971

8072
it('should emit onShowing when toast is shown', () => {
@@ -83,42 +75,45 @@ describe('IgxToast', () => {
8375
expect(toast.onShowing.emit).toHaveBeenCalled();
8476
});
8577

86-
it('should emit onShown when super onAppended is fired', waitForAsync((done: DoneFn) => {
87-
toast.show();
88-
89-
toast.onAppended.subscribe(() => {
90-
spyOn(toast.onShown, 'emit');
91-
expect(toast.onShown.emit).toHaveBeenCalled();
92-
done();
93-
});
94-
}));
95-
9678
it('should emit onHiding when toast is hidden', () => {
9779
spyOn(toast.onHiding, 'emit');
9880
toast.hide();
9981
expect(toast.onHiding.emit).toHaveBeenCalled();
10082
});
10183

102-
it('should emit onHidden when super onClosed is fired', waitForAsync((done: DoneFn) => {
84+
it('should emit onShown when toggle onOpened is fired', waitForAsync(() => {
85+
spyOn(toast.onShown, 'emit');
86+
toast.open();
87+
88+
toast.onOpened.subscribe(() => {
89+
expect(toast.onShown.emit).toHaveBeenCalled();
90+
});
91+
}));
92+
93+
it('should emit onHidden when toggle onClosed is fired', waitForAsync(() => {
94+
spyOn(toast.onHidden, 'emit');
10395
toast.isVisible = true;
104-
toast.hide();
96+
toast.close();
10597

10698
toast.onClosed.subscribe(() => {
107-
spyOn(toast.onHidden, 'emit');
10899
expect(toast.onHidden.emit).toHaveBeenCalled();
109-
done();
110100
});
111101
}));
112102

113-
it('visibility is updated by the toggle() method', () => {
103+
it('visibility is updated by the toggle() method', waitForAsync((done: DoneFn) => {
104+
toast.autoHide = false;
105+
114106
toast.toggle();
115-
fixture.detectChanges();
116-
expect(toast.isVisible).toEqual(true);
107+
toast.onOpened.subscribe(() => {
108+
expect(toast.isVisible).toEqual(true);
109+
});
117110

118111
toast.toggle();
119-
fixture.detectChanges();
120-
expect(toast.isVisible).toEqual(false);
121-
});
112+
toast.onClosed.subscribe(() => {
113+
expect(toast.isVisible).toEqual(false);
114+
done();
115+
});
116+
}));
122117

123118
it('can set message through show method', fakeAsync(() => {
124119
toast.displayTime = 100;

projects/igniteui-angular/src/lib/toast/toast.component.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class IgxToastComponent extends IgxToggleDirective
104104
* Emits an event when the toast is shown.
105105
* Provides reference to the `IgxToastComponent` as event argument.
106106
* ```html
107-
* <igx-toast (onShown) = "onShown($event)"></igx-toast>
107+
* <igx-toast (onShown)="onShown($event)"></igx-toast>
108108
* ```
109109
* @memberof IgxToastComponent
110110
*/
@@ -115,7 +115,7 @@ export class IgxToastComponent extends IgxToggleDirective
115115
* Emits an event prior the toast is hidden.
116116
* Provides reference to the `IgxToastComponent` as event argument.
117117
* ```html
118-
* <igx-toast (onHiding) = "onHiding($event)"></igx-toast>
118+
* <igx-toast (onHiding)="onHiding($event)"></igx-toast>
119119
* ```
120120
* @memberof IgxToastComponent
121121
*/
@@ -126,7 +126,7 @@ export class IgxToastComponent extends IgxToggleDirective
126126
* Emits an event when the toast is hidden.
127127
* Provides reference to the `IgxToastComponent` as event argument.
128128
* ```html
129-
* <igx-toast (onHidden) = "onHidden($event)"></igx-toast>
129+
* <igx-toast (onHidden)="onHidden($event)"></igx-toast>
130130
* ```
131131
* @memberof IgxToastComponent
132132
*/
@@ -321,9 +321,7 @@ export class IgxToastComponent extends IgxToggleDirective
321321
this.toastMessage = message;
322322
}
323323

324-
this.isVisible = true;
325324
this.onShowing.emit(this);
326-
327325
super.open(overlaySettings);
328326

329327
if (this.autoHide) {
@@ -342,7 +340,6 @@ export class IgxToastComponent extends IgxToggleDirective
342340
*/
343341
public hide(): void {
344342
clearInterval(this.timeoutId);
345-
this.isVisible = false;
346343
this.onHiding.emit(this);
347344
super.close();
348345
}
@@ -378,12 +375,14 @@ export class IgxToastComponent extends IgxToggleDirective
378375
* @hidden
379376
*/
380377
ngOnInit() {
381-
this.onAppended.pipe(takeUntil(this.d$)).subscribe(() => {
378+
this.onOpened.pipe(takeUntil(this.d$)).subscribe(() => {
382379
this.onShown.emit(this);
380+
this.isVisible = true;
383381
});
384382

385383
this.onClosed.pipe(takeUntil(this.d$)).subscribe(() => {
386384
this.onHidden.emit(this);
385+
this.isVisible = false;
387386
});
388387
}
389388

src/app/toast/toast.sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class ToastSampleComponent {
2727
}
2828

2929
handleHiding(event) {
30-
console.log('toast hiding', event);
30+
console.log('hiding toast', event);
3131
}
3232

3333
handleHidden(event) {

0 commit comments

Comments
 (0)