Files
canina/backend/src/redis/redis.service.spec.ts
T

57 lines
1.5 KiB
TypeScript

import { Test, TestingModule } from '@nestjs/testing';
import { RedisService } from './redis.service';
jest.mock('ioredis', () => {
return jest.fn().mockImplementation(() => {
return {
set: jest.fn(),
get: jest.fn().mockResolvedValue('mock-value'),
del: jest.fn(),
disconnect: jest.fn(),
};
});
});
describe('RedisService', () => {
let service: RedisService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RedisService],
}).compile();
service = module.get<RedisService>(RedisService);
service.onModuleInit();
});
afterEach(() => {
service.onModuleDestroy();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should call set with or without ttl', async () => {
const setSpy = jest.spyOn((service as any).client, 'set');
await service.set('key', 'val');
expect(setSpy).toHaveBeenCalledWith('key', 'val');
await service.set('key', 'val', 60);
expect(setSpy).toHaveBeenCalledWith('key', 'val', 'EX', 60);
});
it('should call get', async () => {
const getSpy = jest.spyOn((service as any).client, 'get');
const result = await service.get('key');
expect(getSpy).toHaveBeenCalledWith('key');
expect(result).toBe('mock-value');
});
it('should call del', async () => {
const delSpy = jest.spyOn((service as any).client, 'del');
await service.del('key');
expect(delSpy).toHaveBeenCalledWith('key');
});
});