我如何在 Angular 8 中获取我的 UI 组件
How do i get my UI components in Angular 8
我正在使用自己创建的组件。 app-button.ts
的作用是检查当前用户角色,然后在匹配定义角色时呈现。
ButtonType只是定义使用什么样式。
demo.ts:
<app-button
buttonText="BACK"
(clickEvent)="goBack()"
[buttonType]="'secondary'"
>
</app-button>
app-button 按钮实现:
<button *ngIf="canRender" class="{{ buttonClass }}" (click)="onClickEvent()" [disabled]="disabled">
<span class="{{ spanClass }}">{{ buttonText }}</span>
</button>
应用程序-button.ts 文件:
export class PrimaryButtonComponent implements OnInit {
@Input() buttonText: string;
@Input() disabled? = false;
@Input() allowedRoles: Role[] = [];
@Input() buttonType = 'primary';
canRender? = false;
buttonClass: string;
spanClass: string;
@Output() clickEvent = new EventEmitter();
constructor(private credentialsService: CredentialsService) {}
ngOnInit() {
if (this.allowedRoles.length !== 0) {
const currentUserRoles: Role[] = this.credentialsService.currentUserValue.role.slice();
this.canRender = allowedToAccess(this.allowedRoles, currentUserRoles);
} else {
this.canRender = true;
}
if (this.buttonType === 'primary') {
this.buttonClass = 'primary-button';
this.spanClass = 'primary-button-text';
}
if (this.buttonType === 'secondary') {
this.buttonClass = 'secondary-button';
this.spanClass = 'secondary-button-text';
}
}
onClickEvent() {
this.clickEvent.emit();
}
}
单元测试:
fit('should navigate back to dashboard when click back button', async(() => {
const onClickSpy = spyOn(component, 'goBack').and.callThrough();
const routerSpy = spyOn(router, 'navigate');
// Click Back button
const button = fixture.debugElement.query(By.css('.secondary-button'));
expect(button).not.toBeNull();
}));
当我进行单元测试时,我看到了这个错误
Error: Expected null not to be null.
但我的实际 UI 显示的是组件。怎么会在测试中为空?
在TestBed.configureTestingModule
中,在declarations
数组中,添加ButtonComponent
(假设ButtonComponent
就是所谓的class)。完成此操作后,expect(button).not.toBeNull();
就可以开始了。
我正在使用自己创建的组件。 app-button.ts
的作用是检查当前用户角色,然后在匹配定义角色时呈现。
ButtonType只是定义使用什么样式。
demo.ts:
<app-button
buttonText="BACK"
(clickEvent)="goBack()"
[buttonType]="'secondary'"
>
</app-button>
app-button 按钮实现:
<button *ngIf="canRender" class="{{ buttonClass }}" (click)="onClickEvent()" [disabled]="disabled">
<span class="{{ spanClass }}">{{ buttonText }}</span>
</button>
应用程序-button.ts 文件:
export class PrimaryButtonComponent implements OnInit {
@Input() buttonText: string;
@Input() disabled? = false;
@Input() allowedRoles: Role[] = [];
@Input() buttonType = 'primary';
canRender? = false;
buttonClass: string;
spanClass: string;
@Output() clickEvent = new EventEmitter();
constructor(private credentialsService: CredentialsService) {}
ngOnInit() {
if (this.allowedRoles.length !== 0) {
const currentUserRoles: Role[] = this.credentialsService.currentUserValue.role.slice();
this.canRender = allowedToAccess(this.allowedRoles, currentUserRoles);
} else {
this.canRender = true;
}
if (this.buttonType === 'primary') {
this.buttonClass = 'primary-button';
this.spanClass = 'primary-button-text';
}
if (this.buttonType === 'secondary') {
this.buttonClass = 'secondary-button';
this.spanClass = 'secondary-button-text';
}
}
onClickEvent() {
this.clickEvent.emit();
}
}
单元测试:
fit('should navigate back to dashboard when click back button', async(() => {
const onClickSpy = spyOn(component, 'goBack').and.callThrough();
const routerSpy = spyOn(router, 'navigate');
// Click Back button
const button = fixture.debugElement.query(By.css('.secondary-button'));
expect(button).not.toBeNull();
}));
当我进行单元测试时,我看到了这个错误
Error: Expected null not to be null.
但我的实际 UI 显示的是组件。怎么会在测试中为空?
在TestBed.configureTestingModule
中,在declarations
数组中,添加ButtonComponent
(假设ButtonComponent
就是所谓的class)。完成此操作后,expect(button).not.toBeNull();
就可以开始了。