5

Basically I have a component that look like this:

export class CmpB extends CmpA {
    variableA:any = {};
    @Input() config:any = {};
    constructor(private serviceA: serviceA,
        @Optional() private CmpC?: CmpC) {
        super();
    }
    ngOnInit() {
        if (this.CmpC) {
            super.doSomething(parameterA);
            //other stuff
        }
    }
}

so basically I got CmpB that extends CmpA (which mutates variableA depending on the config) and in order to do something it needs a specific parent component CmpC. ...And I need to write a test for it that actually verifies the CmpA mutations.

my test looks like this

describe('CmpB', () => {
    let mock: any = MockComponent({ selector: "app-element", template: "<CmpC><CmpB></CmpB></CmpC>" });
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [mock, CmpC, CmpB],
            providers: [
                ServiceA
            ]
        }).compileComponents();
    });

    it('CmpB component test', async(() => {
        const appMock = TestBed.createComponent(mock);
        appMock.detectChanges();
        const el = appMock.debugElement.nativeElement as HTMLElement;
        expect(el.tagName).toEqual("DIV");
    }));
}

which works and in terms of coverage it go through the whole thing. But what I need is to manually set the config input and see how variableA mutates; I can't figure out how though.

mock component originates from the following code

    import { Component } from '@angular/core';

export function MockComponent(options: Component): Component {
  let metadata: Component = {
    selector: options.selector,
    template: options.template || '',
    inputs: options.inputs,
    outputs: options.outputs
  };

  class Mock {}

  return Component(metadata)(Mock);
}
elasticrash
  • 1,181
  • 1
  • 12
  • 30

0 Answers0