add tests for session-cache, fix a test
This commit is contained in:
parent
12e6adacbf
commit
c9b04cb2f8
|
@ -17,12 +17,12 @@ export function getUpcomingEvents({ url, limit }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
const dataInCache = SessionCache.get({ url, query, variables: { limit }})
|
const dataInCache = SessionCache.get(sessionStorage, { url, query, variables: { limit }})
|
||||||
if (dataInCache !== null)
|
if (dataInCache !== null)
|
||||||
return Promise.resolve(dataInCache)
|
return Promise.resolve(dataInCache)
|
||||||
return request(url, query, { limit })
|
return request(url, query, { limit })
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
SessionCache.add({ url, query, variables: { limit }}, data)
|
SessionCache.add(sessionStorage, { url, query, variables: { limit }}, data)
|
||||||
return Promise.resolve(data)
|
return Promise.resolve(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -45,12 +45,12 @@ export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
|
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
|
||||||
const dataInCache = SessionCache.get({ url, query, variables: { afterDatetime, groupName, limit }})
|
const dataInCache = SessionCache.get(sessionStorage, { url, query, variables: { afterDatetime, groupName, limit }})
|
||||||
if (dataInCache !== null)
|
if (dataInCache !== null)
|
||||||
return Promise.resolve(dataInCache)
|
return Promise.resolve(dataInCache)
|
||||||
return request(url, query, { afterDatetime, groupName, limit })
|
return request(url, query, { afterDatetime, groupName, limit })
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
SessionCache.add({ url, query, variables: { afterDatetime, groupName, limit }}, data)
|
SessionCache.add(sessionStorage, { url, query, variables: { afterDatetime, groupName, limit }}, data)
|
||||||
return Promise.resolve(data)
|
return Promise.resolve(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ test.beforeEach(() => {
|
||||||
global.document = new JSDOM().window.document
|
global.document = new JSDOM().window.document
|
||||||
})
|
})
|
||||||
|
|
||||||
test('createAnchorElement() usual parameters', t => {
|
test('#createAnchorElement usual parameters', t => {
|
||||||
const a = HtmlCreator.createAnchorElement({ text: 'a', url: 'b' })
|
const a = HtmlCreator.createAnchorElement({ text: 'a', url: 'b' })
|
||||||
t.is(a.tagName, 'A')
|
t.is(a.tagName, 'A')
|
||||||
t.is(a.innerHTML, 'a')
|
t.is(a.innerHTML, 'a')
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
import { hash } from './object-hash-wrapper'
|
import hash from './object-hash-wrapper'
|
||||||
|
|
||||||
test('#hash object', t => {
|
test('#hash object', t => {
|
||||||
t.is(hash({foo: 'bar'}), 'a75c05bdca7d704bdfcd761913e5a4e4636e956b')
|
t.is(hash({foo: 'bar'}), 'a75c05bdca7d704bdfcd761913e5a4e4636e956b')
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
import test from 'ava'
|
||||||
|
import { SessionCache } from './session-cache'
|
||||||
|
|
||||||
|
const fakeStorage = {
|
||||||
|
|
||||||
|
elements: {},
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.elements = {}
|
||||||
|
},
|
||||||
|
|
||||||
|
getItem(key) {
|
||||||
|
const value = this.elements[key]
|
||||||
|
if (value === undefined) return null
|
||||||
|
return value
|
||||||
|
},
|
||||||
|
|
||||||
|
setItem(key, value) {
|
||||||
|
this.elements[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test.afterEach(() => {
|
||||||
|
fakeStorage.clear()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('#add & #get', t => {
|
||||||
|
SessionCache.add(fakeStorage, { a: 'b' }, { c: 'd' })
|
||||||
|
t.deepEqual(SessionCache.get(fakeStorage, { a: 'b' }), { c: 'd' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('#get no entry', t => {
|
||||||
|
t.is(SessionCache.get(fakeStorage, { a: 'bb' }), null)
|
||||||
|
})
|
|
@ -4,20 +4,20 @@ const MAX_AGE_IN_MS = 120000
|
||||||
|
|
||||||
export class SessionCache {
|
export class SessionCache {
|
||||||
|
|
||||||
static add(parameters, data) {
|
static add(storage, parameters, data) {
|
||||||
const key = hash(parameters)
|
const key = hash(parameters)
|
||||||
const timestamp = Date.now()
|
const timestamp = Date.now()
|
||||||
const value = {
|
const value = {
|
||||||
data,
|
data,
|
||||||
timestamp,
|
timestamp,
|
||||||
}
|
}
|
||||||
sessionStorage.setItem(key, JSON.stringify(value))
|
storage.setItem(key, JSON.stringify(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
static get(parameters) {
|
static get(storage, parameters) {
|
||||||
const key = hash(parameters)
|
const key = hash(parameters)
|
||||||
const value = JSON.parse(sessionStorage.getItem(key))
|
const value = JSON.parse(storage.getItem(key))
|
||||||
if (value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS)
|
if (value && value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS)
|
||||||
return value.data
|
return value.data
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue