title: How can we unit test the sessionstorage in Angular?
body: <p>I am new to unit testing in Angular, and I am trying to test the sessionstorage and localstorage. </p>
  
  <p>My code is:</p>
  
  <pre><code>subscribe(data) {
          sessionStorage.setItem('myProductDetail', JSON.stringify(data));
      }
  
  subscribe(data) {
          LocalStorage.setItem('dummyname', data[0].name);
      }
  </code></pre>
  
  <p>Is it a good practice to do unit testing for session, local storage and private classes?</p>
  
  <p>I am trying to follow the example code below to mock my localstorage and sessionstorage.</p>
  
  <pre><code>beforeEach(function () {
    var store = {};
  
    spyOn(localStorage, 'getItem').andCallFake(function (key) {
      return store[key];
    });
    spyOn(sessionStorage, 'setItem').andCallFake(function (key, value) {
      return store[key] = value + '';
    });
    spyOn(localStorage, 'clear').andCallFake(function () {
        store = {};
    });
  });
  </code></pre>
  
  <p>Any guidance is greatly appreciated. </p>