Skip to content

Commit 9626c2f

Browse files
authored
Merge pull request #10813 from IgniteUI/toast-coverage
test(toast): refactor, increase coverage #10812
2 parents 44e2256 + 90cb692 commit 9626c2f

File tree

6 files changed

+58
-128
lines changed

6 files changed

+58
-128
lines changed
Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,109 @@
1-
import { Component, ViewChild } from '@angular/core';
21
import {
32
waitForAsync,
43
TestBed,
54
ComponentFixture,
5+
flushMicrotasks,
6+
fakeAsync,
67
} from '@angular/core/testing';
7-
import { By } from '@angular/platform-browser';
88
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
99
import {
1010
IgxToastComponent,
1111
IgxToastModule,
1212
} from './toast.component';
1313
import { configureTestSuite } from '../test-utils/configure-suite';
14-
import { useAnimation } from '@angular/animations';
15-
import { HorizontalAlignment, PositionSettings, slideInLeft, slideInRight, VerticalAlignment } from 'igniteui-angular';
14+
import { HorizontalAlignment, PositionSettings, VerticalAlignment } from 'igniteui-angular';
1615

1716
describe('IgxToast', () => {
1817
configureTestSuite();
1918
beforeAll(waitForAsync(() => {
2019
TestBed.configureTestingModule({
21-
declarations: [ToastInitializeTestComponent],
2220
imports: [NoopAnimationsModule, IgxToastModule]
2321
}).compileComponents();
2422
}));
2523

26-
const baseClass = 'igx-toast';
27-
let fixture: ComponentFixture<ToastInitializeTestComponent>;
24+
const baseId = 'igx-toast-';
25+
let fixture: ComponentFixture<IgxToastComponent>;
2826
let toast: IgxToastComponent;
2927

3028
beforeEach(() => {
31-
fixture = TestBed.createComponent(ToastInitializeTestComponent);
32-
toast = fixture.componentInstance.toast;
29+
fixture = TestBed.createComponent(IgxToastComponent);
30+
toast = fixture.componentInstance;
3331
fixture.detectChanges();
3432
});
3533

3634
it('should properly initialize', () => {
37-
const domToast = fixture.debugElement.query(By.css(baseClass))
38-
.nativeElement;
39-
expect(toast.id).toContain('igx-toast-');
40-
expect(domToast.id).toContain('igx-toast-');
35+
expect(toast.id).toContain(baseId);
36+
expect(toast.element.id).toContain(baseId);
4137

4238
toast.id = 'customToast';
4339
fixture.detectChanges();
4440

4541
expect(toast.id).toBe('customToast');
46-
expect(domToast.id).toBe('customToast');
42+
expect(toast.element.id).toContain('customToast');
4743
});
4844

49-
it('should properly change verical position', () => {
50-
expect(toast.position).toBe('bottom');
51-
expect(toast.positionSettings.verticalDirection).toBe(0);
45+
it('should properly toggle and emit isVisibleChange', fakeAsync(() => {
46+
spyOn(toast.isVisibleChange, 'emit').and.callThrough();
47+
expect(toast.isVisible).toBe(false);
48+
expect(toast.isVisibleChange.emit).toHaveBeenCalledTimes(0);
5249

53-
toast.position = 'top';
54-
fixture.detectChanges();
55-
expect(toast.positionSettings.verticalDirection).toBe(-1);
56-
});
50+
toast.toggle();
51+
expect(toast.isVisible).toBe(true);
52+
flushMicrotasks();
53+
expect(toast.isVisibleChange.emit).toHaveBeenCalledOnceWith({ owner: toast, id: '0' });
54+
55+
toast.toggle();
56+
flushMicrotasks();
57+
expect(toast.isVisible).toBe(false);
58+
expect(toast.isVisibleChange.emit).toHaveBeenCalledTimes(2);
59+
}));
5760
});
5861

5962
describe('IgxToast with positionSettings', () => {
6063
configureTestSuite();
6164
beforeAll(waitForAsync(() => {
6265
TestBed.configureTestingModule({
63-
declarations: [ToastPositionSettingsTestComponent],
6466
imports: [NoopAnimationsModule, IgxToastModule]
6567
}).compileComponents();
6668
}));
6769

68-
let fixture: ComponentFixture<ToastPositionSettingsTestComponent>;
70+
let fixture: ComponentFixture<IgxToastComponent>;
6971
let toast: IgxToastComponent;
72+
let firstPositionSettings: PositionSettings = {
73+
horizontalDirection: HorizontalAlignment.Left,
74+
verticalDirection: VerticalAlignment.Middle,
75+
horizontalStartPoint: HorizontalAlignment.Left,
76+
verticalStartPoint: VerticalAlignment.Middle
77+
};
78+
let secondPositionSettings: PositionSettings = {
79+
horizontalDirection: HorizontalAlignment.Center,
80+
verticalDirection: VerticalAlignment.Middle,
81+
horizontalStartPoint: HorizontalAlignment.Center,
82+
verticalStartPoint: VerticalAlignment.Middle
83+
};
7084

7185
beforeEach(() => {
72-
fixture = TestBed.createComponent(ToastPositionSettingsTestComponent);
73-
toast = fixture.componentInstance.toast;
86+
fixture = TestBed.createComponent(IgxToastComponent);
87+
toast = fixture.componentInstance;
7488
fixture.detectChanges();
7589
});
7690

7791
it('should be able to change positionSettings', () => {
92+
toast.positionSettings = firstPositionSettings;
7893
expect(toast.positionSettings.horizontalDirection).toBe(-1);
7994
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
80-
toast.positionSettings = fixture.componentInstance.secondPositionSettings;
95+
toast.positionSettings = secondPositionSettings;
8196
fixture.detectChanges();
8297
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
8398
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
8499
});
85100

86101
it('positionSettings passed in the open method should be applied', () => {
87-
const positions = fixture.componentInstance.secondPositionSettings;
88-
toast.open(undefined, positions);
102+
const positions = secondPositionSettings;
103+
toast.open("New Message", positions);
89104
fixture.detectChanges();
90105
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
91106
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
107+
expect(toast.textMessage).toBe("New Message");
92108
});
93109
});
94-
95-
@Component({
96-
template: `<igx-toast #toast></igx-toast>`,
97-
})
98-
class ToastInitializeTestComponent {
99-
@ViewChild(IgxToastComponent, { static: true })
100-
public toast: IgxToastComponent;
101-
}
102-
103-
@Component({
104-
template: `<igx-toast #toast [positionSettings]="firstPositionSettings"></igx-toast>`,
105-
})
106-
class ToastPositionSettingsTestComponent {
107-
@ViewChild(IgxToastComponent, { static: true })
108-
public toast: IgxToastComponent;
109-
public firstPositionSettings: PositionSettings = {
110-
horizontalDirection: HorizontalAlignment.Left,
111-
verticalDirection: VerticalAlignment.Middle,
112-
horizontalStartPoint: HorizontalAlignment.Left,
113-
verticalStartPoint: VerticalAlignment.Middle
114-
};
115-
public secondPositionSettings: PositionSettings = {
116-
horizontalDirection: HorizontalAlignment.Center,
117-
verticalDirection: VerticalAlignment.Middle,
118-
horizontalStartPoint: HorizontalAlignment.Center,
119-
verticalStartPoint: VerticalAlignment.Middle
120-
};
121-
}
122-

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

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import {
88
Inject,
99
Input,
1010
NgModule,
11-
OnChanges,
1211
OnInit,
1312
Optional,
14-
Output,
15-
SimpleChanges
13+
Output
1614
} from '@angular/core';
1715
import { takeUntil } from 'rxjs/operators';
1816
import { IgxNavigationService } from '../core/navigation';
@@ -23,29 +21,13 @@ import {
2321
GlobalPositionStrategy,
2422
PositionSettings
2523
} from '../services/public_api';
26-
import { mkenum } from '../core/utils';
2724
import { IgxNotificationsDirective } from '../directives/notification/notifications.directive';
2825
import { ToggleViewEventArgs } from '../directives/toggle/toggle.directive';
2926
import { useAnimation } from '@angular/animations';
3027
import { fadeIn, fadeOut } from '../animations/fade';
3128

3229
let NEXT_ID = 0;
3330

34-
/**
35-
* Enumeration for toast position
36-
* Can be:
37-
* Bottom
38-
* Middle
39-
* Top
40-
*/
41-
export const IgxToastPosition = mkenum({
42-
Bottom: 'bottom',
43-
Middle: 'middle',
44-
Top: 'top'
45-
});
46-
47-
export type IgxToastPosition = (typeof IgxToastPosition)[keyof typeof IgxToastPosition];
48-
4931
/**
5032
* **Ignite UI for Angular Toast** -
5133
* [Documentation](https://www.infragistics.com/products/ignite-ui-angular/angular/components/toast)
@@ -65,7 +47,7 @@ export type IgxToastPosition = (typeof IgxToastPosition)[keyof typeof IgxToastPo
6547
selector: 'igx-toast',
6648
templateUrl: 'toast.component.html'
6749
})
68-
export class IgxToastComponent extends IgxNotificationsDirective implements OnInit, OnChanges {
50+
export class IgxToastComponent extends IgxNotificationsDirective implements OnInit {
6951
/**
7052
* @hidden
7153
*/
@@ -108,32 +90,6 @@ export class IgxToastComponent extends IgxNotificationsDirective implements OnIn
10890
@Output()
10991
public isVisibleChange = new EventEmitter<ToggleViewEventArgs>();
11092

111-
/**
112-
* @deprecated in version 12.2.3. We suggest using `positionSettings` property instead
113-
*
114-
* Sets/gets the position of the toast.
115-
* If not set, the `position` attribute will have value `IgxToastPosition.Bottom`.
116-
* ```html
117-
* <igx-toast [position]="top"></igx-toast>
118-
* ```
119-
* ```typescript
120-
* let toastPosition = this.toast.position;
121-
* ```
122-
*
123-
* @memberof IgxToastComponent
124-
*/
125-
@Input()
126-
public get position(): IgxToastPosition {
127-
return this._position;
128-
}
129-
130-
public set position(position: IgxToastPosition) {
131-
if (position) {
132-
this._position = position;
133-
this._positionSettings.verticalDirection = this.calculatePosition();
134-
}
135-
}
136-
13793
/**
13894
* Get the position and animation settings used by the toast.
13995
* ```typescript
@@ -170,7 +126,6 @@ export class IgxToastComponent extends IgxNotificationsDirective implements OnIn
170126
this._positionSettings = settings;
171127
}
172128

173-
private _position: IgxToastPosition = 'bottom';
174129
private _positionSettings: PositionSettings = {
175130
horizontalDirection: HorizontalAlignment.Center,
176131
verticalDirection: VerticalAlignment.Bottom,
@@ -247,20 +202,6 @@ export class IgxToastComponent extends IgxNotificationsDirective implements OnIn
247202
this.isVisibleChange.emit(closedEventArgs);
248203
});
249204
}
250-
251-
public ngOnChanges(changes: SimpleChanges) {
252-
if (changes['position'] && this._positionSettings) {
253-
this._positionSettings.verticalDirection = this.calculatePosition();
254-
}
255-
}
256-
257-
private calculatePosition() {
258-
return this.position === 'bottom'
259-
? VerticalAlignment.Bottom
260-
: this.position === 'middle'
261-
? VerticalAlignment.Middle
262-
: VerticalAlignment.Top;
263-
}
264205
}
265206

266207
/**

src/app/drop-down/drop-down-virtual/drop-down-virtual.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core';
22
import { RemoteService } from 'src/app/shared/remote.service';
33
import { Observable } from 'rxjs';
4-
import { IForOfState, IgxDropDownComponent, IgxToastComponent, IgxForOfDirective, DisplayDensity } from 'igniteui-angular';
4+
import { IForOfState, IgxDropDownComponent, IgxToastComponent, IgxForOfDirective, DisplayDensity, VerticalAlignment } from 'igniteui-angular';
55

66
interface DataItem {
77
name: string;
@@ -63,7 +63,7 @@ export class DropDownVirtualComponent implements OnInit, AfterViewInit {
6363
if (this.prevRequest) {
6464
this.prevRequest.unsubscribe();
6565
}
66-
this.loadingToast.position = 'middle';
66+
this.loadingToast.positionSettings.verticalDirection = VerticalAlignment.Middle;
6767
this.loadingToast.autoHide = false;
6868
this.loadingToast.open('Loading Remote Data...');
6969
this.cdr.detectChanges();

src/app/grid/grid.sample.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
IgxExcelExporterService,
1616
IgxStringFilteringOperand,
1717
DefaultSortingStrategy,
18-
GridSelectionMode
18+
GridSelectionMode,
19+
VerticalAlignment
1920
} from 'igniteui-angular';
2021
import { RemoteService } from '../shared/remote.service';
2122
import { LocalService } from '../shared/local.service';
@@ -118,7 +119,7 @@ export class GridSampleComponent implements OnInit, AfterViewInit {
118119
}
119120

120121
public process() {
121-
this.toast.position = 'bottom';
122+
this.toast.positionSettings.verticalDirection = VerticalAlignment.Bottom;
122123
this.toast.open('Loading remote data');
123124
this.remoteService.getData(this.grid3.data, () => {
124125
this.toast.close();

src/app/toast/toast.sample.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ <h4 class="sample-title">Bottom Position</h4>
55
<button
66
igxButton="raised"
77
igxRipple="white"
8-
(click)="showToast(toast, 'bottom')"
8+
(click)="showToast(toast, position.Bottom)"
99
[disabled]="toast.isVisible"
1010
>
1111
Show Toast
@@ -27,7 +27,7 @@ <h4 class="sample-title">Middle Position</h4>
2727
<button
2828
igxButton="raised"
2929
igxRipple="white"
30-
(click)="showToast(toast, 'middle')"
30+
(click)="showToast(toast, position.Middle)"
3131
[disabled]="toast.isVisible"
3232
>
3333
Show Toast
@@ -49,7 +49,7 @@ <h4 class="sample-title">Top Position</h4>
4949
<button
5050
igxButton="raised"
5151
igxRipple="white"
52-
(click)="showToast(toast, 'top')"
52+
(click)="showToast(toast, position.Top)"
5353
[disabled]="toast.isVisible"
5454
>
5555
Show Toast

src/app/toast/toast.sample.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, ViewChild } from '@angular/core';
2-
import { IgxToastComponent, IgxToastPosition } from 'igniteui-angular';
2+
import { IgxToastComponent, VerticalAlignment } from 'igniteui-angular';
33

44
@Component({
55
selector: 'app-toast-sample',
@@ -9,9 +9,10 @@ import { IgxToastComponent, IgxToastPosition } from 'igniteui-angular';
99
export class ToastSampleComponent {
1010
@ViewChild('toast')
1111
public toast: IgxToastComponent;
12+
public position = VerticalAlignment;
1213

13-
public showToast(toast: IgxToastComponent, pos: IgxToastPosition) {
14-
toast.position = pos;
14+
public showToast(toast: IgxToastComponent, pos: VerticalAlignment) {
15+
toast.positionSettings.verticalDirection = pos;
1516
toast.open();
1617
}
1718

0 commit comments

Comments
 (0)