Mock Dexie's .open() for unit tests

48 views Asked by At

I'm trying to write a test for my class that extends Dexie. I need to mock a positive response from the this.open() so that my code will run, but I can't work out what it needs. open() appears to return a PromiseExtended but I can't create one

export class DbService extends Dexie {

....

this.open()
          .then(data => {
            this.loggerService.logInfo('Opened indexedDB');
            this.isReady = true;
            resolve();
          })

....
}

and the test so far

 it('should be ready if indexedDB opens', waitForAsync(async() => {

    const myDatabase = new Dexie('MyDatabase');
    spyOn(service, 'open').and.returnValue(Promise.resolve(myDatabase));
    const wasReady = await service.ready()


    expect(wasReady).toBeTruthy();
    expect(service.open).toHaveBeenCalledTimes(1);
}));
0

There are 0 answers