2021-10-30 07:21:27 -06:00
|
|
|
import {CacheEntryListener, CacheListener} from '../src/cache-base'
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
describe('caching report', () => {
|
|
|
|
describe('reports not fully restored', () => {
|
|
|
|
it('with one requested entry report', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
report.entry('foo').markRequested('1', ['2'])
|
|
|
|
report.entry('bar').markRequested('3').markRestored('4')
|
2021-10-30 07:15:20 -06:00
|
|
|
expect(report.fullyRestored).toBe(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('reports fully restored', () => {
|
|
|
|
it('when empty', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
2021-10-30 07:15:20 -06:00
|
|
|
expect(report.fullyRestored).toBe(true)
|
|
|
|
})
|
|
|
|
it('with empty entry reports', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
report.entry('foo')
|
|
|
|
report.entry('bar')
|
2021-10-30 07:15:20 -06:00
|
|
|
expect(report.fullyRestored).toBe(true)
|
|
|
|
})
|
|
|
|
it('with restored entry report', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
report.entry('bar').markRequested('3').markRestored('4')
|
2021-10-30 07:15:20 -06:00
|
|
|
expect(report.fullyRestored).toBe(true)
|
|
|
|
})
|
|
|
|
it('with multiple restored entry reportss', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
report.entry('foo').markRestored('4')
|
|
|
|
report.entry('bar').markRequested('3').markRestored('4')
|
2021-10-30 07:15:20 -06:00
|
|
|
expect(report.fullyRestored).toBe(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('can be stringified and rehydrated', () => {
|
|
|
|
it('when empty', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
const stringRep = report.stringify()
|
2021-10-30 07:21:27 -06:00
|
|
|
const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
expect(reportClone.cacheEntries).toEqual([])
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
// Can call methods on rehydrated
|
2021-10-30 07:21:27 -06:00
|
|
|
expect(reportClone.entry('foo')).toBeInstanceOf(CacheEntryListener)
|
2021-10-30 07:15:20 -06:00
|
|
|
})
|
|
|
|
it('with entry reports', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
report.entry('foo')
|
|
|
|
report.entry('bar')
|
|
|
|
report.entry('baz')
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
const stringRep = report.stringify()
|
2021-10-30 07:21:27 -06:00
|
|
|
const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
expect(reportClone.cacheEntries.length).toBe(3)
|
|
|
|
expect(reportClone.cacheEntries[0].entryName).toBe('foo')
|
|
|
|
expect(reportClone.cacheEntries[1].entryName).toBe('bar')
|
|
|
|
expect(reportClone.cacheEntries[2].entryName).toBe('baz')
|
2021-10-30 07:15:20 -06:00
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
expect(reportClone.entry('foo')).toBe(reportClone.cacheEntries[0])
|
2021-10-30 07:15:20 -06:00
|
|
|
})
|
|
|
|
it('with rehydrated entry report', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
const entryReport = report.entry('foo')
|
2021-10-30 07:15:20 -06:00
|
|
|
entryReport.markRequested('1', ['2', '3'])
|
|
|
|
entryReport.markSaved('4')
|
|
|
|
|
|
|
|
const stringRep = report.stringify()
|
2021-10-30 07:21:27 -06:00
|
|
|
const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
|
|
|
|
const entryClone = reportClone.entry('foo')
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
expect(entryClone.requestedKey).toBe('1')
|
|
|
|
expect(entryClone.requestedRestoreKeys).toEqual(['2', '3'])
|
|
|
|
expect(entryClone.savedKey).toBe('4')
|
|
|
|
})
|
|
|
|
it('with live entry report', async () => {
|
2021-10-30 07:21:27 -06:00
|
|
|
const report = new CacheListener()
|
|
|
|
const entryReport = report.entry('foo')
|
2021-10-30 07:15:20 -06:00
|
|
|
entryReport.markRequested('1', ['2', '3'])
|
|
|
|
|
|
|
|
const stringRep = report.stringify()
|
2021-10-30 07:21:27 -06:00
|
|
|
const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
|
|
|
|
const entryClone = reportClone.entry('foo')
|
2021-10-30 07:15:20 -06:00
|
|
|
|
|
|
|
// Check type and call method on rehydrated entry report
|
2021-10-30 07:21:27 -06:00
|
|
|
expect(entryClone).toBeInstanceOf(CacheEntryListener)
|
2021-10-30 07:15:20 -06:00
|
|
|
entryClone.markSaved('4')
|
|
|
|
|
|
|
|
expect(entryClone.requestedKey).toBe('1')
|
|
|
|
expect(entryClone.requestedRestoreKeys).toEqual(['2', '3'])
|
|
|
|
expect(entryClone.savedKey).toBe('4')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|