@v4fire/core
Version:
V4Fire core library
88 lines (87 loc) • 2.88 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _restricted = _interopRequireDefault(require("../../../core/cache/restricted"));
describe('core/cache/restricted', () => {
it('crud', () => {
const cache = new _restricted.default();
expect(cache.has('foo')).toBe(false);
expect(cache.set('foo', 1)).toBe(1);
expect(cache.get('foo')).toBe(1);
expect(cache.has('foo')).toBe(true);
expect(cache.size).toBe(1);
expect(cache.remove('foo')).toBe(1);
expect(cache.has('foo')).toBe(false);
});
it('specifying capacity', () => {
const cache = new _restricted.default(3);
cache.set('foo', 1);
cache.set('bar', 2);
cache.set('bla', 3);
expect(cache.has('foo')).toBe(true);
expect(cache.has('bar')).toBe(true);
expect(cache.has('bla')).toBe(true);
cache.set('baz', 4);
expect(cache.size).toBe(3);
expect(cache.has('foo')).toBe(false);
expect(cache.has('baz')).toBe(true);
});
it('default iterator', () => {
const cache = new _restricted.default(2);
cache.set('1', 1);
cache.set('2', 2);
cache.set('3', 3);
expect(cache[Symbol.iterator]().next()).toEqual({
value: '2',
done: false
});
expect([...cache]).toEqual(['2', '3']);
});
it('`keys`', () => {
const cache = new _restricted.default(2);
cache.set('1', 1);
cache.set('2', 2);
cache.set('3', 3);
expect([...cache.keys()]).toEqual(['2', '3']);
});
it('`values`', () => {
const cache = new _restricted.default(2);
cache.set('1', 1);
cache.set('2', 2);
cache.set('3', 3);
expect([...cache.values()]).toEqual([2, 3]);
});
it('`entries`', () => {
const cache = new _restricted.default(2);
cache.set('1', 1);
cache.set('2', 2);
cache.set('3', 3);
expect([...cache.entries()]).toEqual([['2', 2], ['3', 3]]);
});
it('`clear`', () => {
const cache = new _restricted.default(2);
cache.set('foo', 1);
cache.set('bar', 2);
expect(cache.has('foo')).toBe(true);
expect(cache.has('bar')).toBe(true);
expect(cache.clear()).toEqual(new Map([['foo', 1], ['bar', 2]]));
});
it('`clear` with a filter', () => {
const cache = new _restricted.default();
cache.set('foo', 1);
cache.set('bar', 2);
expect(cache.has('foo')).toBe(true);
expect(cache.has('bar')).toBe(true);
expect(cache.clear(el => el > 1)).toEqual(new Map([['bar', 2]]));
});
it("modifying cache' capacity", () => {
const cache = new _restricted.default(1);
cache.set('foo', 1);
cache.setCapacity(2);
cache.set('bar', 2);
expect(cache.has('foo')).toBe(true);
expect(cache.has('bar')).toBe(true);
expect(cache.setCapacity(0)).toEqual(new Map([['foo', 1], ['bar', 2]]));
expect(cache.has('foo')).toBe(false);
expect(cache.has('bar')).toBe(false);
});
});