Skip to content

Commit 35771fb

Browse files
test(apps): stub @heroku/sdk directly, drop nock
1 parent eb26766 commit 35771fb

1 file changed

Lines changed: 72 additions & 100 deletions

File tree

‎test/unit/commands/apps/index.unit.test.ts‎

Lines changed: 72 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
import {runCommand} from '@heroku-cli/test-utils'
2+
import {HerokuSDK} from '@heroku/sdk'
23
import {expect} from 'chai'
3-
import nock from 'nock'
4+
import * as sinon from 'sinon'
45

56
import Apps from '../../../../src/commands/apps/index.js'
67
import removeAllWhitespace from '../../../helpers/utils/remove-whitespaces.js'
78

9+
type FakePlatform = {
10+
account: {info: sinon.SinonStub}
11+
app: {
12+
list: sinon.SinonStub
13+
listOwnedAndCollaborated: sinon.SinonStub
14+
}
15+
space: {info: sinon.SinonStub}
16+
teamApp: {listByTeam: sinon.SinonStub}
17+
}
18+
19+
function buildFakePlatform(): FakePlatform {
20+
return {
21+
account: {info: sinon.stub()},
22+
app: {
23+
list: sinon.stub(),
24+
listOwnedAndCollaborated: sinon.stub(),
25+
},
26+
space: {info: sinon.stub()},
27+
teamApp: {listByTeam: sinon.stub()},
28+
}
29+
}
30+
831
describe('apps', function () {
932
const example = {
1033
name: 'example',
@@ -76,40 +99,32 @@ describe('apps', function () {
7699
space: {id: 'test-space-id', name: 'test-space'},
77100
}
78101

79-
let euLockedApp = {}
80-
let euInternalApp = {}
81-
let euInternalLockedApp = {}
82-
let api: nock.Scope
102+
let fakePlatform: FakePlatform
83103

84104
beforeEach(function () {
85-
api = nock('https://api.heroku.com')
105+
fakePlatform = buildFakePlatform()
106+
sinon.stub(HerokuSDK.prototype, 'platform').get(() => fakePlatform)
86107
})
87108

88109
afterEach(function () {
89-
api.done()
90-
nock.cleanAll()
110+
sinon.restore()
91111
})
92112

93113
describe('with no args', function () {
94114
it('displays a message when the user has no apps', async function () {
95-
api
96-
.get('/account')
97-
.reply(200, {email: 'foo@bar.com'})
98-
.get('/users/~/apps')
99-
.reply(200, [])
115+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
116+
fakePlatform.app.listOwnedAndCollaborated.resolves([])
100117

101118
const {stderr, stdout} = await runCommand(Apps, [])
102119

103120
expect(stderr).to.equal('')
104121
expect(stdout).to.equal('You have no apps.\n')
122+
expect(fakePlatform.app.listOwnedAndCollaborated.calledOnceWithExactly('~')).to.equal(true)
105123
})
106124

107125
it('list all user apps', async function () {
108-
api
109-
.get('/account')
110-
.reply(200, {email: 'foo@bar.com'})
111-
.get('/users/~/apps')
112-
.reply(200, [example, collabApp])
126+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
127+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, collabApp])
113128

114129
const {stderr, stdout} = await runCommand(Apps, [])
115130

@@ -126,11 +141,8 @@ describe('apps', function () {
126141
})
127142

128143
it('lists all apps', async function () {
129-
api
130-
.get('/account')
131-
.reply(200, {email: 'foo@bar.com'})
132-
.get('/apps')
133-
.reply(200, [example, collabApp, teamApp1])
144+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
145+
fakePlatform.app.list.resolves([example, collabApp, teamApp1])
134146

135147
const {stderr, stdout} = await runCommand(Apps, ['--all'])
136148

@@ -144,14 +156,13 @@ describe('apps', function () {
144156
expect(actual).to.include(expectedPersonalApps)
145157
expect(actual).to.include(expectedCollaboratedAppsHeader)
146158
expect(actual).to.include(expectedCollaboratedApps)
159+
expect(fakePlatform.app.list.calledOnce).to.equal(true)
160+
expect(fakePlatform.app.listOwnedAndCollaborated.called).to.equal(false)
147161
})
148162

149163
it('shows as json', async function () {
150-
api
151-
.get('/account')
152-
.reply(200, {email: 'foo@bar.com'})
153-
.get('/users/~/apps')
154-
.reply(200, [example, collabApp])
164+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
165+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, collabApp])
155166

156167
const {stderr, stdout} = await runCommand(Apps, ['--json'])
157168

@@ -160,11 +171,8 @@ describe('apps', function () {
160171
})
161172

162173
it('shows region if not us', async function () {
163-
api
164-
.get('/account')
165-
.reply(200, {email: 'foo@bar.com'})
166-
.get('/users/~/apps')
167-
.reply(200, [example, euApp])
174+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
175+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp])
168176

169177
const {stderr, stdout} = await runCommand(Apps, [])
170178

@@ -173,11 +181,8 @@ describe('apps', function () {
173181
})
174182

175183
it('shows locked app', async function () {
176-
api
177-
.get('/account')
178-
.reply(200, {email: 'foo@bar.com'})
179-
.get('/users/~/apps')
180-
.reply(200, [example, euApp, lockedApp])
184+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
185+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, lockedApp])
181186

182187
const {stderr, stdout} = await runCommand(Apps, [])
183188

@@ -186,13 +191,9 @@ describe('apps', function () {
186191
})
187192

188193
it('shows locked eu app', async function () {
189-
euLockedApp = Object.assign(lockedApp, {region: {name: 'eu'}})
190-
191-
api
192-
.get('/account')
193-
.reply(200, {email: 'foo@bar.com'})
194-
.get('/users/~/apps')
195-
.reply(200, [example, euApp, euLockedApp])
194+
const euLockedApp = {...lockedApp, region: {name: 'eu'}}
195+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
196+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, euLockedApp])
196197

197198
const {stderr, stdout} = await runCommand(Apps, [])
198199

@@ -201,11 +202,8 @@ describe('apps', function () {
201202
})
202203

203204
it('shows internal app', async function () {
204-
api
205-
.get('/account')
206-
.reply(200, {email: 'foo@bar.com'})
207-
.get('/users/~/apps')
208-
.reply(200, [example, euApp, internalApp])
205+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
206+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, internalApp])
209207

210208
const {stderr, stdout} = await runCommand(Apps, [])
211209

@@ -214,11 +212,8 @@ describe('apps', function () {
214212
})
215213

216214
it('shows internal locked app', async function () {
217-
api
218-
.get('/account')
219-
.reply(200, {email: 'foo@bar.com'})
220-
.get('/users/~/apps')
221-
.reply(200, [example, euApp, internalLockedApp])
215+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
216+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, internalLockedApp])
222217

223218
const {stderr, stdout} = await runCommand(Apps, [])
224219

@@ -227,13 +222,9 @@ describe('apps', function () {
227222
})
228223

229224
it('shows internal eu app', async function () {
230-
euInternalApp = Object.assign(internalApp, {region: {name: 'eu'}})
231-
232-
api
233-
.get('/account')
234-
.reply(200, {email: 'foo@bar.com'})
235-
.get('/users/~/apps')
236-
.reply(200, [example, euApp, euInternalApp])
225+
const euInternalApp = {...internalApp, region: {name: 'eu'}}
226+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
227+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, euInternalApp])
237228

238229
const {stderr, stdout} = await runCommand(Apps, [])
239230

@@ -242,13 +233,9 @@ describe('apps', function () {
242233
})
243234

244235
it('shows internal locked eu app', async function () {
245-
euInternalLockedApp = Object.assign(internalLockedApp, {region: {name: 'eu'}})
246-
247-
api
248-
.get('/account')
249-
.reply(200, {email: 'foo@bar.com'})
250-
.get('/users/~/apps')
251-
.reply(200, [example, euApp, euInternalLockedApp])
236+
const euInternalLockedApp = {...internalLockedApp, region: {name: 'eu'}}
237+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
238+
fakePlatform.app.listOwnedAndCollaborated.resolves([example, euApp, euInternalLockedApp])
252239

253240
const {stderr, stdout} = await runCommand(Apps, [])
254241

@@ -259,24 +246,19 @@ describe('apps', function () {
259246

260247
describe('with team', function () {
261248
it('displays a message when the team has no apps', async function () {
262-
api
263-
.get('/account')
264-
.reply(200, {email: 'foo@bar.com'})
265-
.get('/teams/test-team/apps')
266-
.reply(200, [])
249+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
250+
fakePlatform.teamApp.listByTeam.resolves([])
267251

268252
const {stderr, stdout} = await runCommand(Apps, ['--team', 'test-team'])
269253

270254
expect(stderr).to.equal('')
271255
expect(stdout).to.equal('There are no apps in team test-team.\n')
256+
expect(fakePlatform.teamApp.listByTeam.calledOnceWithExactly('test-team')).to.equal(true)
272257
})
273258

274259
it('list all in a team', async function () {
275-
api
276-
.get('/account')
277-
.reply(200, {email: 'foo@bar.com'})
278-
.get('/teams/test-team/apps')
279-
.reply(200, [teamApp1, teamApp2])
260+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
261+
fakePlatform.teamApp.listByTeam.resolves([teamApp1, teamApp2])
280262

281263
const {stderr, stdout} = await runCommand(Apps, ['--team', 'test-team'])
282264

@@ -287,28 +269,22 @@ describe('apps', function () {
287269

288270
describe('with space', function () {
289271
it('displays a message when the space has no apps', async function () {
290-
api
291-
.get('/account')
292-
.reply(200, {email: 'foo@bar.com'})
293-
.get('/spaces/test-space')
294-
.reply(200, {team: {name: 'test-team'}})
295-
.get('/teams/test-team/apps')
296-
.reply(200, [])
272+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
273+
fakePlatform.space.info.resolves({team: {name: 'test-team'}})
274+
fakePlatform.teamApp.listByTeam.resolves([])
297275

298276
const {stderr, stdout} = await runCommand(Apps, ['--space', 'test-space'])
299277

300278
expect(stderr).to.equal('')
301279
expect(stdout).to.equal('There are no apps in space ⬡ test-space.\n')
280+
expect(fakePlatform.space.info.calledOnceWithExactly('test-space')).to.equal(true)
281+
expect(fakePlatform.teamApp.listByTeam.calledOnceWithExactly('test-team')).to.equal(true)
302282
})
303283

304284
it('lists only apps in spaces by name', async function () {
305-
api
306-
.get('/account')
307-
.reply(200, {email: 'foo@bar.com'})
308-
.get('/spaces/test-space')
309-
.reply(200, {team: {name: 'test-team'}})
310-
.get('/teams/test-team/apps')
311-
.reply(200, [teamSpaceApp1, teamSpaceApp2, teamApp1])
285+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
286+
fakePlatform.space.info.resolves({team: {name: 'test-team'}})
287+
fakePlatform.teamApp.listByTeam.resolves([teamSpaceApp1, teamSpaceApp2, teamApp1])
312288

313289
const {stderr, stdout} = await runCommand(Apps, ['--space', 'test-space'])
314290

@@ -317,13 +293,9 @@ describe('apps', function () {
317293
})
318294

319295
it('lists only internal apps in spaces by name', async function () {
320-
api
321-
.get('/account')
322-
.reply(200, {email: 'foo@bar.com'})
323-
.get('/spaces/test-space')
324-
.reply(200, {team: {name: 'test-team'}})
325-
.get('/teams/test-team/apps')
326-
.reply(200, [teamSpaceApp1, teamSpaceApp2, teamApp1, teamSpaceInternalApp])
296+
fakePlatform.account.info.resolves({email: 'foo@bar.com'})
297+
fakePlatform.space.info.resolves({team: {name: 'test-team'}})
298+
fakePlatform.teamApp.listByTeam.resolves([teamSpaceApp1, teamSpaceApp2, teamApp1, teamSpaceInternalApp])
327299

328300
const {stderr, stdout} = await runCommand(Apps, ['--space', 'test-space', '--internal-routing'])
329301

0 commit comments

Comments
 (0)