score:0

localstorage.getitem should be spied before expected call:

jest.spyon(localstorage, 'getitem');

and asserted that it was called afterwards:

expect(localstorage.getitem).tobecalledwith('access_token');

score:0

put this in your test initializer files:

const localstoragemock = {
  getitem: jest.fn(),
  setitem: jest.fn(),
  clear: jest.fn()
};

global.localstorage = localstoragemock;

so now you can just use localstoragemock globally

expect(localstoragemock.getitem).tohavebeencalledwith('access_token')

the only gotcha is to make sure that your tests have jest.clearallmocks() cleanup step so the mock is reinitialized for other tests.

score:1

you can use like this:-

const result = '{ "name":"john", "age":30, "car":"bmw"}';

//set json object to storage 
localstorage.setitem('user', json.stringify(result));

//get object
const value = localstorage.getitem('user');

//remove object
localstorage.removeitem('user');

Related Query

More Query from same tag