1- import { runCommand } from '@oclif/test'
2- import { expect } from 'chai'
1+ import { runCommand } from '@oclif/test'
2+ import { expect } from 'chai'
33import * 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
810describe ( '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