-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathlist.js
123 lines (100 loc) · 3.56 KB
/
list.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
117
118
119
120
121
122
123
/* @flow */
import type {Tree} from '../../src/reporters/types.js';
import {BufferReporter} from '../../src/reporters/index.js';
import {run as buildRun} from './_helpers.js';
import {getParent, getReqDepth, run as list} from '../../src/cli/commands/list.js';
import * as reporters from '../../src/reporters/index.js';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;
const path = require('path');
function makeTree(
name,
{children = [], hint = null, color = null, depth = 0}: Object = {},
): Tree {
return {
name,
children,
hint,
color,
depth,
};
}
const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'list');
const runList = buildRun.bind(null, BufferReporter, fixturesLoc, (args, flags, config, reporter): Promise<void> => {
return list(config, reporter, flags, args);
});
test.concurrent('throws if lockfile out of date', (): Promise<void> => {
const reporter = new reporters.ConsoleReporter({});
return new Promise(async (resolve) => {
try {
await runList([], {}, 'lockfile-outdated');
} catch (err) {
expect(err.message).toContain(reporter.lang('lockfileOutdated'));
} finally {
resolve();
}
});
});
test.concurrent('lists everything with no args', (): Promise<void> => {
return runList([], {}, 'no-args', (config, reporter): ?Promise<void> => {
const rprtr = new BufferReporter({});
const tree = reporter.getBuffer().slice(-1);
const children = [{name: 'is-plain-obj@^1.0.0', color: 'dim', shadow: true}];
const trees = [
makeTree('[email protected]', {color: 'bold'}),
makeTree('[email protected]', {children, color: 'bold'}),
makeTree('[email protected]'),
];
rprtr.tree('list', trees);
expect(tree).toEqual(rprtr.getBuffer());
});
});
test.concurrent('respects depth flag', (): Promise<void> => {
return runList([], {depth: 1}, 'depth-flag', (config, reporter): ?Promise<void> => {
const rprtr = new BufferReporter({});
const tree = reporter.getBuffer().slice(-1);
const trees = [
makeTree('[email protected]', {color: 'bold'}),
makeTree('[email protected]'),
];
rprtr.tree('list', trees);
expect(tree).toEqual(rprtr.getBuffer());
});
});
test.concurrent('accepts an argument', (): Promise<void> => {
return runList(['is-plain-obj'], {}, 'one-arg', (config, reporter): ?Promise<void> => {
const rprtr = new BufferReporter({});
const tree = reporter.getBuffer().slice(-1);
const trees = [
makeTree('[email protected]'),
];
rprtr.tree('list', trees);
expect(tree).toEqual(rprtr.getBuffer());
});
});
test('getParent should extract a parent object from a hash, if the parent key exists', () => {
const mockTreesByKey = {};
mockTreesByKey['parentPkg'] = {
name: '[email protected]',
children: [],
};
const res = getParent('parentPkg#childPkg', mockTreesByKey);
expect(res instanceof Object).toBe(true);
expect(res.name).toBe('[email protected]');
expect(res.children.length).toBe(0);
});
test('getParent should return undefined if the key does not exist in hash', () => {
const mockTreesByKey = {};
mockTreesByKey['parentPkg'] = { };
const res = getParent('parentPkg#childPkg', mockTreesByKey);
expect(res.name).not.toBeDefined();
expect(res.children).not.toBeDefined();
});
test('getReqDepth should return a number if valid', () => {
expect(getReqDepth('1')).toEqual(1);
expect(getReqDepth('01')).toEqual(1);
});
test('getReqDepth should return -1 if invalid', () => {
expect(getReqDepth('foo')).toEqual(-1);
expect(getReqDepth('bar')).toEqual(-1);
expect(getReqDepth('')).toEqual(-1);
});