Skip to content

Commit

Permalink
feat(cache): caching function is now a separate configuration
Browse files Browse the repository at this point in the history
BREAKING CHANGE: caching function should be a function not related to the store
  • Loading branch information
stephanebachelier committed Jan 3, 2016
1 parent cd02e97 commit 267a9d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export default function cache (config = {}) {
}

const store = config.store;
const cache = config.cache;

if (!store.cache || typeof store.cache !== 'function') {
throw new Error('Store does not have a cache function.');
if (!config.cache || typeof config.cache !== 'function') {
throw new Error('Cache middleware need a cache function.');
}

return (req, next, service) => {
Expand All @@ -19,7 +20,7 @@ export default function cache (config = {}) {
}

const promise = next()
promise.then(store.cache)
promise.then(cache)

return promise;
}
Expand Down
37 changes: 22 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,44 @@ import superapiCache from '../lib/index.js'
import test from 'blue-tape'
import { spy } from 'sinon'

test('middleware configuration', t => {
test('middleware configuration need a store', t => {
t.throws(() => {
superapiCache()
}, /Cache middleware need to be provided a store/, 'should throw if no store property found')

t.end()
})

test('middleware need a cache function ', t => {
t.throws(() => {
superapiCache({
store: {
foo: 'bar'
}
store: {}
})
}, /Store does not have a cache function/, 'should throw if invalid store provided')
}, /Cache middleware need a cache function/, 'should throw if no caching function provided')

t.end()
})


test('middleware need a store and a cache function', t => {
t.doesNotThrow(() => {
superapiCache({
store: {
cache: function () {}
}
store: {},
cache: function () {}
})
}, 'should not throw if valid store provided')
}, 'should not throw with a valid configuration provided')

t.end()
})
t.end()
})


test('should call cache function by default', t => {
const cacheFn = spy()
const handler = superapiCache({
store: {
cache: cacheFn
}
store: new MemoryStore(),
cache: cacheFn
})

const next = () => {
Expand All @@ -54,9 +62,8 @@ test('should call cache function by default', t => {
test('disable cache', t => {
const cacheFn = spy(function () {})
const handler = superapiCache({
store: {
cache: cacheFn
}
store: {},
cache: cacheFn
})

const next = () => {
Expand Down

0 comments on commit 267a9d4

Please sign in to comment.