0

我正在使用https://testing-library.com/进行测试

这是我的反应形式:

this.createForm = this.formBuilder.group({
 'Id': new FormControl({ value: 0, disabled: true }, [Validators.required]),
 'Name': new FormControl('', { validators: [ValidationService.required, ValidationService.SubsystemMxLenght, ValidationService.SubsystemPatternMatch], updateOn: 'blur' }),
'UpdatedByName': new FormControl({ value: this.appUserName, disabled: true }, []),
 'UpdatedDate': new FormControl({ value: '', disabled: true }, [])

});

根据图书馆的建议,我将值设置为这样的表单字段:

component.input(component.getByTestId('form-create-name-field-1'), {
        target: {
            value: data.Name /* value not setting since we use updateOn value, so it's null */
        }
    });

    component.input(component.getByTestId('form-create-name-field-2'), {
        target: {
            value: data.UpdatedByName
        }
    });

在其中我无法使用 设置值Name,如果我updateOn: 'blur'从表单中删除参数 - 它会设置。如何解决这个问题?否则我的表格有什么问题?

有人帮帮我吗?

更新: 组件功能:

onCreateFormSubmit() {

 this.ssCreated.emit(created);

}

测试onCreateFormSubmitssCreated- 但ssCreated不起作用:

规格文件:

test('Testing add Subsystem operation', async () => {

    const data = {
        Id: 2,
        Name: 'subsystem2',
        IsDeletePossible: true,
        CreatedBy: '',
        CreatedDate: new Date(),
        UpdatedBy: '',
        UpdatedDate: new Date(),
        UpdatedByName: 'test value',
        CreatedByName: ''
    } as ModelSubSystem;

    const ssCreated = jest.fn();

    const component = await render(SubSystemComponent, {
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        imports: [
            HttpClientTestingModule,
            FormsModule,
            ReactiveFormsModule,
            StoreModule.forRoot({}, { runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true } }),
            StoreModule.forFeature('pfservice', reducer),
            EffectsModule.forRoot([]),
            EffectsModule.forFeature([EffectsSubSystem])
        ],
        componentProperties: {
            onCreateFormSubmit: jest.fn(),
            ssCreated: {
                emit: data
            } as any,
        }

    });

    const componentInstance = component.fixture.componentInstance;


    /*
     *   Testing the form by DOM.
     */

    const createButton = component.getByTestId('btn-addRow');
    component.click(createButton);
    component.fixture.detectChanges();
    // status changes because of click on button
    expect(componentInstance.crud.isCreate).toBeTruthy();

    component.fixture.detectChanges();
    // onclick submit should not called, becasue of empty input

    component.input(component.getByTestId('form-create-name-field-0'), {
        target: {
            value: data.Id
        }
    });

    component.input(component.getByTestId('form-create-name-field-1'), {
        target: {
            value: data.Name
        }
    });


    component.blur(component.getByTestId('form-create-name-field-1'));

    component.input(component.getByTestId('form-create-name-field-2'), {
        target: {
            value: data.UpdatedByName
        }
    });

    const submit = component.getAllByTestId('form-create-btn')[0];

    component.click(submit);

    component.fixture.detectChanges();
    const name = componentInstance.createForm.controls['Name'];
    const updatedByName = componentInstance.createForm.controls['UpdatedByName'];
    expect(name.value).toEqual(data.Name);
    expect(name.errors).toBeFalsy();
    expect(updatedByName.value).toEqual(data.UpdatedByName);
    expect(componentInstance.onCreateFormSubmit).toBeCalled(); //works

    // console.log(componentInstance.createForm.getRawValue());
    console.log(data);

    expect(ssCreated).toHaveBeenCalledWith(data); //not works

});
4

1 回答 1

1

updateOne只会在输入模糊时触发,这就是您必须这样做的原因:

component.input(component.getByTestId('form-create-name-field-1'), {
        target: {
            value: data.Name
        }
    });

// this line here
component.blur(component.getByTestId('form-create-name-field-1'));

我在这里创建了一个示例https://github.com/testing-library/angular-testing-library/commit/6989a66cfe2d0dfb66dcbb9566bf122307edca6c

于 2019-11-12T14:02:57.383 回答