Skip to content

Commit f9afd43

Browse files
committed
feature/cache: Add tests for cache set feature
1 parent 422a50f commit f9afd43

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/cache.spec.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// env jest
2-
31
const cache = require('./cache');
42

53
describe('cache()', () => {
64
const parent = { id: 1 };
75
const args = { name: 'hello' };
8-
let cacheMock;
6+
let cacheMock = {};
97

10-
beforeAll(() => {
8+
beforeEach(() => {
119
cacheMock = {
1210
get: jest.fn(() => Promise.resolve()),
11+
set: jest.fn((key, result) => Promise.resolve(result)),
1312
};
1413
});
1514

@@ -27,7 +26,7 @@ describe('cache()', () => {
2726
});
2827
});
2928

30-
describe('cache retrieval', () => {
29+
describe('cache behavior', () => {
3130
const cachedValue = 'cached';
3231
const uncachedValue = 'uncached';
3332
const resolver = cache(() => uncachedValue);
@@ -37,18 +36,47 @@ describe('cache()', () => {
3736

3837
expect.assertions(1);
3938

40-
resolver(parent, args, { resolverCache: cacheMock }).then(actual => {
39+
return resolver(parent, args, {
40+
resolverCache: cacheMock,
41+
}).then(actual => {
4142
expect(actual).toEqual(cachedValue);
4243
});
4344
});
4445

46+
it('does not re-cache cached value', () => {
47+
cacheMock.get = jest.fn(() => Promise.resolve(cachedValue));
48+
49+
expect.assertions(1);
50+
51+
return resolver(parent, args, {
52+
resolverCache: cacheMock,
53+
}).then(() => {
54+
expect(cacheMock.set).toHaveBeenCalledTimes(0);
55+
});
56+
});
57+
4558
it('returns uncached value if the cache returns undefined', () => {
4659
cacheMock.get = jest.fn(() => Promise.resolve(undefined));
4760

48-
expect.assertions(1);
61+
expect.assertions(2);
4962

50-
resolver(parent, args, { resolverCache: cacheMock }).then(actual => {
63+
return resolver(parent, args, {
64+
resolverCache: cacheMock,
65+
}).then(actual => {
5166
expect(actual).toEqual(uncachedValue);
67+
expect(cacheMock.set).toHaveBeenCalledTimes(1);
68+
});
69+
});
70+
71+
it('sets uncached value in cache', () => {
72+
cacheMock.get = jest.fn(() => Promise.resolve(undefined));
73+
74+
expect.assertions(1);
75+
76+
return resolver(parent, args, {
77+
resolverCache: cacheMock,
78+
}).then(() => {
79+
expect(cacheMock.set).toHaveBeenCalledTimes(1);
5280
});
5381
});
5482
});

0 commit comments

Comments
 (0)