简体   繁体   English

Component extends with super class 需要帮助来测试

[英]Component extends with super class needs help to test

Here is my base class:这是我的基地 class:

export class DetailsComponent
extends AddClinicComponent
implements OnInit, AfterViewInit{}

in the spec of DetailsComponent.spec I am tryin to write a testing, which is in AddClinicComponent component.DetailsComponent.spec的规范中,我正在尝试编写一个测试,它位于AddClinicComponent组件中。

here is the test:这是测试:

fit('it should open popup  on click of button add clinic button ', async(() => {
    spyOn(component, 'openDialog');
    fixture.detectChanges();
    const removeButton =
        fixture.debugElement.nativeElement.querySelector('.hfs-add-btn');
    expect(removeButton).toBeTruthy();
    let event = new MouseEvent('click');
    removeButton.dispatchEvent(event);
    fixture.whenStable().then(() => {
        expect(component.openDialog).toHaveBeenCalled();
    });
}));

But getting an error as:但是得到一个错误:

Error: Directive AddClinicComponent has no selector, please add it! how to resolve it?如何解决? what is the correct way to test the extended component?测试扩展组件的正确方法是什么?

my super class:我的超级 class:

import { TemplateRef, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { AddClinicDataService } from '../services/personnel-add-clinic-data.service';
import { HFSClinicTaleSchema } from './hfs-clinic-table.schema';
export abstract class AddClinicComponent {
    popTableSchema;
    clinicTaleSchema;
    @ViewChild('popupTemp', { static: true })
    popupTemp: TemplateRef<HTMLAllCollection>;
    public primaryName = '';
    public primaryAdminId = '';
    public dialog: MatDialog;

    constructor(protected aAddClinicDataService: AddClinicDataService) {
        this.clinicTaleSchema = HFSClinicTaleSchema;
    }

    clinicRowGenerator(rows) {
        return rows.map((p) => {
            return {
                ...p,
                address: Object.values(p.address).filter((v) => v != null),
                transform: true,
            };
        });
    }

    handleAddClinic() {
        this.popTableSchema = { ...HFSClinicTaleSchema, rows: [] };
        this.openDialog(this.popupTemp);
    }
    openDialog(templateRef: TemplateRef<HTMLAllCollection>) {
        const dialogRef = this.dialog.open(templateRef);
        dialogRef.afterClosed().subscribe((result) => {
            console.log(`Dialog result: ${result}`);
        });
    }

    addClinics() {
        const { rows } = this.popTableSchema;
        const transform = [...rows]
            .filter((r) => r.checked)
            .map((item) => ({ ...item, checked: false }));
        if (!transform.length) return;
        this.rowTransformer(transform);
        this.dialog.closeAll();
    }

    searchClinics($event: Event): void {
        $event.preventDefault();
        const formDetails = this.postProfile();
        const countryCodes = formDetails.countries.map((v) => v.codeId);
        const searchParams = {
            primaryAdminId: this.primaryAdminId,
            primaryName: this.primaryName,
            countryCodes: countryCodes.join(','),
        };
        this.fetchClinicList(
            `name=${this.primaryName}&adminUserName=${
                this.primaryAdminId
            }&countryCds=${countryCodes.join(',')}`
        );
        this.primaryAdminId = '';
        this.primaryName = '';
    }
    fetchClinicList(searchParams) {
        this.aAddClinicDataService
            .getClinicsList(searchParams)
            .subscribe((data) => {
                const nwRows = data?.map((d) => {
                    return {
                        ...d,
                        address: Object.values(d.address).filter(
                            (v) => v !== null
                        ),
                    };
                });
                this.popTableSchema = { ...this.popTableSchema, rows: nwRows };
            });
    }
    protected abstract rowTransformer(value);
    abstract postProfile();
}

after having some try: I got this error:尝试后:我收到此错误:

 Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
        This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

        Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.

constructor of details component:细节组件的构造函数:

constructor(
        private personnelViewDataService: PersonnelViewDataService,
        private personnelTranslateService: PersonnelTranslateService,
        private cdr: ChangeDetectorRef,
        public dialog: MatDialog,
        protected aAddClinicDataService: AddClinicDataService
    ) {
        super();
    }

constructor of AddClinicComponent AddClinicComponent的构造函数

 constructor(protected aAddClinicDataService: AddClinicDataService) {
        this.clinicTaleSchema = HFSClinicTaleSchema;
    }

Dependency injection works only if the class has an Angular decorator.仅当 class 具有 Angular 装饰器时,依赖注入才有效。

DI is wired into the Angular framework and allows classes with Angular decorators, such as Components, Directives, Pipes, and Injectables, to configure dependencies that they need. DI 连接到 Angular 框架,并允许具有 Angular 装饰器的类(例如组件、指令、管道和可注入对象)配置它们需要的依赖项。

https://angular.io/guide/dependency-injection https://angular.io/guide/dependency-injection

This could be causing the error, try to inject the service in the component class and when calling super pass the service to the extended class这可能是导致错误的原因,尝试在组件 class 中注入服务,并在调用 super 时将服务传递给扩展的 class

constructor of details component:细节组件的构造函数:

constructor(
  private personnelViewDataService: PersonnelViewDataService,
  private personnelTranslateService: PersonnelTranslateService,
  private cdr: ChangeDetectorRef,
  public dialog: MatDialog,
  public aAddClinicDataService: AddClinicDataService
) {
  super(aAddClinicDataService);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM