How to mock & Inject functions with dependencies in my case httpsCallable from rxfire for unit test

44 Views Asked by At

I am unable to find any docs with any instructions to mock a function I importing from other files like httpsCallable function in the example below, to create unit tests for my Angular Webapp using Jasmine and Karma. I am using it in a smart component where I am calling an endpoint to submit form. I would also like to know how to mock and inject functions with its own dependencies.

this is how my function look like

import {httpsCallable} from 'rxfire';
...
saveForm(data: Object) {
        httpsCallable<any, any>(
            this.functions,
            'api/endpoint',
        )(data).subscribe({
            next: (res) => {
                console.log(res);
            },
            error: (error) => {
                console.log(error);
            },
        });
    }

I tried mocking it in many different ways and always got functionInstance._url is not a function error, i think its something to do with this.function instance. I am unable to inject it. I want to test each response the calls would receive.

I even tried mocking the rxfire package and its method httpsCallable using createSpyObj.

type RequestData = unknown;
type ResponseData = unknown;
let mockRxFire = spyOnProperty<any>(rxFire, 'httpsCallable').and.returnValue((functions: Functions, name: string) => {
       return (data?: RequestData | null | undefined) => {
           const responseData = { message: `Data for  updated` };
           const response: ResponseData = { status: 200, data: responseData };
           return of(response);
       };
   });

but unable to inject.

Thank you for your time.

0

There are 0 best solutions below