ngx-formly / ngx-formly

📝 JSON powered / Dynamic forms for Angular
https://formly.dev
MIT License
2.81k stars 565 forks source link

How can I write unit test in my own form-ui-library? #1965

Closed hellojayjay closed 4 years ago

hellojayjay commented 4 years ago

Question

I create my own formly library based on @ngx-formly/core and ng-zorro-antd, and now I want to write its unit test. I refer to https://github.com/ngx-formly/ngx-formly/blob/92d4cfeffac36a0ee33dc6d9f9ee87554606ec17/src/bootstrap/src/lib/types/select.spec.ts and https://github.com/ngx-formly/ngx-formly/blob/f5625ea31405c1e432b155a4c72ed8302ef2c79c/src/material/select/src/select.spec.ts but these two files pass unit test by testing the doms whether has specific dom, and my form components class in my formly library has their method. so I want to test these method in form components, How can I do to solve this? or how can I get the instance of form component in unit test?

aitboudad commented 4 years ago

you can get the component instance using query fn:

fixture.debugElement.query(By.css('formly-field-input')).componentInstance

We've improved our testing in the master branch by introducing createFormlyFieldComponent helper (it'll be part of v6).

for ng-zorro-antd we've made an initial work which you may check:

aitboudad commented 4 years ago

Note: published an early version 0.0.1 for @ngx-formly/ng-zorro-antd which you can use with v5, a stable version is expected in formly@v6.

Happy coding!

loxy commented 3 years ago

I want to use import { createFormlyFieldComponent } from '@ngx-formly/core/testing'; But it is still impossible to import createFormlyFieldComponent. When will it be available?

aitboudad commented 3 years ago

When will it be available?

yes but in V6 (we'll release an alpha version soon)

aitboudad commented 3 years ago

you can copy the code from src/core/testing/src/component-factory.ts as helper in your project:

loxy commented 3 years ago

I did that. But I had to adapted to this:

useFactory: (builder: FormlyFormBuilder) => () => {
  builder.buildForm(new FormGroup({}), [field], model, options);
  return of(null);
},

Because function build is not available...

zzhimin commented 2 weeks ago

you can also use this library:https://github.com/zzhimin/ngx-ng-zorro-formly