pinafore/tests/unit/test-quick-lru.mjs
Nolan Lawson 16e66346d7
fix!: remove esm package, use native Node ES modules (#2064)
BREAKING CHANGE: Node v12.20+, v14.14+, or v16.0+ is required

* fix!: remove esm package, use native Node ES modules

* fix: fix some CJS imports
2021-07-04 20:19:04 -07:00

55 lines
1.4 KiB
JavaScript

/* global describe, it */
import { QuickLRU } from '../../src/routes/_thirdparty/quick-lru/quick-lru.js'
import assert from 'assert'
describe('test-quick-lru.js', () => {
it('fires evict events correctly', () => {
const cache = new QuickLRU({ maxSize: 3 })
const evictions = []
cache.on('evict', (value, key) => {
evictions.push({ key, value })
})
cache.set('a', 1)
cache.set('b', 2)
cache.set('c', 3)
cache.set('d', 4)
cache.set('e', 5)
cache.set('f', 6)
cache.set('a', 1)
cache.set('d', 4)
cache.set('g', 7)
assert.deepStrictEqual(evictions, [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'c', value: 3 },
{ key: 'e', value: 5 },
{ key: 'f', value: 6 }
])
})
it('fires evict events correctly, using get()', () => {
const cache = new QuickLRU({ maxSize: 3 })
const evictions = []
cache.on('evict', (value, key) => {
evictions.push({ key, value })
})
cache.set('a', 1)
cache.set('b', 2)
cache.set('c', 3)
cache.set('d', 4)
cache.set('e', 5)
cache.set('f', 6)
cache.set('a', 1)
cache.get('e')
cache.set('g', 7)
assert.deepStrictEqual(evictions, [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'c', value: 3 },
{ key: 'd', value: 4 },
{ key: 'f', value: 6 }
])
})
})