Skip to content

Commit

Permalink
feat: add redisClient
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Jan 26, 2024
1 parent a5c4e8d commit 4582bad
Show file tree
Hide file tree
Showing 2 changed files with 416 additions and 0 deletions.
56 changes: 56 additions & 0 deletions __test__/unittest/redisClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import RedisMock from 'ioredis-mock';

describe('Test redis client', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules(); // Most important - it clears the cache
jest.clearAllMocks();
jest.mock('ioredis', () => RedisMock);
process.env = { ...OLD_ENV }; // Make a copy
});

afterAll(() => {
process.env = OLD_ENV; // Restore old environment
});
test('Client reports not enabled when REDIS_HOST is not set', () => {
const { redisClient } = require('../../src/redis-client');
expect(redisClient.clientEnabled).toEqual(false);
});

test('will receive process.env variables', () => {
// Set the variables
process.env.REDIS_HOST = 'localhost';
process.env.REDIS_PORT = '3367';
const { redisClient } = require('../../src/redis-client');
expect(redisClient).not.toEqual({});
});

test('Test if record is correctly stored', async () => {
process.env.REDIS_HOST = 'localhost';
process.env.REDIS_PORT = '3367';
const { redisClient } = require('../../src/redis-client');
await redisClient.client.setTimeout('record1', 'hello');
const res = await redisClient.client.getTimeout('record1');
expect(res).toEqual('hello');
});

test('Test if record is correctly stored', async () => {
process.env.REDIS_HOST = 'localhost';
process.env.REDIS_PORT = '3367';
const { redisClient } = require('../../src/redis-client');
await redisClient.client.setTimeout('record1', 'hello');
const res = await redisClient.client.getTimeout('record1');
expect(res).toEqual('hello');
});

test('Test key should be removed after ttl', async () => {
process.env.REDIS_HOST = 'localhost';
process.env.REDIS_PORT = '3367';
const { redisClient } = require('../../src/redis-client');
await redisClient.client.setTimeout('record1', 'hello', 'EX', 2);
await new Promise((r) => setTimeout(r, 3000));
const res = await redisClient.client.getTimeout('record1');
expect(res).not.toBeNull;
});
});
Loading

0 comments on commit 4582bad

Please sign in to comment.