-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
101 lines (87 loc) · 2.81 KB
/
index.test.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { createServer } from 'node:http';
import * as de from 'descript';
import { Redis } from 'ioredis';
import type { Mock } from 'vitest';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { Cache } from './index';
import type { Server } from 'http';
vi.mock('ioredis', () => {
const Redis = vi.fn();
Redis.prototype.get = vi.fn().mockResolvedValue(undefined);
Redis.prototype.set = vi.fn().mockResolvedValue(undefined);
return {
Redis,
};
});
const redisGet = Redis.prototype.get as Mock;
const redisSet = Redis.prototype.set as Mock;
let cache: Cache<never>;
let server: Server;
beforeEach(() => {
cache = new Cache<never>({
redis: {},
});
server = createServer((req, res) => {
res.statusCode = 200;
res.setHeader('date', new Date(0).toUTCString());
res.end('some http value');
});
server.listen(8080);
redisGet.mockReset();
redisSet.mockReset();
});
afterEach(() => {
server.close();
});
it('de.func', async() => {
const block = de.func({
block: () => {
return Promise.resolve('somevalue');
},
options: {
key: () => 'somekey',
cache,
},
});
await de.run(block);
expect(redisGet).toHaveBeenCalledOnce();
expect(redisGet).toHaveBeenCalledWith(
'e33d0afae8e08752efa1a467653931ae9ba60c6a3ea693e684a6a56ef3b18ba3c7e711edee33f99471d9bb2d02302e92512cc3c8513ab473bbe71d52b8f7e39a',
expect.anything(),
);
expect(redisSet).toHaveBeenCalledOnce();
expect(redisSet).toHaveBeenCalledWith(
'e33d0afae8e08752efa1a467653931ae9ba60c6a3ea693e684a6a56ef3b18ba3c7e711edee33f99471d9bb2d02302e92512cc3c8513ab473bbe71d52b8f7e39a',
'"somevalue"',
'EX',
86400,
expect.anything(),
);
});
it('de.http', async() => {
const block = de.http({
block: {
hostname: 'localhost',
port: 8080,
pathname: '/',
},
options: {
key: () => 'somekey',
cache,
},
});
await de.run(block);
expect(redisGet).toHaveBeenCalledOnce();
expect(redisGet).toHaveBeenCalledWith(
'e33d0afae8e08752efa1a467653931ae9ba60c6a3ea693e684a6a56ef3b18ba3c7e711edee33f99471d9bb2d02302e92512cc3c8513ab473bbe71d52b8f7e39a',
expect.anything(),
);
expect(redisSet).toHaveBeenCalledOnce();
expect(redisSet).toHaveBeenCalledWith(
'e33d0afae8e08752efa1a467653931ae9ba60c6a3ea693e684a6a56ef3b18ba3c7e711edee33f99471d9bb2d02302e92512cc3c8513ab473bbe71d52b8f7e39a',
'{"statusCode":200,"headers":{"date":"Thu, 01 Jan 1970 00:00:00 GMT","connection":"keep-alive","keep-alive":"timeout=5","content-length":"15"},"result":"some http value"}',
'EX',
86400,
expect.anything(),
);
});