Angular material 如何对角材料MatTableDataSource进行单元测试?

Angular material 如何对角材料MatTableDataSource进行单元测试?,angular-material,karma-jasmine,Angular Material,Karma Jasmine,有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行 /** * Setup the filter for a table * @param dataTable data source to setup */ private setupFilter(dataTable: MatTableDataSource<Element>) { dataTable.filterPredicate = (data: any, filter: string) =>

有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行

/**
 * Setup the filter for a table
 * @param dataTable data source to setup
 */
private setupFilter(dataTable: MatTableDataSource<Element>) {
    dataTable.filterPredicate = (data: any, filter: string) => {
        filter = filter.toLowerCase();
        return data.name.toLowerCase().includes(filter)
            || data.description.toString().includes(filter);
    };
}
/**
 * Checking control validation for edit inputs
 * @param value Equals to ngmodel for the input
 * @param column Equals to column name
 */
public editControlHasError(value: string, column: string): void {
    if (column === 'name') {
        this.errorInName.required = value === '';
        this.errorInName.maxlength = value.length > Constants.MAX_LENGTH_NAME;
        return;
    }
    this.errorInDescription.required = value === '';
    this.errorInDescription.maxlength = value.length > Constants.MAX_LENGTH_DESCRIPTION;
}
/**
 * Methode that apply the table filter
 * @param filterValue the filter value
 */
public applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}
/**
 * Method that loads a list of profiles to display on the rol select
 */
private loadProfiles() {
    this.loaders.dataSource = true;
    this.profilesService.getAllProfiles()
        .pipe(
            finalize(() => {
                this.loaders.dataSource = false;
                this.updateDOM();
            })
        )
        .subscribe(
            data => {
                this.dataSource = new MatTableDataSource<Element>(data);
                // getting properties from the object to sort the column from nested objects
                this.dataSource.sortingDataAccessor = (obj, property) => 
                this.getProperty(obj, property);
                this.dataSource.sort = this.sort;
                this.setupFilter(this.dataSource);
            }
        );
}
/**
 * Method that create a Profile
 */
public addProfile() {
    for (const key in this.profileForm.controls) {
        if (this.profileForm.controls[key] && 
            this.profileForm.controls[key].value.toString().trim() === '') {
            this.profileForm.controls[key].setValue('');
        }
    }
    if (this.profileForm.valid) {
        this.loaders.process = true;
        const profile: Profile = new Profile();
        profile.idProfile = 0;
        profile.name = this.profileForm.controls.name.value.trim();
        profile.description = this.profileForm.controls.description.value.trim();
        this.profilesService.addProfile(profile).subscribe(
            data => {
                this.loaders.process = false;
                this.profileForm.reset();
                this.loadProfiles();
                this.showModalAlert(data);
            }
        );
    }
}
/**
 * Method that loads a profile for edit from the list
 */
public loadEditProfile(element: Profile) {
    if (this.dataSource.data.some((e: any) => !!e.edit)) {
        this.cancelEdit();
        this.initErrorCheckers();
    }
    this.profileElementTmp = JSON.parse(JSON.stringify(element));
    this.profileElementAux = element;
    element.edit = true;
}
有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行

/**
 * Setup the filter for a table
 * @param dataTable data source to setup
 */
private setupFilter(dataTable: MatTableDataSource<Element>) {
    dataTable.filterPredicate = (data: any, filter: string) => {
        filter = filter.toLowerCase();
        return data.name.toLowerCase().includes(filter)
            || data.description.toString().includes(filter);
    };
}
/**
 * Checking control validation for edit inputs
 * @param value Equals to ngmodel for the input
 * @param column Equals to column name
 */
public editControlHasError(value: string, column: string): void {
    if (column === 'name') {
        this.errorInName.required = value === '';
        this.errorInName.maxlength = value.length > Constants.MAX_LENGTH_NAME;
        return;
    }
    this.errorInDescription.required = value === '';
    this.errorInDescription.maxlength = value.length > Constants.MAX_LENGTH_DESCRIPTION;
}
/**
 * Methode that apply the table filter
 * @param filterValue the filter value
 */
public applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}
/**
 * Method that loads a list of profiles to display on the rol select
 */
private loadProfiles() {
    this.loaders.dataSource = true;
    this.profilesService.getAllProfiles()
        .pipe(
            finalize(() => {
                this.loaders.dataSource = false;
                this.updateDOM();
            })
        )
        .subscribe(
            data => {
                this.dataSource = new MatTableDataSource<Element>(data);
                // getting properties from the object to sort the column from nested objects
                this.dataSource.sortingDataAccessor = (obj, property) => 
                this.getProperty(obj, property);
                this.dataSource.sort = this.sort;
                this.setupFilter(this.dataSource);
            }
        );
}
/**
 * Method that create a Profile
 */
public addProfile() {
    for (const key in this.profileForm.controls) {
        if (this.profileForm.controls[key] && 
            this.profileForm.controls[key].value.toString().trim() === '') {
            this.profileForm.controls[key].setValue('');
        }
    }
    if (this.profileForm.valid) {
        this.loaders.process = true;
        const profile: Profile = new Profile();
        profile.idProfile = 0;
        profile.name = this.profileForm.controls.name.value.trim();
        profile.description = this.profileForm.controls.description.value.trim();
        this.profilesService.addProfile(profile).subscribe(
            data => {
                this.loaders.process = false;
                this.profileForm.reset();
                this.loadProfiles();
                this.showModalAlert(data);
            }
        );
    }
}
/**
 * Method that loads a profile for edit from the list
 */
public loadEditProfile(element: Profile) {
    if (this.dataSource.data.some((e: any) => !!e.edit)) {
        this.cancelEdit();
        this.initErrorCheckers();
    }
    this.profileElementTmp = JSON.parse(JSON.stringify(element));
    this.profileElementAux = element;
    element.edit = true;
}
有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行 /** *清除“过滤器”值 */ clearFilters(){ this.filter=''; this.dataSource.filter=''; }

有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行

/**
 * Setup the filter for a table
 * @param dataTable data source to setup
 */
private setupFilter(dataTable: MatTableDataSource<Element>) {
    dataTable.filterPredicate = (data: any, filter: string) => {
        filter = filter.toLowerCase();
        return data.name.toLowerCase().includes(filter)
            || data.description.toString().includes(filter);
    };
}
/**
 * Checking control validation for edit inputs
 * @param value Equals to ngmodel for the input
 * @param column Equals to column name
 */
public editControlHasError(value: string, column: string): void {
    if (column === 'name') {
        this.errorInName.required = value === '';
        this.errorInName.maxlength = value.length > Constants.MAX_LENGTH_NAME;
        return;
    }
    this.errorInDescription.required = value === '';
    this.errorInDescription.maxlength = value.length > Constants.MAX_LENGTH_DESCRIPTION;
}
/**
 * Methode that apply the table filter
 * @param filterValue the filter value
 */
public applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}
/**
 * Method that loads a list of profiles to display on the rol select
 */
private loadProfiles() {
    this.loaders.dataSource = true;
    this.profilesService.getAllProfiles()
        .pipe(
            finalize(() => {
                this.loaders.dataSource = false;
                this.updateDOM();
            })
        )
        .subscribe(
            data => {
                this.dataSource = new MatTableDataSource<Element>(data);
                // getting properties from the object to sort the column from nested objects
                this.dataSource.sortingDataAccessor = (obj, property) => 
                this.getProperty(obj, property);
                this.dataSource.sort = this.sort;
                this.setupFilter(this.dataSource);
            }
        );
}
/**
 * Method that create a Profile
 */
public addProfile() {
    for (const key in this.profileForm.controls) {
        if (this.profileForm.controls[key] && 
            this.profileForm.controls[key].value.toString().trim() === '') {
            this.profileForm.controls[key].setValue('');
        }
    }
    if (this.profileForm.valid) {
        this.loaders.process = true;
        const profile: Profile = new Profile();
        profile.idProfile = 0;
        profile.name = this.profileForm.controls.name.value.trim();
        profile.description = this.profileForm.controls.description.value.trim();
        this.profilesService.addProfile(profile).subscribe(
            data => {
                this.loaders.process = false;
                this.profileForm.reset();
                this.loadProfiles();
                this.showModalAlert(data);
            }
        );
    }
}
/**
 * Method that loads a profile for edit from the list
 */
public loadEditProfile(element: Profile) {
    if (this.dataSource.data.some((e: any) => !!e.edit)) {
        this.cancelEdit();
        this.initErrorCheckers();
    }
    this.profileElementTmp = JSON.parse(JSON.stringify(element));
    this.profileElementAux = element;
    element.edit = true;
}
有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行

/**
 * Setup the filter for a table
 * @param dataTable data source to setup
 */
private setupFilter(dataTable: MatTableDataSource<Element>) {
    dataTable.filterPredicate = (data: any, filter: string) => {
        filter = filter.toLowerCase();
        return data.name.toLowerCase().includes(filter)
            || data.description.toString().includes(filter);
    };
}
/**
 * Checking control validation for edit inputs
 * @param value Equals to ngmodel for the input
 * @param column Equals to column name
 */
public editControlHasError(value: string, column: string): void {
    if (column === 'name') {
        this.errorInName.required = value === '';
        this.errorInName.maxlength = value.length > Constants.MAX_LENGTH_NAME;
        return;
    }
    this.errorInDescription.required = value === '';
    this.errorInDescription.maxlength = value.length > Constants.MAX_LENGTH_DESCRIPTION;
}
/**
 * Methode that apply the table filter
 * @param filterValue the filter value
 */
public applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}
/**
 * Method that loads a list of profiles to display on the rol select
 */
private loadProfiles() {
    this.loaders.dataSource = true;
    this.profilesService.getAllProfiles()
        .pipe(
            finalize(() => {
                this.loaders.dataSource = false;
                this.updateDOM();
            })
        )
        .subscribe(
            data => {
                this.dataSource = new MatTableDataSource<Element>(data);
                // getting properties from the object to sort the column from nested objects
                this.dataSource.sortingDataAccessor = (obj, property) => 
                this.getProperty(obj, property);
                this.dataSource.sort = this.sort;
                this.setupFilter(this.dataSource);
            }
        );
}
/**
 * Method that create a Profile
 */
public addProfile() {
    for (const key in this.profileForm.controls) {
        if (this.profileForm.controls[key] && 
            this.profileForm.controls[key].value.toString().trim() === '') {
            this.profileForm.controls[key].setValue('');
        }
    }
    if (this.profileForm.valid) {
        this.loaders.process = true;
        const profile: Profile = new Profile();
        profile.idProfile = 0;
        profile.name = this.profileForm.controls.name.value.trim();
        profile.description = this.profileForm.controls.description.value.trim();
        this.profilesService.addProfile(profile).subscribe(
            data => {
                this.loaders.process = false;
                this.profileForm.reset();
                this.loadProfiles();
                this.showModalAlert(data);
            }
        );
    }
}
/**
 * Method that loads a profile for edit from the list
 */
public loadEditProfile(element: Profile) {
    if (this.dataSource.data.some((e: any) => !!e.edit)) {
        this.cancelEdit();
        this.initErrorCheckers();
    }
    this.profileElementTmp = JSON.parse(JSON.stringify(element));
    this.profileElementAux = element;
    element.edit = true;
}
有些方法是有棱角的材料,我需要对每种方法进行单元测试,但我不知道如何进行

/**
 * Setup the filter for a table
 * @param dataTable data source to setup
 */
private setupFilter(dataTable: MatTableDataSource<Element>) {
    dataTable.filterPredicate = (data: any, filter: string) => {
        filter = filter.toLowerCase();
        return data.name.toLowerCase().includes(filter)
            || data.description.toString().includes(filter);
    };
}
/**
 * Checking control validation for edit inputs
 * @param value Equals to ngmodel for the input
 * @param column Equals to column name
 */
public editControlHasError(value: string, column: string): void {
    if (column === 'name') {
        this.errorInName.required = value === '';
        this.errorInName.maxlength = value.length > Constants.MAX_LENGTH_NAME;
        return;
    }
    this.errorInDescription.required = value === '';
    this.errorInDescription.maxlength = value.length > Constants.MAX_LENGTH_DESCRIPTION;
}
/**
 * Methode that apply the table filter
 * @param filterValue the filter value
 */
public applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
}
/**
 * Method that loads a list of profiles to display on the rol select
 */
private loadProfiles() {
    this.loaders.dataSource = true;
    this.profilesService.getAllProfiles()
        .pipe(
            finalize(() => {
                this.loaders.dataSource = false;
                this.updateDOM();
            })
        )
        .subscribe(
            data => {
                this.dataSource = new MatTableDataSource<Element>(data);
                // getting properties from the object to sort the column from nested objects
                this.dataSource.sortingDataAccessor = (obj, property) => 
                this.getProperty(obj, property);
                this.dataSource.sort = this.sort;
                this.setupFilter(this.dataSource);
            }
        );
}
/**
 * Method that create a Profile
 */
public addProfile() {
    for (const key in this.profileForm.controls) {
        if (this.profileForm.controls[key] && 
            this.profileForm.controls[key].value.toString().trim() === '') {
            this.profileForm.controls[key].setValue('');
        }
    }
    if (this.profileForm.valid) {
        this.loaders.process = true;
        const profile: Profile = new Profile();
        profile.idProfile = 0;
        profile.name = this.profileForm.controls.name.value.trim();
        profile.description = this.profileForm.controls.description.value.trim();
        this.profilesService.addProfile(profile).subscribe(
            data => {
                this.loaders.process = false;
                this.profileForm.reset();
                this.loadProfiles();
                this.showModalAlert(data);
            }
        );
    }
}
/**
 * Method that loads a profile for edit from the list
 */
public loadEditProfile(element: Profile) {
    if (this.dataSource.data.some((e: any) => !!e.edit)) {
        this.cancelEdit();
        this.initErrorCheckers();
    }
    this.profileElementTmp = JSON.parse(JSON.stringify(element));
    this.profileElementAux = element;
    element.edit = true;
}