Skip to content

Commit 67cef80

Browse files
committed
test: add tests to lib/stars.js
PR-URL: #2240 Credit: @ruyadorno Close: #2240 Reviewed-by: @darcyclarke
1 parent 997cbdb commit 67cef80

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

lib/stars.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
'use strict'
22

3-
const npm = require('./npm.js')
4-
const fetch = require('npm-registry-fetch')
53
const log = require('npmlog')
4+
const fetch = require('npm-registry-fetch')
5+
6+
const npm = require('./npm.js')
67
const output = require('./utils/output.js')
78
const getIdentity = require('./utils/get-identity.js')
89
const usageUtil = require('./utils/usage.js')
9-
const usage = usageUtil('stars', 'npm stars [<user>]')
1010
const completion = require('./utils/completion/none.js')
1111

12+
const usage = usageUtil('stars', 'npm stars [<user>]')
13+
1214
const cmd = (args, cb) => stars(args).then(() => cb()).catch(cb)
1315

1416
const stars = (args) => {
1517
return stars_(args).catch(er => {
1618
if (er.code === 'ENEEDAUTH')
17-
log.warn('star', 'auth is required to look up your username')
19+
log.warn('stars', 'auth is required to look up your username')
1820

1921
throw er
2022
})
@@ -25,6 +27,7 @@ const stars_ = async ([user = getIdentity(npm.flatOptions)]) => {
2527
...npm.flatOptions,
2628
query: { key: `"${await user}"` },
2729
})
30+
2831
if (rows.length === 0)
2932
log.warn('stars', 'user has not starred any packages')
3033

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/lib/stars.js TAP no args > should output a list of starred packages 1`] = `
9+
10+
@npmcli/arborist
11+
@npmcli/map-workspaces
12+
libnpmfund
13+
libnpmpublish
14+
ipt
15+
`

test/lib/stars.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const requireInject = require('require-inject')
2+
const t = require('tap')
3+
4+
let result = ''
5+
6+
const noop = () => null
7+
const npm = { config: { get () {} }, flatOptions: {} }
8+
const npmFetch = { json: noop }
9+
const npmlog = { warn: noop }
10+
const mocks = {
11+
npmlog,
12+
'npm-registry-fetch': npmFetch,
13+
'../../lib/npm.js': npm,
14+
'../../lib/utils/output.js': (...msg) => {
15+
result = [result, ...msg].join('\n')
16+
},
17+
'../../lib/utils/get-identity.js': async () => 'foo',
18+
'../../lib/utils/usage.js': () => 'usage instructions',
19+
}
20+
21+
const stars = requireInject('../../lib/stars.js', mocks)
22+
23+
t.afterEach(cb => {
24+
npm.config = { get () {} }
25+
npmlog.warn = noop
26+
result = ''
27+
cb()
28+
})
29+
30+
t.test('no args', t => {
31+
npmFetch.json = async (uri, opts) => {
32+
t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
33+
t.equal(opts.query.key, '"foo"', 'should match logged in username')
34+
35+
return {
36+
rows: [
37+
{ value: '@npmcli/arborist' },
38+
{ value: '@npmcli/map-workspaces' },
39+
{ value: 'libnpmfund' },
40+
{ value: 'libnpmpublish' },
41+
{ value: 'ipt' },
42+
],
43+
}
44+
}
45+
46+
stars([], err => {
47+
if (err)
48+
throw err
49+
50+
t.matchSnapshot(
51+
result,
52+
'should output a list of starred packages'
53+
)
54+
55+
t.end()
56+
})
57+
})
58+
59+
t.test('npm star <user>', t => {
60+
t.plan(3)
61+
npmFetch.json = async (uri, opts) => {
62+
t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
63+
t.equal(opts.query.key, '"ruyadorno"', 'should match username')
64+
65+
return {
66+
rows: [{ value: '@npmcli/arborist' }],
67+
}
68+
}
69+
70+
stars(['ruyadorno'], err => {
71+
if (err)
72+
throw err
73+
74+
t.match(
75+
result,
76+
'@npmcli/arborist',
77+
'should output expected list of starred packages'
78+
)
79+
})
80+
})
81+
82+
t.test('unauthorized request', t => {
83+
t.plan(4)
84+
npmFetch.json = async () => {
85+
throw Object.assign(
86+
new Error('Not logged in'),
87+
{ code: 'ENEEDAUTH' }
88+
)
89+
}
90+
91+
npmlog.warn = (title, msg) => {
92+
t.equal(title, 'stars', 'should use expected title')
93+
t.equal(
94+
msg,
95+
'auth is required to look up your username',
96+
'should warn auth required msg'
97+
)
98+
}
99+
100+
stars([], err => {
101+
t.match(
102+
err,
103+
/Not logged in/,
104+
'should throw unauthorized request msg'
105+
)
106+
107+
t.equal(
108+
result,
109+
'',
110+
'should have empty output'
111+
)
112+
})
113+
})
114+
115+
t.test('unexpected error', t => {
116+
npmFetch.json = async () => {
117+
throw new Error('ERROR')
118+
}
119+
120+
npmlog.warn = (title, msg) => {
121+
throw new Error('Should not output extra warning msgs')
122+
}
123+
124+
stars([], err => {
125+
t.match(
126+
err,
127+
/ERROR/,
128+
'should throw unexpected error message'
129+
)
130+
t.end()
131+
})
132+
})
133+
134+
t.test('no pkg starred', t => {
135+
t.plan(2)
136+
npmFetch.json = async (uri, opts) => ({ rows: [] })
137+
138+
npmlog.warn = (title, msg) => {
139+
t.equal(title, 'stars', 'should use expected title')
140+
t.equal(
141+
msg,
142+
'user has not starred any packages',
143+
'should warn no starred packages msg'
144+
)
145+
}
146+
147+
stars([], err => {
148+
if (err)
149+
throw err
150+
})
151+
})

0 commit comments

Comments
 (0)