2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2023-02-22 17:16:38 +03:00
|
|
|
const InMemoryCache = require('@tryghost/adapter-cache-memory-ttl');
|
|
|
|
|
2023-02-22 15:47:32 +03:00
|
|
|
const EventAwareCacheWrapper = require('../index');
|
2023-02-22 17:16:38 +03:00
|
|
|
const {EventEmitter} = require('stream');
|
|
|
|
|
2023-02-22 15:47:32 +03:00
|
|
|
describe('EventAwareCacheWrapper', function () {
|
|
|
|
it('Can initialize', function () {
|
2023-02-22 17:16:38 +03:00
|
|
|
const cache = new InMemoryCache();
|
|
|
|
const wrappedCache = new EventAwareCacheWrapper({
|
|
|
|
cache
|
|
|
|
});
|
|
|
|
assert.ok(wrappedCache);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', function () {
|
|
|
|
it('calls a wrapped cache with extra key', async function () {
|
|
|
|
const cache = new InMemoryCache();
|
|
|
|
const wrapper = new EventAwareCacheWrapper({
|
2023-08-09 09:33:15 +03:00
|
|
|
cache: cache
|
2023-02-22 17:16:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.set('a', 'b');
|
|
|
|
assert.equal(await wrapper.get('a'), 'b');
|
2023-08-09 09:33:15 +03:00
|
|
|
assert.equal(await cache.get('a'), 'b');
|
2023-02-22 17:16:38 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('listens to reset events', function () {
|
|
|
|
it('resets the cache when reset event is triggered', async function () {
|
|
|
|
const cache = new InMemoryCache();
|
2023-08-09 09:33:15 +03:00
|
|
|
|
2023-02-22 17:16:38 +03:00
|
|
|
const eventRegistry = new EventEmitter();
|
|
|
|
const wrapper = new EventAwareCacheWrapper({
|
|
|
|
cache: cache,
|
|
|
|
resetEvents: ['site.changed'],
|
|
|
|
eventRegistry: eventRegistry
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.set('a', 'b');
|
|
|
|
assert.equal(await wrapper.get('a'), 'b');
|
|
|
|
|
|
|
|
eventRegistry.emit('site.changed');
|
|
|
|
|
|
|
|
assert.equal(await wrapper.get('a'), undefined);
|
|
|
|
});
|
2023-02-22 15:47:32 +03:00
|
|
|
});
|
|
|
|
});
|