Skip to content

Commit f60aec6

Browse files
committed
enable test case coverage
1 parent 66cdded commit f60aec6

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

src/index.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ export class RedisCache implements ICache {
4545
return this.redisClient.disconnect();
4646
}
4747

48-
48+
/**
49+
* It sets the tags for the cache.
50+
* @param {string[]} keys - A list of keys to be used as tags.
51+
* @returns RedisCache.
52+
*/
4953
public tags(keys: string[]): RedisCache {
5054
this._tags = keys;
5155
return this;
5256
}
5357

54-
58+
public enable(enable: boolean): RedisCache {
59+
this._enable = enable;
60+
return this;
61+
}
62+
5563
/**
5664
* If the key is not already in the cache, set it to the value and set the expiration time
5765
* @param {string} key - The key to set.
@@ -60,7 +68,7 @@ export class RedisCache implements ICache {
6068
* @returns The return value is a boolean indicating whether the operation was successful.
6169
*/
6270
async set(key: string, value: any, ttl: number = this._ttl): Promise<boolean> {
63-
if (! this._enable) return false;
71+
if (!this._enable) return false;
6472

6573
try {
6674
key = this._setKeyPrefix(key);
@@ -69,7 +77,7 @@ export class RedisCache implements ICache {
6977
NX: true,
7078
});
7179

72-
if(result !== 'OK') return false
80+
if (result !== 'OK') return false;
7381

7482
if (this._tags.length) {
7583
await this._addTags(key);
@@ -87,8 +95,8 @@ export class RedisCache implements ICache {
8795
* @param {string} key - The key to get the value for.
8896
* @returns A promise.
8997
*/
90-
async get(key: string) {
91-
if (! this._enable) return null;
98+
async get(key: string): Promise<any> {
99+
if (!this._enable) return null;
92100

93101
try {
94102
key = this._setKeyPrefix(key);
@@ -98,7 +106,6 @@ export class RedisCache implements ICache {
98106
}
99107
}
100108

101-
102109
/**
103110
* Set the value of the key if it doesn't exist, and keep it forever.
104111
* @param {string} key - The key to set.
@@ -109,7 +116,6 @@ export class RedisCache implements ICache {
109116
return this.set(key, value, 0);
110117
}
111118

112-
113119
/**
114120
* If the key is not in the cache, call the callback and store the result in the cache
115121
* @param {string} key - The key to store the value under.
@@ -140,7 +146,7 @@ export class RedisCache implements ICache {
140146
* @returns A boolean value.
141147
*/
142148
async has(key: string): Promise<boolean> {
143-
if (! this._enable) return false;
149+
if (!this._enable) return false;
144150

145151
try {
146152
return Boolean(await this.redisClient.exists(this._setKeyPrefix(key)));
@@ -149,14 +155,13 @@ export class RedisCache implements ICache {
149155
}
150156
}
151157

152-
153158
/**
154159
* It deletes the key from the cache and removes the key from the tags.
155160
* @param {string} key - The key to be deleted.
156161
* @returns The result of the `del` command.
157162
*/
158163
async destroy(key: string): Promise<boolean> {
159-
if (! this._enable) return false;
164+
if (!this._enable) return false;
160165

161166
key = this._setKeyPrefix(key);
162167
const destroyPromises = [this.redisClient.del(key)];
@@ -179,6 +184,8 @@ export class RedisCache implements ICache {
179184
* @returns The return value is a boolean indicating whether the flush operation was successful.
180185
*/
181186
async flush(): Promise<boolean> {
187+
if (!this._enable) return false;
188+
182189
if (this._tags.length) {
183190
const keys = await this.redisClient.sUnion(this._tags);
184191
if (!keys.length) return false;
@@ -239,7 +246,6 @@ export class RedisCache implements ICache {
239246
this._tags = [];
240247
}
241248

242-
243249
/**
244250
* Set the key prefix to the value of the _prefix property
245251
* @param {string} key - The key to set.
@@ -248,4 +254,4 @@ export class RedisCache implements ICache {
248254
private _setKeyPrefix(key: string): string {
249255
return `${this._prefix}:${key}`;
250256
}
251-
}
257+
}

tests/enable.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { RedisCache } from '../src/index';
2+
3+
const cacheManager = new RedisCache();
4+
5+
beforeEach(async () => {
6+
await cacheManager.flush();
7+
});
8+
9+
beforeAll(async () => {
10+
await cacheManager.connect({
11+
url: 'redis://redis:6379',
12+
prefix: 'dorik',
13+
});
14+
});
15+
16+
test('Redis SET (string) if enable true', async () => {
17+
expect(await cacheManager.enable(true).set('key', 'value')).toBeTruthy();
18+
});
19+
20+
test('Redis DESTROY if enable false', async () => {
21+
await cacheManager.enable(false).set('key', 'value');
22+
expect(await cacheManager.enable(false).destroy('key')).toBeFalsy();
23+
});
24+
25+
test('Redis DESTROY if not exists', async () => {
26+
expect(await cacheManager.enable(false).destroy('key')).toBeFalsy();
27+
});
28+
29+
afterAll(async () => {
30+
await cacheManager.disconnect();
31+
});

tests/get.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ test('Redis GET if not exists', async () => {
4040

4141
afterAll(async () => {
4242
await cacheManager.disconnect();
43-
});
43+
});

0 commit comments

Comments
 (0)