forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.spec.ts
192 lines (182 loc) 路 7.53 KB
/
alert.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { BidiModule, Dir } from '@angular/cdk/bidi';
import { Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NzIconTestModule } from 'ng-zorro-antd/icon/testing';
import { NzAlertComponent } from './alert.component';
import { NzAlertModule } from './alert.module';
describe('alert', () => {
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [BidiModule, NzAlertModule, NoopAnimationsModule, NzIconTestModule],
declarations: [NzDemoTestBasicComponent, NzDemoTestBannerComponent, NzTestAlertRtlComponent]
});
TestBed.compileComponents();
})
);
describe('basic alert', () => {
let fixture: ComponentFixture<NzDemoTestBasicComponent>;
let testComponent: NzDemoTestBasicComponent;
let alert: DebugElement;
beforeEach(() => {
fixture = TestBed.createComponent(NzDemoTestBasicComponent);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
alert = fixture.debugElement.query(By.directive(NzAlertComponent));
});
it('should className correct', () => {
fixture.detectChanges();
expect(alert.nativeElement.firstElementChild!.classList).toContain('ant-alert');
});
it('should banner work', () => {
fixture.detectChanges();
expect(alert.nativeElement.firstElementChild!.classList).not.toContain('ant-alert-banner');
expect(alert.nativeElement.querySelector('.ant-alert').classList).toContain(`ant-alert-info`);
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeNull();
testComponent.banner = true;
fixture.detectChanges();
expect(alert.nativeElement.firstElementChild!.classList).toContain('ant-alert-banner');
expect(alert.nativeElement.querySelector('.ant-alert').classList).toContain(`ant-alert-info`);
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeNull();
});
it('should closeable work', fakeAsync(() => {
testComponent.closeable = true;
fixture.detectChanges();
expect(testComponent.onClose).toHaveBeenCalledTimes(0);
expect(alert.nativeElement.querySelector('.anticon-close')).toBeDefined();
alert.nativeElement.querySelector('.ant-alert-close-icon').click();
fixture.detectChanges();
tick();
fixture.detectChanges();
alert = fixture.debugElement.query(By.directive(NzAlertComponent));
expect(alert.nativeElement.innerText).toBe('');
expect(testComponent.onClose).toHaveBeenCalledTimes(1);
}));
it('should closeText work', () => {
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-close-icon')).toBeNull();
testComponent.closeText = 'closeText';
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-close-icon').innerText).toBe('closeText');
testComponent.closeText = testComponent.template;
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-close-icon').innerText).toBe('template');
});
it('should description work', () => {
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-description').innerText).toBe('description');
testComponent.description = testComponent.template;
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-description').innerText).toBe('template');
});
it('should message work', () => {
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-message').innerText).toBe('message');
testComponent.message = testComponent.template;
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-message').innerText).toBe('template');
});
it('should showIcon work', () => {
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeNull();
testComponent.showIcon = true;
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeDefined();
});
it('should iconType work', () => {
fixture.detectChanges();
testComponent.showIcon = true;
testComponent.iconType = 'lock';
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-icon').classList).toContain('anticon');
expect(alert.nativeElement.querySelector('.ant-alert-icon').classList).toContain('anticon-lock');
});
it('should type work', () => {
const listOfType = ['success', 'info', 'warning', 'error'];
listOfType.forEach(type => {
testComponent.type = type;
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert').classList).toContain(`ant-alert-${type}`);
});
});
it('should action work', () => {
fixture.detectChanges();
testComponent.action = testComponent.template;
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert-action').classList).not.toBeNull();
});
});
describe('banner alert', () => {
let fixture: ComponentFixture<NzDemoTestBannerComponent>;
let alert: DebugElement;
beforeEach(() => {
fixture = TestBed.createComponent(NzDemoTestBannerComponent);
fixture.detectChanges();
alert = fixture.debugElement.query(By.directive(NzAlertComponent));
});
it('should banner work', () => {
fixture.detectChanges();
expect(alert.nativeElement.querySelector('.ant-alert').classList).toContain(`ant-alert-warning`);
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeDefined();
});
});
describe('RTL', () => {
it('should className correct on dir change', () => {
const fixture = TestBed.createComponent(NzTestAlertRtlComponent);
const alert = fixture.debugElement.query(By.directive(NzAlertComponent));
fixture.detectChanges();
expect(alert.nativeElement.firstElementChild!.classList).toContain('ant-alert-rtl');
fixture.componentInstance.direction = 'ltr';
fixture.detectChanges();
expect(alert.nativeElement.firstElementChild!.classList).not.toContain('ant-alert-rtl');
});
});
});
@Component({
// eslint-disable-next-line
selector: 'nz-test-basic-alert',
template: `
<ng-template #template>template</ng-template>
<nz-alert
[nzBanner]="banner"
[nzCloseable]="closeable"
[nzCloseText]="closeText"
[nzDescription]="description"
[nzMessage]="message"
[nzShowIcon]="showIcon"
[nzIconType]="iconType"
[nzType]="type"
[nzAction]="action"
(nzOnClose)="onClose($event)"
></nz-alert>
`
})
export class NzDemoTestBasicComponent {
@ViewChild('template', { static: false }) template!: TemplateRef<void>;
action?: string | TemplateRef<void>;
banner = false;
closeable = false;
closeText?: string | TemplateRef<void>;
description: string | TemplateRef<void> = 'description';
message: string | TemplateRef<void> = 'message';
showIcon = false;
iconType?: string;
type = 'info';
onClose = jasmine.createSpy('close callback');
}
@Component({
template: ` <nz-alert nzBanner></nz-alert> `
})
export class NzDemoTestBannerComponent {}
@Component({
template: `
<div [dir]="direction">
<nz-test-basic-alert></nz-test-basic-alert>
</div>
`
})
export class NzTestAlertRtlComponent {
@ViewChild(Dir) dir!: Dir;
direction = 'rtl';
}