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

star.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64efd9ef8c7ed7dfe6d2bc508e3ffd0eeb0a1fb1 (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
const requireInject = require('require-inject')
const t = require('tap')

let result = ''

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

const Star = requireInject('../../lib/star.js', mocks)
const star = new Star(npm)

t.afterEach(cb => {
  npm.config = { get () {} }
  npm.flatOptions.unicode = false
  npmlog.info = noop
  result = ''
  cb()
})

t.test('no args', t => {
  star.exec([], err => {
    t.match(
      err,
      /usage instructions/,
      'should throw usage instructions'
    )
    t.end()
  })
})

t.test('star a package', t => {
  t.plan(4)
  const pkgName = '@npmcli/arborist'
  npmFetch.json = async (uri, opts) => ({
    _id: pkgName,
    _rev: 'hash',
    users: (
      opts.method === 'PUT'
        ? { foo: true }
        : {}
    ),
  })
  npmlog.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')
  }
  star.exec([pkgName], err => {
    if (err)
      throw err
    t.equal(
      result,
      '(*) @npmcli/arborist',
      'should output starred package msg'
    )
  })
})

t.test('unstar a package', t => {
  t.plan(4)
  const pkgName = '@npmcli/arborist'
  npm.config.get = key => key === 'star.unstar'
  npmFetch.json = async (uri, opts) => ({
    _id: pkgName,
    _rev: 'hash',
    ...(opts.method === 'PUT'
      ? {}
      : { foo: true }
    ),
  })
  npmlog.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')
  }
  star.exec([pkgName], err => {
    if (err)
      throw err
    t.equal(
      result,
      '( ) @npmcli/arborist',
      'should output unstarred package msg'
    )
  })
})

t.test('unicode', async t => {
  t.test('star a package', t => {
    npm.flatOptions.unicode = true
    npmFetch.json = async (uri, opts) => ({})
    star.exec(['pkg'], err => {
      if (err)
        throw err
      t.equal(
        result,
        '\u2605  pkg',
        'should output unicode starred package msg'
      )
      t.end()
    })
  })

  t.test('unstar a package', t => {
    npm.flatOptions.unicode = true
    npm.config.get = key => key === 'star.unstar'
    npmFetch.json = async (uri, opts) => ({})
    star.exec(['pkg'], err => {
      if (err)
        throw err
      t.equal(
        result,
        '\u2606  pkg',
        'should output unstarred package msg'
      )
      t.end()
    })
  })
})

t.test('logged out user', t => {
  const Star = requireInject('../../lib/star.js', {
    ...mocks,
    '../../lib/utils/get-identity.js': async () => undefined,
  })
  const star = new Star(npm)
  star.exec(['@npmcli/arborist'], err => {
    t.match(
      err,
      /You need to be logged in/,
      'should throw login required error'
    )
    t.end()
  })
})