title: How to mock localStorage.setIem and localStorage.removeItem in JEST
body: <p>when i try to mock the localStorage of a react component in jest as below,</p>
  
  <pre><code>spyOn(window.localStorage,'removeItem');
  window.localStorage.removeItem("key1");
  window.localStorage.removeItem("key2");
  expect(window.localStorage.removeItem).toHaveBeenCalledWith("key1");
  expect(window.localStorage.removeItem).toHaveBeenCalledWith("key2");
  </code></pre>
  
  <p>and used the below code for localStorage mocking</p>
  
  <pre><code>let localStorageMock = (function() {
    var storage = {};
  
    return {
      setItem: function(key, value) {
        storage[key] = value || '';
      },
      getItem: function(key) {
        return storage[key] || null;
      },
      removeItem: function(key) {
        delete storage[key];
      },
      get length() {
        return Object.keys(storage).length;
      },
      key: function(i) {
        var keys = Object.keys(storage);
        return keys[i] || null;
      }
    };
  })();
  Object.defineProperty(window, 'localStorage', { value: localStorageMock });
  </code></pre>