-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpackage-optimization.test.ts
More file actions
105 lines (91 loc) · 3.35 KB
/
package-optimization.test.ts
File metadata and controls
105 lines (91 loc) · 3.35 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { describe, expect, test, mock, spyOn } from 'bun:test';
// Mock modules before any imports
mock.module('filesize-parser', () => ({ default: () => 0 }));
mock.module('form-data', () => ({ default: class {} }));
mock.module('node-fetch', () => ({ default: () => {} }));
mock.module('progress', () => ({ default: class { tick() {} } }));
mock.module('tcp-ping', () => ({ default: { ping: () => {} } }));
mock.module('tty-table', () => {
const mockTable = () => ({ render: () => '' });
return { default: mockTable };
});
mock.module('read', () => ({ read: () => {} }));
mock.module('chalk', () => ({
default: {
green: (s: string) => s,
red: (s: string) => s,
yellow: (s: string) => s,
},
}));
mock.module('i18next', () => ({
default: {
t: (k: string) => k,
use: () => ({ init: () => {} }),
init: () => {},
},
}));
mock.module('fs-extra', () => ({
default: {
existsSync: () => false,
readFileSync: () => '',
ensureDirSync: () => {},
pathExists: async () => false,
remove: async () => {},
},
}));
mock.module('compare-versions', () => ({
satisfies: () => true,
}));
mock.module('yauzl', () => ({
open: () => {},
}));
mock.module('isomorphic-git', () => ({}));
mock.module('global-dirs', () => ({
npm: { packages: '' },
yarn: { packages: '' },
}));
mock.module('registry-auth-token', () => ({}));
mock.module('registry-auth-token/registry-url', () => () => 'https://registry.npmjs.org');
mock.module('semver', () => ({}));
mock.module('semver/functions/gt', () => () => true);
mock.module('semver/ranges/max-satisfying', () => (versions: string[]) => versions[0]);
mock.module('bytebuffer', () => ({}));
import * as api from '../src/api';
import * as utils from '../src/utils';
import { choosePackage } from '../src/package';
describe('choosePackage optimization', () => {
test('should return the correct package when a valid ID is entered', async () => {
const mockPackages = [
{ id: '101', name: 'package1' },
{ id: '102', name: 'package2' },
];
const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any);
const questionSpy = spyOn(utils, 'question').mockResolvedValue('102');
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {});
const result = await choosePackage('app123');
expect(result).toEqual(mockPackages[1] as any);
expect(getAllPackagesSpy).toHaveBeenCalledWith('app123');
expect(questionSpy).toHaveBeenCalled();
getAllPackagesSpy.mockRestore();
questionSpy.mockRestore();
consoleSpy.mockRestore();
});
test('should continue to prompt until a valid ID is entered', async () => {
const mockPackages = [
{ id: '201', name: 'packageA' },
];
const getAllPackagesSpy = spyOn(api, 'getAllPackages').mockResolvedValue(mockPackages as any);
const questionMock = mock();
questionMock
.mockResolvedValueOnce('999') // Invalid ID
.mockResolvedValueOnce('201'); // Valid ID
const questionSpy = spyOn(utils, 'question').mockImplementation(questionMock);
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {});
const result = await choosePackage('app123');
expect(result).toEqual(mockPackages[0] as any);
expect(questionMock).toHaveBeenCalledTimes(2);
getAllPackagesSpy.mockRestore();
questionSpy.mockRestore();
consoleSpy.mockRestore();
});
});