-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.test.js
116 lines (96 loc) · 3.41 KB
/
helpers.test.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const ini = require('ini');
const chalk = require('chalk');
const helpers = require('.././helpers');
const { NPMRC, NRMRC, REGISTRY } = require('.././constants');
// ========== mock `fs` within helpers.js ==========
let mockedFiles = Object.create(null);
function writeFileSync(path, content) {
mockedFiles[path] = content;
};
jest.mock("fs", () => {
const originalModule = jest.requireActual('fs');
function readFileSync(path) {
return mockedFiles[path];
};
/* for jest scope, so same to above */
function writeFileSync(path, content) {
mockedFiles[path] = content;
};
function existsSync(path) {
return path in mockedFiles;
};
return {
...originalModule,
existsSync: jest.fn(existsSync),
readFileSync: jest.fn(readFileSync),
writeFileSync: jest.fn(writeFileSync),
};
});
// ========== test cases ==========
beforeEach(() => {
mockedFiles = Object.create(null);
mockedFiles[NPMRC] = '';
mockedFiles[NRMRC] = '';
});
it('geneDashLine', () => {
const result1 = helpers.geneDashLine('taobao', 10);
const result2 = helpers.geneDashLine('taobao', 1);
expect(result1).toBe(` ${chalk.dim('-----')} `);
expect(result2).toBe(` ${chalk.dim('-')} `);
});
it('getCurrentRegistry', async () => {
const registry = ' https://registry.npmjs.org/';
writeFileSync(NPMRC, ini.stringify({ [REGISTRY]: registry }));
const currentRegistry = await helpers.getCurrentRegistry();
expect(currentRegistry).toBe(registry);
});
it('getRegistries', async () => {
const name = 'fake name';
const registry = 'https://registry.example.com/';
writeFileSync(NRMRC, ini.stringify({ [name]: { registry } }));
const registries = await helpers.getRegistries();
expect(Object.keys(registries).includes(name)).toBe(true);
});
it('readFile', async () => {
const content = 'hello nrm';
writeFileSync(NRMRC, ini.stringify({ content: content }));
const result1 = await helpers.readFile(NRMRC);
const result2 = await helpers.readFile('file not exist');
expect(result1.content).toBe(content);
expect(result2).toEqual(Object.create(null));
});
it('writeFile', async () => {
const content = { nrm: 'nrm is great' };
await helpers.writeFile(NRMRC, { content });
const result = await helpers.readFile(NRMRC);
expect(result.content).toEqual(content);
});
test('isLowerCaseEqual', () => {
const result1 = helpers.isLowerCaseEqual('taobao', 'TAOBAO');
const result2 = helpers.isLowerCaseEqual('jd', 'tb');
const result3 = helpers.isLowerCaseEqual('taobao', '');
const result4 = helpers.isLowerCaseEqual('', '');
expect(result1).toBe(true);
expect(result2).toBe(false);
expect(result3).toBe(false);
expect(result4).toBe(true);
});
it('isRegistryNotFound', async () => {
const unknown = 'unknown';
const name = 'custom name';
const registry = 'https://registry.example.com/';
writeFileSync(NRMRC, ini.stringify({ [name]: registry }));
const result1 = await helpers.isRegistryNotFound(unknown, false);
const result2 = await helpers.isRegistryNotFound(name, false);
expect(result1).toBe(true);
expect(result2).toBe(false);
});
it('isInternalRegistry', async () => {
const name = 'custom name';
const registry = 'https://registry.example.com/';
writeFileSync(NRMRC, ini.stringify({ [name]: registry }));
const result1 = await helpers.isInternalRegistry(name);
const result2 = await helpers.isInternalRegistry('npm');
expect(result1).toBe(false);
expect(result2).toBe(true);
});