Skip to content

Commit 55bf931

Browse files
committed
make code more testable; add more unit tests
1 parent f88d6fe commit 55bf931

File tree

8 files changed

+109
-26
lines changed

8 files changed

+109
-26
lines changed

src/lib/cache/Cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export abstract class Cache {
1414

1515
public abstract purge();
1616

17-
public abstract clear();
17+
public abstract clear(): boolean;
1818
}

src/lib/cache/MemoryCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ export class MemoryCache extends Cache {
5959
}
6060

6161
public clear() {
62-
//
62+
return true;
6363
}
6464
}

src/lib/cache/RedisCache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class RedisCache extends Cache {
1313

1414
this.prefix = keyPrefix;
1515

16-
this.redis = new Redis(); //{ keyPrefix: keyPrefix });
16+
this.redis = new Redis({ keyPrefix: keyPrefix });
1717
}
1818

1919
public destroy() {
@@ -37,10 +37,10 @@ export class RedisCache extends Cache {
3737
}
3838

3939
public purge() {
40-
//
40+
return true;
4141
}
4242

4343
public clear() {
44-
//
44+
return true;
4545
}
4646
}

src/lib/resolver.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Cache } from './cache/Cache';
12
import { RedisCache } from './cache/RedisCache';
23
import { MemoryCache } from './cache/MemoryCache';
34
import { getResolver as getCommentsResolver } from '@/resolvers/comments';
@@ -8,12 +9,23 @@ import { getResolver as getCommentCountResolver } from '@/resolvers/Story/commen
89
import { getResolver as getUserResolver } from '@/resolvers/user';
910
import { getScalarType as getDateScalar } from '@/scalars/Date';
1011

12+
export const getCacheInstance = (driver: string): Cache => {
13+
switch (driver) {
14+
case 'redis':
15+
return new RedisCache();
16+
17+
case 'memory':
18+
default:
19+
return new MemoryCache();
20+
}
21+
};
22+
1123
export const getResolvers = (cacheDriver: string) => {
1224
const storyIds: number[] = [];
1325
const stories: Record<string, any>[] = [];
1426
const jobIds: number[] = [];
1527
const jobs: Record<string, any>[] = [];
16-
const cache = cacheDriver === 'redis' ? new RedisCache() : new MemoryCache();
28+
const cache = getCacheInstance(cacheDriver);
1729

1830
const dateScalar = getDateScalar();
1931

tests/lib/cache/MemoryCache.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,20 @@ it('purges expired entries before checking if an item exists', async () => {
5757
expect(await cache.has('one')).toBeFalsy();
5858
expect(cache.map['one']).toBeUndefined();
5959
});
60+
61+
it('prefixes all keys with a prefix', async () => {
62+
const customCache = new MemoryCache('myprefix');
63+
64+
try {
65+
customCache.put('one', 1, 10);
66+
67+
expect(await customCache.has('one')).toBeTruthy();
68+
expect(customCache.map['myprefix:one']).not.toBeUndefined();
69+
} finally {
70+
customCache.destroy();
71+
}
72+
});
73+
74+
it('clears items', () => {
75+
expect(cache.clear()).toBeTruthy();
76+
});

tests/lib/cache/RedisCache.test.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-undef */
22

33
import { RedisCache } from '@/lib/cache/RedisCache';
4+
const Redis = require('ioredis');
45

56
let cache: RedisCache;
67

@@ -37,27 +38,25 @@ it('determines if an item is in the cache', async () => {
3738
expect(await cache.has('two')).toBeFalsy();
3839
});
3940

40-
// it('purges expired items', () => {
41-
// cache.put('one', 1, 10);
42-
// cache.map['one'].expires = 0;
41+
it('prefixes all keys with a prefix', async () => {
42+
const testRedis = new Redis();
43+
const customCache = new RedisCache('myprefix');
4344

44-
// cache.purge();
45+
try {
46+
customCache.put('one', 1, 10);
4547

46-
// expect(cache.map['one']).toBeUndefined();
47-
// });
48-
49-
// it('purges expired entries before returning an item', async () => {
50-
// cache.put('one', 1, 10);
51-
// cache.map['one'].expires -= 15 * 1000;
52-
53-
// expect(await cache.get('one')).toBeNull();
54-
// expect(cache.map['one']).toBeUndefined();
55-
// });
48+
expect(await customCache.has('one')).toBeTruthy();
49+
expect(await testRedis.exists('myprefix:one')).toBeTruthy();
50+
} finally {
51+
customCache.destroy();
52+
testRedis.disconnect();
53+
}
54+
});
5655

57-
// it('purges expired entries before checking if an item exists', async () => {
58-
// cache.put('one', 1, 10);
59-
// cache.map['one'].expires -= 15 * 1000;
56+
it('purges expired items', () => {
57+
expect(cache.purge()).toBeTruthy();
58+
});
6059

61-
// expect(await cache.has('one')).toBeFalsy();
62-
// expect(cache.map['one']).toBeUndefined();
63-
// });
60+
it('clears items', () => {
61+
expect(cache.clear()).toBeTruthy();
62+
});

tests/lib/resolver.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable no-undef */
2+
3+
import { MemoryCache } from '@/lib/cache/MemoryCache';
4+
import { RedisCache } from '@/lib/cache/RedisCache';
5+
import { getCacheInstance, getResolvers } from '@/lib/resolver';
6+
7+
it('returns a MemoryCache instance for the "memory" driver', () => {
8+
const cache = getCacheInstance('memory');
9+
10+
expect(cache).toBeInstanceOf(MemoryCache);
11+
});
12+
13+
it('returns a MemoryCache instance an unknown driver', () => {
14+
const cache = getCacheInstance('unknown');
15+
16+
expect(cache).toBeInstanceOf(MemoryCache);
17+
});
18+
19+
it('returns a RedisCache instance for the "redis" driver', () => {
20+
const cache = getCacheInstance('redis');
21+
22+
expect(cache).toBeInstanceOf(RedisCache);
23+
24+
cache.destroy();
25+
});
26+
27+
it('returns a resolver object', () => {
28+
process.env.NODE_ENV = 'testing';
29+
30+
const resolvers = getResolvers('memory');
31+
32+
expect(resolvers['Comment']).not.toBeUndefined();
33+
expect(resolvers['Date']).not.toBeUndefined();
34+
expect(resolvers['Job']).not.toBeUndefined();
35+
expect(resolvers['Query']).not.toBeUndefined();
36+
expect(resolvers['Story']).not.toBeUndefined();
37+
});

tests/lib/server.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable no-undef */
2+
3+
import { App } from '@tinyhttp/app';
4+
import { MemoryCache } from '@/lib/cache/MemoryCache';
5+
import { getApolloServer, getServer } from '@/lib/server';
6+
import { ApolloServer } from 'apollo-server-express';
7+
8+
it('returns an instance of Apollo Server', () => {
9+
const server = getApolloServer(true, new MemoryCache());
10+
11+
expect(server).toBeInstanceOf(ApolloServer);
12+
});
13+
14+
it('returns an instance of tinyhttp/app', () => {
15+
const server = getServer(new App(), true, new MemoryCache());
16+
17+
expect(server).toBeInstanceOf(App);
18+
});

0 commit comments

Comments
 (0)