Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
akupiec
GitHub Repository: akupiec/mad-proxy
Path: blob/master/test/mockData.test.js
115 views
1
/* eslint-env jest */
2
const mockData = require('../src/middlewares/mockData');
3
const utils = require('../src/middlewares/utils/utils');
4
5
6
jest.mock('../src/middlewares/utils/utils');
7
8
describe('mockData', function () {
9
let req, res, next;
10
beforeEach(function () {
11
res = {};
12
next = jest.fn();
13
req = {
14
httpVersion: '1.1',
15
complete: false,
16
headers: {
17
host: '127.0.0.1:9999',
18
'accept-encoding': 'gzip, deflate',
19
'user-agent': 'node-superagent/3.8.3',
20
},
21
url: '/abb-cc',
22
method: 'GET',
23
baseUrl: '/api',
24
originalUrl: '/api/abb-cc',
25
params: {},
26
query: {},
27
body: {},
28
};
29
});
30
31
it('create mock obj', function () {
32
mockData()(req, res, next);
33
expect(req.mock).toBeDefined();
34
expect(req.mock._hash).toBe('#824ac3d0');
35
});
36
37
it('create fileName', function () {
38
mockData()(req, res, next);
39
expect(req.mock._fileName).toBe('abb-cc');
40
});
41
42
it('create filePath', function() {
43
mockData()(req, res, next);
44
let filePath = req.mock.filePath;
45
filePath = filePath.replace(/\\/g, '/');
46
expect(filePath).toContain('/mockDir/api/GET/abb-cc#824ac3d0');
47
});
48
49
describe('should mockExists', function() {
50
it('be true when local file exists', function() {
51
utils.cacheFileResolve.mockReturnValue('someFilePath');
52
mockData()(req, res, next);
53
expect(req.mock.mockExists).toBe(true);
54
});
55
it('be false when file not exists', function() {
56
utils.cacheFileResolve.mockReturnValue(undefined);
57
mockData()(req, res, next);
58
expect(req.mock.mockExists).toBe(false);
59
});
60
});
61
});
62
63