Как написать модульный тест для диалогового окна с использованием жасмина и Angular 7? - PullRequest
0 голосов
/ 22 октября 2019

Я пишу модульный тест для метода в компоненте, который открывает диалоговое окно. Попытка написания следующего модульного теста, чтобы убедиться, что диалоговое окно открывается. Но я получаю сообщение об ошибке.

component.ts:

У него есть метод onupload.

onUpload(event: FileUpload) {
    this.Service
        .get(event.id)
        .subscribe((data: Data[]) => {
            const dialogRef = this.dialog.open(
                DialogComponent,
                {data,}    
            );

            dialogRef.afterClosed().subscribe({
                next: (Id: string) => {
                    if (Id === null {
                        this.reset();
                    }

unittest.spec.ts:

describe('open()', () => {
    it('should open the dialog', () => {
        const testCases = [
            {
                returnValue: 'Successfully opens a dialog',
                isSuccess: true
            },
            {
                returnValue: 'cancel',
                isSuccess: false
            },
        ];

        testCases.forEach(testCase => {
            it(`should open the file upload matDialog `, () => {
                const returnedVal = {
                    afterClosed: () => of(testCase.returnValue)
                };
                spyOn(component, 'reset');
                spyOn(component['matDialog'], 'open').and.returnValue(returnedVal);
                component.onUpload(new FileUpload(
                    'fid',
                    'oid',
                ));

                if (testCase.isSuccess) {
                    expect(component.reset).toHaveBeenCalled();
                } else {
                    expect(component.reset).not.toHaveBeenCalled();
                }

                expect(component['matDialog'].open).not.toHaveBeenCalled();
            });
        });

Я получаю сообщение об ошибке "'mat-grid-tile' не является известным элементом: 1. Если 'mat-grid-tile' является угловым компонентом, то убедитесь, что он является частью этого модуля.". Пожалуйста помоги. Спасибо.

1 Ответ

0 голосов
/ 22 октября 2019

«Ошибка:« it »следует использовать только в функции« description »» относится к вложенной функции it. Если вы удалите вложенный оператор it, вы должны избавиться от ошибки.

describe('open()', () => {
    // it('should open the dialog', () => { // get rid of this
    const testCases = [
    ...
...