|
1 | | -import { Component, ViewChild } from '@angular/core'; |
2 | 1 | import { |
3 | 2 | waitForAsync, |
4 | 3 | TestBed, |
5 | 4 | ComponentFixture, |
| 5 | + flushMicrotasks, |
| 6 | + fakeAsync, |
6 | 7 | } from '@angular/core/testing'; |
7 | | -import { By } from '@angular/platform-browser'; |
8 | 8 | import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
9 | 9 | import { |
10 | 10 | IgxToastComponent, |
11 | 11 | IgxToastModule, |
12 | 12 | } from './toast.component'; |
13 | 13 | 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'; |
16 | 15 |
|
17 | 16 | describe('IgxToast', () => { |
18 | 17 | configureTestSuite(); |
19 | 18 | beforeAll(waitForAsync(() => { |
20 | 19 | TestBed.configureTestingModule({ |
21 | | - declarations: [ToastInitializeTestComponent], |
22 | 20 | imports: [NoopAnimationsModule, IgxToastModule] |
23 | 21 | }).compileComponents(); |
24 | 22 | })); |
25 | 23 |
|
26 | | - const baseClass = 'igx-toast'; |
27 | | - let fixture: ComponentFixture<ToastInitializeTestComponent>; |
| 24 | + const baseId = 'igx-toast-'; |
| 25 | + let fixture: ComponentFixture<IgxToastComponent>; |
28 | 26 | let toast: IgxToastComponent; |
29 | 27 |
|
30 | 28 | beforeEach(() => { |
31 | | - fixture = TestBed.createComponent(ToastInitializeTestComponent); |
32 | | - toast = fixture.componentInstance.toast; |
| 29 | + fixture = TestBed.createComponent(IgxToastComponent); |
| 30 | + toast = fixture.componentInstance; |
33 | 31 | fixture.detectChanges(); |
34 | 32 | }); |
35 | 33 |
|
36 | 34 | 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); |
41 | 37 |
|
42 | 38 | toast.id = 'customToast'; |
43 | 39 | fixture.detectChanges(); |
44 | 40 |
|
45 | 41 | expect(toast.id).toBe('customToast'); |
46 | | - expect(domToast.id).toBe('customToast'); |
| 42 | + expect(toast.element.id).toContain('customToast'); |
47 | 43 | }); |
48 | 44 |
|
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); |
52 | 49 |
|
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 | + })); |
57 | 60 | }); |
58 | 61 |
|
59 | 62 | describe('IgxToast with positionSettings', () => { |
60 | 63 | configureTestSuite(); |
61 | 64 | beforeAll(waitForAsync(() => { |
62 | 65 | TestBed.configureTestingModule({ |
63 | | - declarations: [ToastPositionSettingsTestComponent], |
64 | 66 | imports: [NoopAnimationsModule, IgxToastModule] |
65 | 67 | }).compileComponents(); |
66 | 68 | })); |
67 | 69 |
|
68 | | - let fixture: ComponentFixture<ToastPositionSettingsTestComponent>; |
| 70 | + let fixture: ComponentFixture<IgxToastComponent>; |
69 | 71 | 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 | + }; |
70 | 84 |
|
71 | 85 | beforeEach(() => { |
72 | | - fixture = TestBed.createComponent(ToastPositionSettingsTestComponent); |
73 | | - toast = fixture.componentInstance.toast; |
| 86 | + fixture = TestBed.createComponent(IgxToastComponent); |
| 87 | + toast = fixture.componentInstance; |
74 | 88 | fixture.detectChanges(); |
75 | 89 | }); |
76 | 90 |
|
77 | 91 | it('should be able to change positionSettings', () => { |
| 92 | + toast.positionSettings = firstPositionSettings; |
78 | 93 | expect(toast.positionSettings.horizontalDirection).toBe(-1); |
79 | 94 | expect(toast.positionSettings.verticalDirection).toBe(-0.5); |
80 | | - toast.positionSettings = fixture.componentInstance.secondPositionSettings; |
| 95 | + toast.positionSettings = secondPositionSettings; |
81 | 96 | fixture.detectChanges(); |
82 | 97 | expect(toast.positionSettings.horizontalDirection).toBe(-0.5); |
83 | 98 | expect(toast.positionSettings.verticalDirection).toBe(-0.5); |
84 | 99 | }); |
85 | 100 |
|
86 | 101 | 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); |
89 | 104 | fixture.detectChanges(); |
90 | 105 | expect(toast.positionSettings.horizontalDirection).toBe(-0.5); |
91 | 106 | expect(toast.positionSettings.verticalDirection).toBe(-0.5); |
| 107 | + expect(toast.textMessage).toBe("New Message"); |
92 | 108 | }); |
93 | 109 | }); |
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 | | - |
0 commit comments