Skip to content

Commit 48dab99

Browse files
Update description flag in update env command with improved tests
1 parent d665597 commit 48dab99

File tree

2 files changed

+90
-27
lines changed

2 files changed

+90
-27
lines changed

src/commands/platform/env/update.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class PlatformEnvUpdate extends ScCommand<typeof PlatformEnvUpdat
1818
'<%= config.bin %> <%= command.id %> --env-id=MyEnvId --new-name=MyNewEnvName --desc="My description to update" --isDefault'
1919
]
2020
static override flags = {
21-
desc: Flags.string({
21+
description: Flags.string({
2222
char: 'd',
2323
description: 'Description of the environment to update.'
2424
}),
@@ -42,7 +42,7 @@ export default class PlatformEnvUpdate extends ScCommand<typeof PlatformEnvUpdat
4242
public async run(): Promise<Environment> {
4343
const { flags } = await this.parse(PlatformEnvUpdate)
4444

45-
const desc = flags.desc ?? ''
45+
const desc = flags.description ?? ''
4646
const envId = flags['env-id'] ?? ''
4747
const name = flags.name ?? ''
4848
const newName = flags['new-name'] ?? ''
@@ -58,7 +58,7 @@ export default class PlatformEnvUpdate extends ScCommand<typeof PlatformEnvUpdat
5858

5959
// API url
6060
let apiUrl: string = `/platform/environments`
61-
let envIdToUdpate: string | undefined = envId
61+
let envIdToUpdate: string | undefined = envId
6262

6363
// If env name provided, get the environment matching provided name and delete. If more than one environment matches, an error will be thrown.
6464
// If env id provided, delete environment with that id
@@ -69,14 +69,14 @@ export default class PlatformEnvUpdate extends ScCommand<typeof PlatformEnvUpdat
6969
if (resp.data.length > 1) {
7070
this.error(`Multiple environments found with: ${name}. Exactly one environment must match the provided name.`)
7171
} else {
72-
envIdToUdpate = resp.data[0]?.id
72+
envIdToUpdate = resp.data[0]?.id
7373
}
7474
}
7575

7676
// API call to update environment by id
77-
apiUrl += `/${envIdToUdpate}`
77+
apiUrl += `/${envIdToUpdate}`
7878
const resp = await conn.put<EnvironmentDetail>(apiUrl, body)
79-
this.log(`Environment with id '${envIdToUdpate}' has been updated successfully.`)
79+
this.log(`Environment with id '${envIdToUpdate}' has been updated successfully.`)
8080
this.print(resp.data)
8181
return resp.data
8282
}
Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import { runCommand } from '@oclif/test'
2-
import { expect } from 'chai'
1+
import {runCommand} from '@oclif/test'
2+
import {expect} from 'chai'
33
import * as sinon from 'sinon'
44

5-
import { ScConnection } from '../../../../src/util/sc-connection'
6-
import { anEnv, setEnvVariables } from '../../../util/test-utils'
5+
import {Environment, EnvironmentApiResponse, EnvironmentDetail} from '../../../../src/types/environment'
6+
import {camelCaseToTitleCase, renderKeyValueTable} from '../../../../src/util/internal'
7+
import {ScConnection} from '../../../../src/util/sc-connection'
8+
import {anEnv, setEnvVariables} from '../../../util/test-utils'
79

810
describe('platform:env:update', () => {
911
setEnvVariables()
1012
let scConnUpdateStub: sinon.SinonStub
1113
let scConnGetStub: sinon.SinonStub
1214
const envName: string = 'MyTestEnvironment'
15+
const envNewName: string = 'MyNewTestEnvironment'
16+
const envDescription: string = 'This is an environment description.'
1317

1418
beforeEach(() => {
1519
scConnUpdateStub = sinon.stub(ScConnection.prototype, 'put')
@@ -22,38 +26,97 @@ describe('platform:env:update', () => {
2226
})
2327

2428
it('runs platform:env:update cmd', async () => {
25-
const { stdout } = await runCommand('platform:env:update')
29+
const {stdout} = await runCommand('platform:env:update')
2630
expect(stdout).to.contain('')
2731
})
2832

29-
it(`runs platform:env:update --name ${envName} --new-name New${envName}`, async () => {
33+
it(`runs platform:env:update --name=${envName} --new-name=${envNewName}`, async () => {
3034
// Arrange
31-
const envs = {
32-
data: [anEnv(envName, true, false)],
35+
const expectBody = {
36+
name: envNewName,
37+
}
38+
const expectEnv: Environment = anEnv(envNewName, false, false) // Environment with new name
39+
const expectResponse: EnvironmentDetail = {
40+
data: expectEnv,
41+
}
42+
const expectEnvApiResponse: EnvironmentApiResponse = {
43+
data: [anEnv(envName, false, false)],
3344
meta: {
3445
pagination: {
3546
count: 1,
3647
nextPage: null,
3748
pageNumber: 1,
3849
pageSize: 10,
39-
totalPages: 1
40-
}
41-
}
50+
totalPages: 1,
51+
},
52+
},
4253
}
4354

44-
const updateSuccessMsg = `Environment with id 'id${envName}' has been updated successfully.`
45-
scConnGetStub.returns(Promise.resolve(envs))
46-
scConnUpdateStub.returns(updateSuccessMsg)
55+
scConnGetStub.returns(Promise.resolve(expectEnvApiResponse))
56+
scConnUpdateStub.returns(Promise.resolve(expectResponse))
57+
58+
const tableRows = [
59+
['Key', 'Value'],
60+
...Object.entries(expectResponse.data).map(([key, value]) => [camelCaseToTitleCase(key), value]),
61+
]
4762

48-
const { stdout } = await runCommand(`platform:env:update --name ${envName} --new-name New${envName}`)
49-
expect(stdout).to.contain(updateSuccessMsg)
63+
// Act
64+
const {stdout} = await runCommand(`platform:env:update --name=${envName} --new-name=${envNewName}`)
65+
66+
// Assert
67+
expect(scConnGetStub.getCall(0).args[0]).to.contain(`?name=${envName}`)
68+
expect(scConnUpdateStub.getCall(0).calledWith(`/platform/environments/id${envName}`, expectBody)).to.be.true
69+
expect(stdout).to.contain(renderKeyValueTable(tableRows))
5070
})
5171

52-
it(`runs platform:env:update --env-id id${envName} --desc "This is a test description."`, async () => {
53-
const updateSuccessMsg = `Environment with id 'id${envName}' has been updated successfully.`
54-
scConnUpdateStub.returns(updateSuccessMsg)
72+
it(`runs platform:env:update --env-id=id${envName} --isDefault`, async () => {
73+
// Arrange
74+
const expectBody = {
75+
isDefault: true,
76+
}
77+
const expectEnv: Environment = anEnv(envName, true, false)
78+
const expectResponse: EnvironmentDetail = {
79+
data: expectEnv,
80+
}
81+
82+
scConnUpdateStub.returns(Promise.resolve(expectResponse))
83+
84+
const tableRows = [
85+
['Key', 'Value'],
86+
...Object.entries(expectResponse.data).map(([key, value]) => [camelCaseToTitleCase(key), value]),
87+
]
88+
89+
// Act
90+
const {stdout} = await runCommand(`platform:env:update --env-id=id${envName} --isDefault`)
91+
92+
// Assert
93+
expect(scConnUpdateStub.getCall(0).calledWith(`/platform/environments/id${envName}`, expectBody)).to.be.true
94+
expect(stdout).to.contain(renderKeyValueTable(tableRows))
95+
})
96+
97+
it(`runs platform:env:update --env-id=id${envName} --description="${envDescription}"`, async () => {
98+
// Arrange
99+
const expectBody = {
100+
description: envDescription,
101+
}
102+
const expectEnv: Environment = anEnv(envName, false, false)
103+
expectEnv.description = envDescription
104+
const expectResponse: EnvironmentDetail = {
105+
data: expectEnv,
106+
}
107+
108+
scConnUpdateStub.returns(Promise.resolve(expectResponse))
109+
110+
const tableRows = [
111+
['Key', 'Value'],
112+
...Object.entries(expectResponse.data).map(([key, value]) => [camelCaseToTitleCase(key), value]),
113+
]
114+
115+
// Act
116+
const {stdout} = await runCommand(`platform:env:update --env-id=id${envName} --description="${envDescription}"`)
55117

56-
const { stdout } = await runCommand(`platform:env:update --env-id id${envName} --desc "This is a test description."`)
57-
expect(stdout).to.contain(updateSuccessMsg)
118+
// Assert
119+
expect(scConnUpdateStub.getCall(0).calledWith(`/platform/environments/id${envName}`, expectBody)).to.be.true
120+
expect(stdout).to.contain(renderKeyValueTable(tableRows))
58121
})
59122
})

0 commit comments

Comments
 (0)