Skip to content

Commit ff41c45

Browse files
Improvements to unit test for Environment commands
1 parent cf51f52 commit ff41c45

File tree

6 files changed

+40
-101
lines changed

6 files changed

+40
-101
lines changed

test/commands/platform/env/create.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { runCommand } from '@oclif/test'
22
import { expect } from 'chai'
33
import * as sinon from 'sinon'
44

5-
import { ScConnection } from '../../../../src/util/sc-connection.js'
5+
import { ScConnection } from '../../../../src/util/sc-connection'
6+
import { setEnvVariables } from '../../../util/test-utils'
67

78
describe('platform:env:create', () => {
8-
process.env.SC_ACCESS_TOKEN = 'TEST'
9+
setEnvVariables()
910
let scConnStub: sinon.SinonStub
1011
const envName: string = 'MyTestEnvironment'
1112

test/commands/platform/env/delete.test.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,11 @@ import { runCommand } from '@oclif/test'
22
import { expect } from 'chai'
33
import * as sinon from 'sinon'
44

5-
import { ScConnection } from '../../../../src/util/sc-connection.js'
6-
7-
function anEnv(name: string, isDefault: boolean, isProd: boolean) {
8-
return {
9-
bgColor: '#DA162D',
10-
createdBy: 'someuser',
11-
createdTime: '2024-09-05T19:54:42.766',
12-
description: `This is a description for the the environment ${name}`,
13-
fgColor: '#FFFFFF',
14-
icon: 'ROCKET_LAUNCH',
15-
id: `id${name}`,
16-
isDefault,
17-
isProduction: isProd,
18-
name,
19-
updatedBy: 'someuser',
20-
updatedTime: '2024-09-05T19:54:42.766',
21-
}
22-
}
5+
import { ScConnection } from '../../../../src/util/sc-connection'
6+
import { anEnv, setEnvVariables } from '../../../util/test-utils'
237

248
describe('platform:env:delete', () => {
25-
process.env.SC_ACCESS_TOKEN = 'TEST'
9+
setEnvVariables()
2610
let scConnDeleteStub: sinon.SinonStub
2711
let scConnGetStub: sinon.SinonStub
2812
const envName: string = 'MyTestEnvironment'
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
import { runCommand } from '@oclif/test'
22
import { expect } from 'chai'
33
import * as sinon from 'sinon'
4-
import { table } from 'table'
54

6-
import { camelCaseToTitleCase } from '../../../../src/util/internal.js'
7-
import { ScConnection } from '../../../../src/util/sc-connection.js'
8-
9-
function anEnv(name: string, isDefault: boolean, isProd: boolean) {
10-
return {
11-
bgColor: '#DA162D',
12-
createdBy: 'someuser',
13-
createdTime: '2024-09-05T19:54:42.766',
14-
description: `This is a description for the the environment ${name}`,
15-
fgColor: '#FFFFFF',
16-
icon: 'ROCKET_LAUNCH',
17-
id: `id${name}`,
18-
isDefault,
19-
isProduction: isProd,
20-
name,
21-
updatedBy: 'someuser',
22-
updatedTime: '2024-09-05T19:54:42.766',
23-
}
24-
}
5+
import { camelCaseToTitleCase, renderKeyValueTable } from '../../../../src/util/internal'
6+
import { ScConnection } from '../../../../src/util/sc-connection'
7+
import { anEnv, setEnvVariables } from '../../../util/test-utils'
258

269
describe('platform:env:display', () => {
27-
process.env.SC_ACCESS_TOKEN = 'TEST'
10+
setEnvVariables()
2811
let scConnStub: sinon.SinonStub
2912

3013
beforeEach(() => {
@@ -62,16 +45,7 @@ describe('platform:env:display', () => {
6245
...Object.entries(envs.data[0]).map(([key, value]) => [camelCaseToTitleCase(key), value]),
6346
]
6447

65-
const config = {
66-
columns: {
67-
1: { width: 50, wrapWord: true }
68-
},
69-
drawHorizontalLine(lineIndex: number, rowCount: number) {
70-
return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount;
71-
}
72-
}
73-
7448
const { stdout } = await runCommand(`platform:env:display --name=${envName}`)
75-
expect(stdout).to.contain(table(tableRows, config))
49+
expect(stdout).to.contain(renderKeyValueTable(tableRows, { 1: { width: 50, wrapWord: true } }))
7650
})
7751
})

test/commands/platform/env/list.test.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
import { runCommand } from '@oclif/test'
22
import { expect } from 'chai'
33
import * as sinon from 'sinon'
4-
import { table } from 'table'
54

65
import { Environment } from '../../../../src/types/environment'
6+
import { renderTable } from '../../../../src/util/internal'
77
import { ScConnection } from '../../../../src/util/sc-connection'
8-
9-
function anEnv(name: string, isDefault: boolean, isProd: boolean) {
10-
return {
11-
createdBy: 'someuser',
12-
createdTime: '2024-09-05T19:54:42.766',
13-
description: `This is a description for the environment ${name}`,
14-
id: `id${name}`,
15-
isDefault,
16-
isProduction: isProd,
17-
name,
18-
updateddBy: 'someuser',
19-
updatedTime: '2024-09-05T19:54:42.766',
20-
}
21-
}
8+
import { anEnv, setEnvVariables } from '../../../util/test-utils'
229

2310
describe('platform:env:list', () => {
24-
process.env.SC_ACCESS_TOKEN = 'TEST'
11+
setEnvVariables()
2512
let scConnStub: sinon.SinonStub
2613

2714
beforeEach(() => {
@@ -59,14 +46,9 @@ describe('platform:env:list', () => {
5946
item.description]),
6047
]
6148

62-
const config = {
63-
columns: {
64-
4: { width: 50, wrapWord: true }
65-
}
66-
}
6749
// Run command
6850
const { stdout } = await runCommand('platform:env:list')
69-
expect(stdout).to.contain(table(envArray, config))
51+
expect(stdout).to.contain(renderTable(envArray, { 4: { width: 50, wrapWord: true } }))
7052
})
7153

7254
it('runs platform:env:list --pageSize=5 --pageNumber=1', async () => {
@@ -96,14 +78,8 @@ describe('platform:env:list', () => {
9678
item.description]),
9779
]
9880

99-
const config = {
100-
columns: {
101-
4: { width: 50, wrapWord: true }
102-
}
103-
}
104-
10581
// Run command
10682
const { stdout } = await runCommand('platform:env:list --pageSize=5 --pageNumber=1')
107-
expect(stdout).to.contain(table(envArray, config))
83+
expect(stdout).to.contain(renderTable(envArray, { 4: { width: 50, wrapWord: true } }))
10884
})
10985
})

test/commands/platform/env/update.test.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,11 @@ import { runCommand } from '@oclif/test'
22
import { expect } from 'chai'
33
import * as sinon from 'sinon'
44

5-
import { ScConnection } from '../../../../src/util/sc-connection.js'
6-
7-
function anEnv(name: string, isDefault: boolean, isProd: boolean) {
8-
return {
9-
bgColor: '#DA162D',
10-
createdBy: 'someuser',
11-
createdTime: '2024-09-05T19:54:42.766',
12-
description: `This is a description for the the environment ${name}`,
13-
fgColor: '#FFFFFF',
14-
icon: 'ROCKET_LAUNCH',
15-
id: `id${name}`,
16-
isDefault,
17-
isProduction: isProd,
18-
name,
19-
updatedBy: 'someuser',
20-
updatedTime: '2024-09-05T19:54:42.766',
21-
}
22-
}
5+
import { ScConnection } from '../../../../src/util/sc-connection'
6+
import { anEnv, setEnvVariables } from '../../../util/test-utils'
237

248
describe('platform:env:update', () => {
25-
process.env.SC_ACCESS_TOKEN = 'TEST'
9+
setEnvVariables()
2610
let scConnUpdateStub: sinon.SinonStub
2711
let scConnGetStub: sinon.SinonStub
2812
const envName: string = 'MyTestEnvironment'
@@ -56,7 +40,7 @@ describe('platform:env:update', () => {
5640
}
5741
}
5842
}
59-
43+
6044
const updateSuccessMsg = `Environment with id 'id${envName}' has been updated successfully.`
6145
scConnGetStub.returns(Promise.resolve(envs))
6246
scConnUpdateStub.returns(updateSuccessMsg)

test/util/test-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function setEnvVariables(): void {
2+
process.env.SC_ACCESS_TOKEN = 'TEST'
3+
}
4+
5+
export function anEnv(name: string, isDefault: boolean, isProd: boolean) {
6+
return {
7+
bgColor: '#DA162D',
8+
createdBy: 'someuser',
9+
createdTime: '2024-09-05T19:54:42.766',
10+
description: `This is a description for the the environment ${name}`,
11+
fgColor: '#FFFFFF',
12+
icon: 'ROCKET_LAUNCH',
13+
id: `id${name}`,
14+
isDefault,
15+
isProduction: isProd,
16+
name,
17+
updatedBy: 'someuser',
18+
updatedTime: '2024-09-05T19:54:42.766',
19+
}
20+
}

0 commit comments

Comments
 (0)