|
| 1 | +import { createClient } from 'redis'; |
| 2 | +import Redis from '../../../services/Redis'; |
| 3 | + |
| 4 | +const mockSet = jest.fn((key, value, cb) => cb()); |
| 5 | +const mockQuit = jest.fn(); |
| 6 | +const mockDel = jest.fn((key, cb) => cb()); |
| 7 | +const mockExpire = jest.fn((key, time, cb) => cb()); |
| 8 | +const mockExpireAt = jest.fn((key, time, cb) => cb()); |
| 9 | +const mockGet = jest.fn((key, cb) => cb(null, 'someData')); |
| 10 | + |
| 11 | +jest.mock('redis', () => ({ |
| 12 | + createClient: jest.fn(() => ({ |
| 13 | + del: mockDel, |
| 14 | + expire: mockExpire, |
| 15 | + expireat: mockExpireAt, |
| 16 | + get: mockGet, |
| 17 | + set: mockSet, |
| 18 | + quit: mockQuit, |
| 19 | + })), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('services/redis/Redis', () => { |
| 23 | + const key = 'foo'; |
| 24 | + const value = 'bar'; |
| 25 | + |
| 26 | + [true, false].forEach(ephemeral => { |
| 27 | + const isEphemeral = ephemeral ? 'ephemeral' : 'nonEphemeral'; |
| 28 | + const cache = new Redis({}, ephemeral); |
| 29 | + |
| 30 | + describe(isEphemeral, () => { |
| 31 | + beforeEach(() => { |
| 32 | + mockQuit.mockReset(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + if (ephemeral) { |
| 37 | + expect(createClient).toHaveBeenCalled(); |
| 38 | + expect(mockQuit).toHaveBeenCalledWith(); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it('persists the key/value pair in the cache', async () => { |
| 43 | + await cache.put(key, value); |
| 44 | + |
| 45 | + expect(mockSet).toHaveBeenCalledWith('foo', 'bar', expect.any(Function)); |
| 46 | + }); |
| 47 | + |
| 48 | + it('persists the key/value pair in the cache to expire at a particular time', async () => { |
| 49 | + const timestamp = (new Date()).getTime(); |
| 50 | + await cache.put(key, value, { |
| 51 | + at: timestamp, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(mockSet).toHaveBeenCalledWith('foo', 'bar', expect.any(Function)); |
| 55 | + expect(mockExpireAt).toHaveBeenCalledWith( |
| 56 | + 'foo', |
| 57 | + timestamp, |
| 58 | + expect.any(Function), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('persists the key/value pair in the cache to expire in a given number of seconds', async () => { |
| 63 | + await cache.put(key, value, { |
| 64 | + in: 1, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(mockSet).toHaveBeenCalledWith('foo', 'bar', expect.any(Function)); |
| 68 | + expect(mockExpire).toHaveBeenCalledWith('foo', 1, expect.any(Function)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('retrieves the keys value from the cache', async () => { |
| 72 | + const data = await cache.get(key); |
| 73 | + |
| 74 | + expect(mockGet).toHaveBeenCalledWith('foo', expect.any(Function)); |
| 75 | + expect(data).toEqual('someData'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('removes the key/value pair from the cache', async () => { |
| 79 | + await cache.remove(key); |
| 80 | + |
| 81 | + expect(mockDel).toHaveBeenCalledWith('foo', expect.any(Function)); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments