Skip to content

Commit 2f19669

Browse files
author
Olaf Kwant
committed
better unique function and tests
1 parent a2a634a commit 2f19669

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

lib/client_v2.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import Client from './client'
22

33
const _cache = {}
4-
function cache (key, value) {
5-
if (value === undefined) {
6-
return _cache[key]
7-
} else {
8-
_cache[key] = value
9-
return value
4+
export const tools = {
5+
unqiue: function (obj) {
6+
return Object.keys(obj).map((key) => {
7+
const value = obj[key]
8+
return (typeof value === 'object') ? tools.unqiue(value) : `${key}:${value}`
9+
}).join(' ')
10+
},
11+
12+
cache: function (key, value) {
13+
if (value === undefined) {
14+
return _cache[key]
15+
} else {
16+
_cache[key] = value
17+
return value
18+
}
1019
}
1120
}
1221

1322
export default class ClientV2 extends Client {
1423
get (stringOrArray) {
1524
if (typeof stringOrArray !== 'string' && !Array.isArray(stringOrArray)) {
16-
throw new Error('type for get not supported.')
25+
throw new Error('Type for get not supported, get expects String or Array of Strings')
1726
}
1827

1928
return Client.prototype.get.call(this, stringOrArray).then((data) => {
@@ -50,18 +59,16 @@ export default class ClientV2 extends Client {
5059

5160
request (obj) {
5261
var cached = !!obj.cachable
53-
var cacheName = Object.keys(obj).map((k) => {
54-
return obj[k]
55-
}).join(':')
5662
delete obj.cachable
63+
var cacheName = tools.unqiue(obj)
5764

5865
if (cached) {
59-
var currently = cache(cacheName)
66+
var currently = tools.cache(cacheName)
6067
if (currently !== undefined) return Promise.resolve(currently)
6168
}
6269

6370
return Client.prototype.request.call(this, obj).then((data) => {
64-
if (cached) cache(cacheName, data)
71+
if (cached) tools.cache(cacheName, data)
6572
return data
6673
})
6774
}

spec/client_v2_spec.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env mocha */
2-
import ClientV2 from '../lib/client_v2'
2+
import ClientV2, { tools } from '../lib/client_v2'
33
import sinon from 'sinon'
44

55
describe('ClientV2', () => {
@@ -8,19 +8,62 @@ describe('ClientV2', () => {
88
const appGuid = 'ABC123'
99
let subject, source
1010

11-
beforeEach(() => {
11+
before(() => {
1212
sandbox.stub(window, 'addEventListener')
1313
sandbox.stub(window, 'postMessage')
1414
source = { postMessage: sandbox.stub() }
1515
subject = new ClientV2({ origin, appGuid, source })
1616
})
1717

18-
afterEach(() => {
18+
after(() => {
1919
sandbox.restore()
2020
})
2121

22-
describe('isOriginValid', () => {
23-
it('instantiates the client when the domain is valid', () => {
22+
describe('request', () => {
23+
let cacheSpy
24+
25+
before(() => {
26+
cacheSpy = sandbox.stub(tools, 'cache')
27+
})
28+
29+
it('1', () => {
30+
subject.request({
31+
url: '/api/v2/tickets',
32+
cachable: true
33+
})
34+
assert(cacheSpy.withArgs('url:/api/v2/tickets').called)
35+
})
36+
37+
it('2', () => {
38+
subject.request({
39+
type: 'get',
40+
url: '/api/v2/tickets',
41+
cachable: true
42+
})
43+
assert(cacheSpy.withArgs('type:get url:/api/v2/tickets').called)
44+
})
45+
46+
it('3', () => {
47+
subject.request({
48+
url: '/api/v2/tickets',
49+
data: {
50+
test: true
51+
},
52+
cachable: true
53+
})
54+
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true').called)
55+
})
56+
57+
it('4', () => {
58+
subject.request({
59+
url: '/api/v2/tickets',
60+
data: {
61+
test: true
62+
},
63+
other: function () {},
64+
cachable: true
65+
})
66+
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true other:function () {}').called)
2467
})
2568
})
2669
})

0 commit comments

Comments
 (0)