import * as LocalStorageLib from 'src/lib/localStorage';

describe('src/lib/localStorage', () => {
  beforeEach(() => {
    window.localStorage.setItem('test', '{"spam":true}');
  });

  afterEach(() => {
    window.localStorage.removeItem('test');
  });

  describe('getItem', () => {
    it('returns the parsed item', () => {
      // Given
      const result = LocalStorageLib.getItem('test');

      // Then
      expect(result).toEqual({'spam': true});
    });

    it('returns default if the item is not present', () => {
      // Given
      const result = LocalStorageLib.getItem('test2', 'default');

      // Then
      expect(result).toEqual('default');
    });

    it('defaults the default to null', () => {
      // Given
      const result = LocalStorageLib.getItem('test3');

      // Then
      expect(result).toBe(null);
    });
  });

  describe('setItem', () => {
    it('stores the serialized item', () => {
      // Given
      LocalStorageLib.setItem('test', {spam: false});

      // Then
      const storedItem = window.localStorage.getItem('test');
      expect(storedItem).toEqual('{"spam":false}');
    });
  });
});
