angularangular-testangular-testing-library

Not able to set input value, when I use 'updateOn' attribute with form Control


I am doing testing using https://testing-library.com/

Here is my Reactive form:

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 }, [])

});

from the library's recommendation, I am setting value to form field like this:

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
        }
    });

In which I am not able to set the value with Name, If I remove the param updateOn: 'blur' - from the form, it sets. how to fix this? what is the issue with my form otherwise?

any one help me please?

UPDATE: component function:

onCreateFormSubmit() {

 this.ssCreated.emit(created);

}

testing both onCreateFormSubmit and ssCreated - but ssCreated not works:

spec file:

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

});

Solution

  • updateOne will only fire when the input is blurred, that's why you'll have to do:

    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'));
    

    I created an example here https://github.com/testing-library/angular-testing-library/commit/6989a66cfe2d0dfb66dcbb9566bf122307edca6c