Home > Mobile >  Expect object length to be null
Expect object length to be null

Time:08-05

I have the following Object where I need to test it to be null:

  getLastTeamUpdatedItemLogBuffer(): IBufferElement {
const storageItems = this.storageSvc.getItem(StorageKey.lastTeamUpdatedItem, true) as IBufferElement;
return storageItems || null;

}

Here is IbufferElement:

export interface IBufferElement {
  timestamp: number;
  text: string;
}

I succeed in testing return storageItems but I can't do it on return null Here is return storageItems:

it('should return value from storage, if available', () => {
  storageSvc.getItem = jasmine.createSpy().and.returnValue([{}, {}]);
  expect(Object.keys(service.getLastTeamUpdatedItemLogBuffer()).length).toBe(2);
});

Here is what I'm trying for return null but it's not working:

it('should return empty, if storage value is null', () => {
  storageSvc.getItem = jasmine.createSpy().and.returnValue([]);
  expect(Object.keys(service.getLastTeamUpdatedItemLogBuffer()).length).toBe(0);
});

I also tried it with "toBeFalsy()" or "toBe(null)" and returnValue(null) or returnValue([null]) but I don't understand what I am doing wrong.

CodePudding user response:

I think the || null is not covered.

To cover it, do:

storageSvc.getItem = jasmine.createSpy().and.returnValue(undefined); and then expect(service.getLastTeamUpdatedItemLogBuffer()).toBeNull();
  • Related