-1
export default {
  retrieve() {...},
  process: {
    getData(source) {
      return this.retrieve({id: source.id})
         .then((reply) => {
              source.reply = reply;
              return reply
          });
   }
 }
}

Test

describe("getData", () => {
    const source = { id: 1 };
    it("calls #retrieve", () => {
        spyOn(helpers, "retrieve").and.returnValue(PromiseSpy);
        helpers.process.getData(source);
        expect(helpers.retrieve).toHaveBeenCalled();
    });
});

Error: undefined is not a constructor evaluating this.retrieve({id: source.id}).

Can anyone help with what I might be doing wrong?

Soviut
  • 88,194
  • 49
  • 192
  • 260
CodeBreaker
  • 95
  • 4
  • 12

1 Answers1

0

I have added a working example in stackblitz so you can play with it. If you want to mock a function you can use "and.callFake" in Jasmine.

spyOn(helpers, "fetch").and.callFake(
      () => new Promise((resolve, reject) => resolve({data: "mocked"}))
    );
const data = await helpers.getList(1);
expect(data).toBe("mocked"); 

Here is the link to stackblitz. https://stackblitz.com/edit/jasmine-testing-c2nicd?file=src%2Fhelpers.spec.ts

fernando
  • 707
  • 8
  • 24