-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6363a48
commit 7943c2d
Showing
4 changed files
with
82 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
'use strict' | ||
|
||
const cache = {} | ||
|
||
exports.getCachedItem = key => cache[key] | ||
exports.setCachedItem = (key, value) => { | ||
cache[key] = value | ||
return value | ||
exports.cache = () => { | ||
let storage = null | ||
return { | ||
store: async create => { | ||
return storage || (storage = await create()) | ||
}, | ||
read: otherwise => storage || otherwise(), | ||
evict: () => { | ||
storage = null | ||
} | ||
} | ||
} | ||
exports.deleteCachedItem = key => delete cache[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
'use strict' | ||
|
||
const { deleteCachedItem, getCachedItem, setCachedItem } = require('./cache') | ||
const { cache } = require('./cache') | ||
|
||
describe('cache', () => { | ||
const key = 'item' | ||
const value = 'value' | ||
it('should cache', () => { | ||
expect(getCachedItem(key)).toBe(undefined) | ||
it('should store, read and evict', async () => { | ||
const myCache = cache() | ||
const value = 'value' | ||
|
||
expect(setCachedItem(key, value)).toBe(value) | ||
const create = jest.fn() | ||
create.mockResolvedValue(value) | ||
await expect(myCache.store(create)).resolves.toBe(value) | ||
await expect(myCache.store(create)).resolves.toBe(value) | ||
|
||
expect(getCachedItem(key)).toBe(value) | ||
expect(create).toHaveBeenCalledTimes(1) | ||
|
||
deleteCachedItem(key) | ||
const otherwise = jest.fn() | ||
expect(myCache.read(otherwise)).toBe(value) | ||
|
||
expect(getCachedItem(key)).toBe(undefined) | ||
myCache.evict() | ||
|
||
otherwise.mockImplementation(() => { | ||
throw new Error('no value') | ||
}) | ||
expect(() => myCache.read(otherwise)).toThrow(new Error('no value')) | ||
|
||
await expect(myCache.store(create)).resolves.toBe(value) | ||
|
||
expect(create).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |