Skip to content

test: add tests to lib/stars.js #2240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/stars.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
'use strict'

const npm = require('./npm.js')
const fetch = require('npm-registry-fetch')
const log = require('npmlog')
const fetch = require('npm-registry-fetch')

const npm = require('./npm.js')
const output = require('./utils/output.js')
const getIdentity = require('./utils/get-identity.js')
const usageUtil = require('./utils/usage.js')
const usage = usageUtil('stars', 'npm stars [<user>]')
const completion = require('./utils/completion/none.js')

const usage = usageUtil('stars', 'npm stars [<user>]')

const cmd = (args, cb) => stars(args).then(() => cb()).catch(cb)

const stars = (args) => {
return stars_(args).catch(er => {
if (er.code === 'ENEEDAUTH')
log.warn('star', 'auth is required to look up your username')
log.warn('stars', 'auth is required to look up your username')

throw er
})
Expand All @@ -25,6 +27,7 @@ const stars_ = async ([user = getIdentity(npm.flatOptions)]) => {
...npm.flatOptions,
query: { key: `"${await user}"` },
})

if (rows.length === 0)
log.warn('stars', 'user has not starred any packages')

Expand Down
15 changes: 15 additions & 0 deletions tap-snapshots/test-lib-stars.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/stars.js TAP no args > should output a list of starred packages 1`] = `

@npmcli/arborist
@npmcli/map-workspaces
libnpmfund
libnpmpublish
ipt
`
151 changes: 151 additions & 0 deletions test/lib/stars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const requireInject = require('require-inject')
const t = require('tap')

let result = ''

const noop = () => null
const npm = { config: { get () {} }, flatOptions: {} }
const npmFetch = { json: noop }
const npmlog = { warn: noop }
const mocks = {
npmlog,
'npm-registry-fetch': npmFetch,
'../../lib/npm.js': npm,
'../../lib/utils/output.js': (...msg) => {
result = [result, ...msg].join('\n')
},
'../../lib/utils/get-identity.js': async () => 'foo',
'../../lib/utils/usage.js': () => 'usage instructions',
}

const stars = requireInject('../../lib/stars.js', mocks)

t.afterEach(cb => {
npm.config = { get () {} }
npmlog.warn = noop
result = ''
cb()
})

t.test('no args', t => {
npmFetch.json = async (uri, opts) => {
t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
t.equal(opts.query.key, '"foo"', 'should match logged in username')

return {
rows: [
{ value: '@npmcli/arborist' },
{ value: '@npmcli/map-workspaces' },
{ value: 'libnpmfund' },
{ value: 'libnpmpublish' },
{ value: 'ipt' },
],
}
}

stars([], err => {
if (err)
throw err

t.matchSnapshot(
result,
'should output a list of starred packages'
)

t.end()
})
})

t.test('npm star <user>', t => {
t.plan(3)
npmFetch.json = async (uri, opts) => {
t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
t.equal(opts.query.key, '"ruyadorno"', 'should match username')

return {
rows: [{ value: '@npmcli/arborist' }],
}
}

stars(['ruyadorno'], err => {
if (err)
throw err

t.match(
result,
'@npmcli/arborist',
'should output expected list of starred packages'
)
})
})

t.test('unauthorized request', t => {
t.plan(4)
npmFetch.json = async () => {
throw Object.assign(
new Error('Not logged in'),
{ code: 'ENEEDAUTH' }
)
}

npmlog.warn = (title, msg) => {
t.equal(title, 'stars', 'should use expected title')
t.equal(
msg,
'auth is required to look up your username',
'should warn auth required msg'
)
}

stars([], err => {
t.match(
err,
/Not logged in/,
'should throw unauthorized request msg'
)

t.equal(
result,
'',
'should have empty output'
)
})
})

t.test('unexpected error', t => {
npmFetch.json = async () => {
throw new Error('ERROR')
}

npmlog.warn = (title, msg) => {
throw new Error('Should not output extra warning msgs')
}

stars([], err => {
t.match(
err,
/ERROR/,
'should throw unexpected error message'
)
t.end()
})
})

t.test('no pkg starred', t => {
t.plan(2)
npmFetch.json = async (uri, opts) => ({ rows: [] })

npmlog.warn = (title, msg) => {
t.equal(title, 'stars', 'should use expected title')
t.equal(
msg,
'user has not starred any packages',
'should warn no starred packages msg'
)
}

stars([], err => {
if (err)
throw err
})
})