guillotinaweb / ngx-schema-form

HTML form generation based on JSON Schema
MIT License
483 stars 173 forks source link

Mocking FormProperty #274

Open sammaphey opened 5 years ago

sammaphey commented 5 years ago

I am attempting to test a custom widget and am running into problems regarding the formProperty.

-- component:

  ngAfterViewInit() {
    if (this.formProperty.value) {
      this.getDoc(this.formProperty.value);
    } else {
      this.edit = true;
    }
  }

-- spec


      let searchComponent: SearchComponent;

      beforeEach(() => {
        component.edit = true;
        component.schema.widget = new StringWidget();

        fixture.detectChanges();

        searchComponent = fixture.debugElement.query(By.css('app-search')).componentInstance as SearchComponent;
      });

      it('should initialize image search with fromExt', () => {
        expect(searchComponent.fromExt).toEqual(true);
      });

-- error: TypeError: Cannot read property 'value' of undefined

I believe that the error is coming from the components formProperty not being defined. I have attempted to mock it but FormProperty is an abstract class so I cannot do that.

Any suggestions?

WhileTrueEndWhile commented 5 years ago

Have you found a solution? If this issue is still open, take a look at formpropertyfactory.ts. Here you will also find an overview of the instantiatable derived classes.

Jiehong commented 5 years ago

One way to bypass this issue is to wrap your this.formProperty into a getter, and then using jasmine to intercept the call, and inject the mocked value (with something like spyOn(myWidget, 'getFormProperty').and.returnValue(mockFormProperty as ArrayProperty); for example)