Skip to content

Commit 605e969

Browse files
authored
feat(module:cron-expression): add Unit Testing (#7993)
1 parent 6d31bde commit 605e969

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

components/cron-expression/cron-expression.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class NzCronExpressionComponent implements OnInit, ControlValueAccessor,
126126
labels: TimeType[] = [];
127127
interval!: CronExpression<false>;
128128
nextTimeList: Date[] = [];
129+
private isNzDisableFirstChange: boolean = true;
129130
private destroy$ = new Subject<void>();
130131

131132
validateForm!: UntypedFormGroup;
@@ -164,6 +165,12 @@ export class NzCronExpressionComponent implements OnInit, ControlValueAccessor,
164165
}
165166
}
166167

168+
setDisabledState(isDisabled: boolean): void {
169+
this.nzDisabled = (this.isNzDisableFirstChange && this.nzDisabled) || isDisabled;
170+
this.isNzDisableFirstChange = false;
171+
this.cdr.markForCheck();
172+
}
173+
167174
constructor(private formBuilder: UntypedFormBuilder, private cdr: ChangeDetectorRef, private i18n: NzI18nService) {}
168175

169176
ngOnInit(): void {
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { HttpClientTestingModule } from '@angular/common/http/testing';
2+
import { Component, DebugElement } from '@angular/core';
3+
import { ComponentFixture, fakeAsync, flush } from '@angular/core/testing';
4+
import { FormsModule, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
5+
import { By } from '@angular/platform-browser';
6+
7+
import { ComponentBed, createComponentBed } from 'ng-zorro-antd/core/testing/component-bed';
8+
import { NzCronExpressionComponent } from 'ng-zorro-antd/cron-expression/cron-expression.component';
9+
import { NzCronExpressionModule } from 'ng-zorro-antd/cron-expression/cron-expression.module';
10+
import { NzCronExpressionSize } from 'ng-zorro-antd/cron-expression/typings';
11+
import { NzFormModule } from 'ng-zorro-antd/form';
12+
13+
describe('nz-cron-expression', () => {
14+
describe('basic', () => {
15+
let testBed: ComponentBed<NzTestCronExpressionComponent>;
16+
let fixture: ComponentFixture<NzTestCronExpressionComponent>;
17+
let testComponent: NzTestCronExpressionComponent;
18+
let resultEl: DebugElement;
19+
20+
beforeEach(() => {
21+
testBed = createComponentBed(NzTestCronExpressionComponent, {
22+
imports: [NzCronExpressionModule, HttpClientTestingModule]
23+
});
24+
fixture = testBed.fixture;
25+
fixture.detectChanges();
26+
testComponent = testBed.component;
27+
resultEl = fixture.debugElement.query(By.directive(NzCronExpressionComponent));
28+
});
29+
30+
it('cron-expression basic', () => {
31+
fixture.detectChanges();
32+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain('ant-input');
33+
});
34+
35+
it('cron-expression nzSize', () => {
36+
testComponent.nzSize = 'small';
37+
fixture.detectChanges();
38+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain(
39+
'ant-input-sm'
40+
);
41+
testComponent.nzSize = 'large';
42+
fixture.detectChanges();
43+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain(
44+
'ant-input-lg'
45+
);
46+
});
47+
48+
it('cron-expression nzDisabled', () => {
49+
testComponent.nzDisabled = true;
50+
fixture.detectChanges();
51+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain(
52+
'ant-input-disabled'
53+
);
54+
});
55+
56+
it('cron-expression nzBorderless', () => {
57+
testComponent.nzBorderless = true;
58+
fixture.detectChanges();
59+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain(
60+
'ant-input-borderless'
61+
);
62+
});
63+
64+
it('cron-expression nzCollapseDisable', () => {
65+
expect(resultEl.nativeElement.querySelector('nz-cron-expression-preview')).not.toBe(null);
66+
testComponent.nzCollapseDisable = true;
67+
fixture.detectChanges();
68+
expect(resultEl.nativeElement.querySelector('nz-cron-expression-preview')).toBe(null);
69+
});
70+
71+
it('cron-expression nzExtra', () => {
72+
fixture.detectChanges();
73+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-map')).not.toBe(null);
74+
});
75+
76+
it('cron-expression nzSemantic', () => {
77+
fixture.detectChanges();
78+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-preview-dateTime').innerText).toBe('Test');
79+
});
80+
});
81+
82+
describe('type', () => {
83+
let testBed: ComponentBed<NzTestCronExpressionTypeComponent>;
84+
let fixture: ComponentFixture<NzTestCronExpressionTypeComponent>;
85+
let resultEl: DebugElement;
86+
87+
beforeEach(() => {
88+
testBed = createComponentBed(NzTestCronExpressionTypeComponent, {
89+
imports: [NzCronExpressionModule, HttpClientTestingModule]
90+
});
91+
fixture = testBed.fixture;
92+
fixture.detectChanges();
93+
resultEl = fixture.debugElement.query(By.directive(NzCronExpressionComponent));
94+
});
95+
96+
it('cron-expression type', () => {
97+
fixture.detectChanges();
98+
expect(resultEl.nativeElement.querySelectorAll('nz-cron-expression-input').length).toBe(6);
99+
expect(resultEl.nativeElement.querySelectorAll('nz-cron-expression-label').length).toBe(6);
100+
});
101+
});
102+
103+
describe('form', () => {
104+
let testBed: ComponentBed<NzTestCronExpressionFormComponent>;
105+
let fixture: ComponentFixture<NzTestCronExpressionFormComponent>;
106+
let testComponent: NzTestCronExpressionFormComponent;
107+
let resultEl: DebugElement;
108+
109+
beforeEach(() => {
110+
testBed = createComponentBed(NzTestCronExpressionFormComponent, {
111+
imports: [NzCronExpressionModule, HttpClientTestingModule, FormsModule, ReactiveFormsModule, NzFormModule]
112+
});
113+
fixture = testBed.fixture;
114+
fixture.detectChanges();
115+
testComponent = testBed.component;
116+
resultEl = fixture.debugElement.query(By.directive(NzCronExpressionComponent));
117+
});
118+
119+
it('cron-expression form', fakeAsync(() => {
120+
flush();
121+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).not.toContain(
122+
'ant-input-disabled'
123+
);
124+
testComponent.disable();
125+
fixture.detectChanges();
126+
flush();
127+
expect(resultEl.nativeElement.querySelector('.ant-cron-expression-input-group').classList).toContain(
128+
'ant-input-disabled'
129+
);
130+
}));
131+
});
132+
});
133+
134+
@Component({
135+
template: `
136+
<nz-cron-expression
137+
[nzSize]="nzSize"
138+
[nzCollapseDisable]="nzCollapseDisable"
139+
[nzDisabled]="nzDisabled"
140+
[nzBorderless]="nzBorderless"
141+
[nzExtra]="shortcuts"
142+
[nzSemantic]="semanticTemplate"
143+
></nz-cron-expression>
144+
<ng-template #shortcuts>
145+
<button nz-button nzType="primary">Test</button>
146+
</ng-template>
147+
<ng-template #semanticTemplate>Test</ng-template>
148+
`
149+
})
150+
export class NzTestCronExpressionComponent {
151+
nzSize: NzCronExpressionSize = 'default';
152+
nzDisabled = false;
153+
nzBorderless = false;
154+
nzCollapseDisable = false;
155+
}
156+
157+
@Component({
158+
template: ` <nz-cron-expression [nzType]="nzType"></nz-cron-expression> `
159+
})
160+
export class NzTestCronExpressionTypeComponent {
161+
nzType: 'linux' | 'spring' = 'spring';
162+
}
163+
164+
@Component({
165+
template: `
166+
<form [formGroup]="formGroup">
167+
<nz-cron-expression formControlName="cron"></nz-cron-expression>
168+
</form>
169+
`
170+
})
171+
export class NzTestCronExpressionFormComponent {
172+
formGroup: UntypedFormGroup;
173+
174+
constructor(private formBuilder: UntypedFormBuilder) {
175+
this.formGroup = this.formBuilder.group({
176+
cron: ['1 1 1 * *']
177+
});
178+
}
179+
180+
disable(): void {
181+
this.formGroup.disable();
182+
}
183+
}

0 commit comments

Comments
 (0)