store.test.ts 1.23 KB
Newer Older
1
import store from '../store';
2 3 4 5 6

Object.assign(window, {
  localStorage: {
    removeItem(key) {
      delete window.localStorage[key];
7 8
    },
  },
9 10
});

11 12 13 14
describe('store', () => {
  it('should store', () => {
    store.set('key1', '123');
    expect(store.get('key1')).toBe('123');
15 16
  });

17 18
  it('get key when undefined', () => {
    expect(store.get('key2')).toBe(undefined);
19 20
  });

21 22 23
  it('check if key exixts', () => {
    store.set('key3', '123');
    expect(store.exists('key3')).toBe(true);
24 25
  });

26 27
  it('get boolean when no key', () => {
    expect(store.getBool('key4', false)).toBe(false);
28 29
  });

30 31 32
  it('get boolean', () => {
    store.set('key5', 'true');
    expect(store.getBool('key5', false)).toBe(true);
33 34
  });

35 36 37 38 39 40 41 42 43 44 45 46
  it('gets an object', () => {
    expect(store.getObject('object1')).toBeUndefined();
    expect(store.getObject('object1', [])).toEqual([]);
    store.setObject('object1', [1]);
    expect(store.getObject('object1')).toEqual([1]);
  });

  it('sets an object', () => {
    expect(store.setObject('object2', { a: 1 })).toBe(true);
    expect(store.getObject('object2')).toEqual({ a: 1 });
  });

47 48 49 50
  it('key should be deleted', () => {
    store.set('key6', '123');
    store.delete('key6');
    expect(store.exists('key6')).toBe(false);
51 52
  });
});