BigW Consortium Gitlab

cache_spec.js 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
import Cache from '~/lib/utils/cache';

describe('Cache', () => {
  const dummyKey = 'just some key';
  const dummyValue = 'more than a value';
  let cache;

  beforeEach(() => {
    cache = new Cache();
  });

  describe('get', () => {
    it('return cached data', () => {
      cache.internalStorage[dummyKey] = dummyValue;

      expect(cache.get(dummyKey)).toBe(dummyValue);
    });

    it('returns undefined for missing data', () => {
      expect(cache.internalStorage[dummyKey]).toBe(undefined);
      expect(cache.get(dummyKey)).toBe(undefined);
    });
  });

  describe('hasData', () => {
    it('return true for cached data', () => {
      cache.internalStorage[dummyKey] = dummyValue;

      expect(cache.hasData(dummyKey)).toBe(true);
    });

    it('returns false for missing data', () => {
      expect(cache.internalStorage[dummyKey]).toBe(undefined);
      expect(cache.hasData(dummyKey)).toBe(false);
    });
  });

  describe('remove', () => {
    it('removes data from cache', () => {
      cache.internalStorage[dummyKey] = dummyValue;

      cache.remove(dummyKey);

      expect(cache.internalStorage[dummyKey]).toBe(undefined);
    });

    it('does nothing for missing data', () => {
      expect(cache.internalStorage[dummyKey]).toBe(undefined);

      cache.remove(dummyKey);

      expect(cache.internalStorage[dummyKey]).toBe(undefined);
    });

    it('does not remove wrong data', () => {
      cache.internalStorage[dummyKey] = dummyValue;
      cache.internalStorage[dummyKey + dummyKey] = dummyValue + dummyValue;

      cache.remove(dummyKey);

      expect(cache.internalStorage[dummyKey]).toBe(undefined);
      expect(cache.internalStorage[dummyKey + dummyKey]).toBe(dummyValue + dummyValue);
    });
  });
});