|
| 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