Skip to content

Commit

Permalink
feat: return validation result in obj get
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Jan 26, 2021
1 parent 65ecca6 commit 8bc23f0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ If a `loader` is present, it will be called with the missing keys and those keys
get('indexKeyForValue', undefined, undefined, 'obj')

// returns the cached data alongside the meta
// also includes the result of the validation according to expire parameter
{
data: 'foo',
meta: {
date: '2021-01-15T18:21:00.152Z',
}
},
valid: true,
}
```

Expand Down
7 changes: 4 additions & 3 deletions src/methods/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addListener, cachedObj, debugLog, dispatch, expire, reactCache, setProp

type valueTypes = {
data: cachedObj['data'],
obj: cachedObj,
obj: cachedObj & {valid: boolean},
}

export type getValue<
Expand Down Expand Up @@ -64,11 +64,12 @@ export function get<
const missing: string[] = []

keys.forEach(key => {
if(!cache[key].promise && !verifyEntry(cache[key], expire)) {
const valid = verifyEntry(cache[key], expire)
if(!cache[key].promise && !valid) {
missing.push(key)
}
if (returnType === 'obj') {
values[key] = cache[key].obj as getValue<T>
values[key] = (cache[key].obj ? { ...cache[key].obj, valid } : undefined) as getValue<T>
} else {
values[key] = cache[key].obj?.data as getValue<T>
}
Expand Down
38 changes: 38 additions & 0 deletions test/methods/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,44 @@ it('call loader for missing entries', async () => {
expect(loader).toBeCalledWith(['faa'])
})

it('verify entries per callback', async () => {
const { api } = await setupApi({cacheValues: { foo: 'FOO', bar: 'BAR'}})

const expire = jest.fn(o => o.data === 'FOO')

const value = api.get(['foo', 'bar'], undefined, expire, 'obj')

expect(expire).toBeCalledTimes(2)
expect(expire).toHaveBeenNthCalledWith(1, expect.objectContaining({data: 'FOO'}))
expect(expire).toHaveBeenNthCalledWith(2, expect.objectContaining({data: 'BAR'}))
expect(value).toEqual({
foo: expect.objectContaining({data: 'FOO', valid: false}),
bar: expect.objectContaining({data: 'BAR', valid: true}),
})
})

it('verify entries per age', async () => {
const { api } = await setupApi({
cacheObjects: {
faa: { data: 'a', meta: { date: new Date() } },
fee: { data: 'b', meta: { date: (new Date()).toString() } },
fii: { data: 'c', meta: { date: undefined } },
foo: { data: 'd', meta: { date: new Date('2001-02-03T04:05:06Z') } },
fuu: { data: 'e', meta: { date: '2001-02-03T04:05:06Z' } },
},
})

const value = api.get(['faa', 'fee', 'fii', 'foo', 'fuu'], undefined, 10000, 'obj')

expect(value).toEqual({
'faa': expect.objectContaining({data: 'a', valid: true}),
'fee': expect.objectContaining({data: 'b', valid: true}),
'fii': expect.objectContaining({data: 'c', valid: true}),
'foo': expect.objectContaining({data: 'd', valid: false}),
'fuu': expect.objectContaining({data: 'e', valid: false}),
})
})

it('call loader for expired (per callback) entries', async () => {
const { cache, rerender, api } = await setupApi({cacheValues: { foo: 'bar' }, idbValues: { fuu: 'baz' } })
const loader = jest.fn(() => {
Expand Down

0 comments on commit 8bc23f0

Please sign in to comment.