Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stars.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 383b5adf426777cc057dae2c1d927e8fea8bea73 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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/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)
const stars = new Stars(npm)

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.exec([], 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.exec(['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.exec([], 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.exec([], 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.exec([], err => {
    if (err)
      throw err
  })
})