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
124
125
126
127
128
129
130
|
const t = require('tap')
const { fake: mockNpm } = require('../../fixtures/mock-npm')
let result = ''
const noop = () => null
const config = {
unicode: false,
'star.unstar': false,
}
const npm = mockNpm({
config,
output: (...msg) => {
result += msg.join('\n')
},
})
const npmFetch = { json: noop }
const log = { error: noop, info: noop, verbose: noop }
const mocks = {
'proc-log': log,
'npm-registry-fetch': npmFetch,
'../../../lib/utils/get-identity.js': async () => 'foo',
'../../../lib/utils/usage.js': () => 'usage instructions',
}
const Star = t.mock('../../../lib/commands/star.js', mocks)
const star = new Star(npm)
t.afterEach(() => {
config.unicode = false
config['star.unstar'] = false
log.info = noop
result = ''
})
t.test('no args', async t => {
await t.rejects(
star.exec([]),
/usage instructions/,
'should throw usage instructions'
)
})
t.test('star a package', async t => {
t.plan(4)
const pkgName = '@npmcli/arborist'
npmFetch.json = async (uri, opts) => ({
_id: pkgName,
_rev: 'hash',
users: (
opts.method === 'PUT'
? { foo: true }
: {}
),
})
log.info = (title, msg, id) => {
t.equal(title, 'star', 'should use expected title')
t.equal(msg, 'starring', 'should use expected msg')
t.equal(id, pkgName, 'should use expected id')
}
await star.exec([pkgName])
t.equal(
result,
'(*) @npmcli/arborist',
'should output starred package msg'
)
})
t.test('unstar a package', async t => {
t.plan(4)
const pkgName = '@npmcli/arborist'
config['star.unstar'] = true
npmFetch.json = async (uri, opts) => ({
_id: pkgName,
_rev: 'hash',
...(opts.method === 'PUT'
? {}
: { foo: true }
),
})
log.info = (title, msg, id) => {
t.equal(title, 'unstar', 'should use expected title')
t.equal(msg, 'unstarring', 'should use expected msg')
t.equal(id, pkgName, 'should use expected id')
}
await star.exec([pkgName])
t.equal(
result,
'( ) @npmcli/arborist',
'should output unstarred package msg'
)
})
t.test('unicode', async t => {
t.test('star a package', async t => {
config.unicode = true
npmFetch.json = async (uri, opts) => ({})
await star.exec(['pkg'])
t.equal(
result,
'\u2605 pkg',
'should output unicode starred package msg'
)
})
t.test('unstar a package', async t => {
config.unicode = true
config['star.unstar'] = true
npmFetch.json = async (uri, opts) => ({})
await star.exec(['pkg'])
t.equal(
result,
'\u2606 pkg',
'should output unstarred package msg'
)
})
})
t.test('logged out user', async t => {
const Star = t.mock('../../../lib/commands/star.js', {
...mocks,
'../../../lib/utils/get-identity.js': async () => undefined,
})
const star = new Star(npm)
await t.rejects(
star.exec(['@npmcli/arborist']),
/You need to be logged in/,
'should throw login required error'
)
})
|