0

I am new to angular and unit testing, I am trying to write a unit test for the component with service where the service url is fetched from the @Input() value.

cms-sonor.component.ts

export class CmsSonarComponent implements OnInit {
    @Input() configParam;
    sonarData;
    constructor(public cmsService: CmsService) {}

    ngOnInit(): void {
        this.cmsService
            .getData(this.configParam.url, { responseType: "text" })
            .subscribe(
                (resp) => {
                    this.sonarData = resp;
                },
                (err) => {
                    console.log(err);
                }
            );
    }
}

cms-sonar.component.spec.ts

describe("CmsSonarComponent", () => {
    let component: CmsSonarComponent;
    let injector: TestBed;
    let fixture: ComponentFixture<CmsSonarComponent>;
    let service: CmsService;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            declarations: [CmsSonarComponent, SonorPipe],
            providers: [
                {
                    provide: CmsService,
                    useValue: {
                        getData: () => of({ id: 123, name: "Product" }),
                    },
                },
            ],
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(CmsSonarComponent);
        service = TestBed.get(CmsService);
        component = fixture.componentInstance;
        //fixture.detectChanges();
    });

    it("should create", () => {
        expect(component).toBeDefined();
    });

    it("testing cms sonar ngOnInit", () => {
        spyOn(service, "getData").and.callThrough();
        component.ngOnInit();
        fixture.detectChanges();
        expect(service.getData).toHaveBeenCalledWith("/getData", "json");
        expect(component.sonarData).toEqual({ id: 123, name: "Product" });
    });
});

Error TypeError: Cannot read property 'url' of undefined

Can you let me known what mistake I am making? Any help is appreciated.

i even try with args like this spyOn(service, 'getData') .withArgs('abc')

Update-1 I was able resolve the above issue, URL was try to getting the input value.

const configParam = {
    url: 'visible'
};
beforeEach(() => {
        fixture = TestBed.createComponent(CmsSonarComponent);
        service = TestBed.get(CmsService);
        component = fixture.componentInstance;
        component.configParam = configParam;
        //fixture.detectChanges();
    });
it('testing cms sonar ngOnInit', () => {
        spyOn(service, 'getData').and.callThrough();
        component.ngOnInit();
        fixture.detectChanges();
        expect(service.getData).toHaveBeenCalledWith('visible', {
            responseType: 'text'
        });
        expect(component.sonarData).toEqual({ id: 123, name: 'Product' });
    });

But now i am get this error

Expected spy getData to have been called with:
  [ 'visible', Object({ responseType: 'text' }) ]
but actual calls were:
  [ 'visible' ],
  [ 'visible' ]

any guides on this is highly appreciated.

Prasad A U
  • 25
  • 1
  • 10
  • Does this answer your question? [Angular2 unit test with @Input()](https://stackoverflow.com/questions/36654834/angular2-unit-test-with-input) As far as I can tell, your issue is that the `@Input` isn't being provided within your test, which is what those answers cover. – Tim Jul 01 '20 at 09:48
  • @Tim, no exactly even after adding the 'component.configParam.url = '/getData';' in my test case i am still getting the same error. – Prasad A U Jul 01 '20 at 10:12
  • @Yeswhen even after that `useValue: { getData: url => of({ id: 123, name: 'Product' }) }` its still same error – Prasad A U Jul 01 '20 at 13:20
  • Hi @Yeswhen for the first error i passed the input value for the second error i changed the component itself. – Prasad A U Jul 06 '20 at 07:10

0 Answers0