forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerts-routes.test.js
More file actions
83 lines (61 loc) · 2.19 KB
/
Copy pathalerts-routes.test.js
File metadata and controls
83 lines (61 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const adminApiKey = 'a'.repeat(64);
process.env.ADMIN_API_KEY = adminApiKey;
const mockStore = new Map();
const mockSortedSets = new Map();
const mockRedis = {
smembers: jest.fn(async () => []),
zadd: jest.fn(async (key, score, member) => {
if (!mockSortedSets.has(key)) mockSortedSets.set(key, new Map());
mockSortedSets.get(key).set(member, score);
}),
zrem: jest.fn(async (key, member) => {
mockSortedSets.get(key)?.delete(member);
}),
zcard: jest.fn(async (key) => mockSortedSets.get(key)?.size || 0),
zrevrange: jest.fn(async (key, start, stop) => {
const sortedSet = mockSortedSets.get(key);
if (!sortedSet) return [];
const entries = Array.from(sortedSet.entries()).sort((a, b) => b[1] - a[1]);
const startIdx = start === -1 ? entries.length + start : start;
const stopIdx = stop === -1 ? entries.length + stop : stop;
return entries.slice(startIdx, stopIdx + 1).map(([member]) => member);
}),
};
jest.mock('../src/services/cache', () => ({
getClient: () => mockRedis,
get: jest.fn(async (key) => mockStore.get(key) || null),
set: jest.fn(async (key, value) => mockStore.set(key, value)),
del: jest.fn(async (key) => mockStore.delete(key)),
disconnect: jest.fn(),
isConnected: jest.fn(() => false),
}));
jest.mock('../src/logger', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}));
const request = require('supertest');
const { app, server } = require('../src');
const priceRefreshJob = require('../src/jobs/priceRefresh');
beforeEach(() => {
mockStore.clear();
mockSortedSets.clear();
});
describe('GET /api/v1/alerts pagination', () => {
afterAll((done) => {
priceRefreshJob.stop();
if (server) server.close(done);
else done();
});
test('returns pagination envelope', async () => {
const response = await request(app)
.get('/api/v1/alerts')
.set('Authorization', `Bearer ${adminApiKey}`);
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('data');
expect(response.body).toHaveProperty('pagination');
expect(response.body.pagination).toHaveProperty('page');
expect(response.body.pagination).toHaveProperty('limit');
});
});