diff --git a/source/connector-mobilizon/front/graphql-wrapper.js b/source/connector-mobilizon/front/graphql-wrapper.js index b2f291f..cbe9684 100644 --- a/source/connector-mobilizon/front/graphql-wrapper.js +++ b/source/connector-mobilizon/front/graphql-wrapper.js @@ -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) return Promise.resolve(dataInCache) return request(url, query, { limit }) .then((data) => { - SessionCache.add({ url, query, variables: { limit }}, data) + SessionCache.add(sessionStorage, { url, query, variables: { limit }}, data) return Promise.resolve(data) }) } @@ -45,12 +45,12 @@ export function getUpcomingEventsByGroupName({ url, limit, groupName }) { } ` 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) return Promise.resolve(dataInCache) return request(url, query, { afterDatetime, groupName, limit }) .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) }) } diff --git a/source/connector-mobilizon/front/html-creator-test.js b/source/connector-mobilizon/front/html-creator-test.js index 51ad679..d92bbdc 100644 --- a/source/connector-mobilizon/front/html-creator-test.js +++ b/source/connector-mobilizon/front/html-creator-test.js @@ -7,7 +7,7 @@ test.beforeEach(() => { global.document = new JSDOM().window.document }) -test('createAnchorElement() usual parameters', t => { +test('#createAnchorElement usual parameters', t => { const a = HtmlCreator.createAnchorElement({ text: 'a', url: 'b' }) t.is(a.tagName, 'A') t.is(a.innerHTML, 'a') diff --git a/source/connector-mobilizon/front/object-hash-wrapper-test.js b/source/connector-mobilizon/front/object-hash-wrapper-test.js index b7ac52d..554a9b6 100644 --- a/source/connector-mobilizon/front/object-hash-wrapper-test.js +++ b/source/connector-mobilizon/front/object-hash-wrapper-test.js @@ -1,5 +1,5 @@ import test from 'ava' -import { hash } from './object-hash-wrapper' +import hash from './object-hash-wrapper' test('#hash object', t => { t.is(hash({foo: 'bar'}), 'a75c05bdca7d704bdfcd761913e5a4e4636e956b') diff --git a/source/connector-mobilizon/front/session-cache-test.js b/source/connector-mobilizon/front/session-cache-test.js new file mode 100644 index 0000000..eab90b2 --- /dev/null +++ b/source/connector-mobilizon/front/session-cache-test.js @@ -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) +}) diff --git a/source/connector-mobilizon/front/session-cache.js b/source/connector-mobilizon/front/session-cache.js index 78d7383..7958090 100644 --- a/source/connector-mobilizon/front/session-cache.js +++ b/source/connector-mobilizon/front/session-cache.js @@ -4,20 +4,20 @@ const MAX_AGE_IN_MS = 120000 export class SessionCache { - static add(parameters, data) { + static add(storage, parameters, data) { const key = hash(parameters) const timestamp = Date.now() const value = { data, 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 value = JSON.parse(sessionStorage.getItem(key)) - if (value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS) + const value = JSON.parse(storage.getItem(key)) + if (value && value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS) return value.data return null }