Skip to content

Commit 3f41afe

Browse files
committed
Added Unit Tests for Connect Stack Util Function
1 parent 27776a5 commit 3f41afe

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ fileignoreconfig:
77
checksum: 60a38392c57ddfd834c0b8204a7b766d53e47681e11e7eb1936bf624a09ee70e
88
- filename: src/utils/connect-stack.ts
99
checksum: 05307c5f7af5c30c8f512f522fc67fd9fc22d868933730efbb4e358e0c072f6a
10+
- filename: test/utils/connect-stack.test.ts
11+
checksum: f5b2cea668977cc0ffd5c278207e729e7f1493e70a77a3e273942939cfd4509a
1012
- filename: test/utils/get-management-token.test.ts
1113
checksum: 27bf7650b7264db640f23aa75ce21c4ecfa4f32ddc5caed48952dc12e9c59f41

test/utils/connect-stack.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as contentstackSdk from '@contentstack/management'
2+
import connectStack from '../../src/utils/connect-stack'
3+
import {cli} from 'cli-ux'
4+
import processStack from '../../src/utils/process-stack'
5+
const regexMessages = require('../../messages/index.json').validateRegex
6+
7+
jest.mock('@contentstack/management', () => ({client: jest.fn()}))
8+
jest.mock('../../src/utils/output.ts')
9+
jest.mock('../../src/utils/process-stack.ts')
10+
11+
/* @ts-ignore */
12+
cli = ({
13+
debug: jest.fn(),
14+
error: jest.fn(),
15+
action: {
16+
start: jest.fn(),
17+
stop: jest.fn(),
18+
}})
19+
20+
describe('Get Client from Management SDK, connect with Stack & process Stack', () => {
21+
beforeEach(() => {
22+
jest.restoreAllMocks()
23+
})
24+
25+
test('API Key is Valid', async () => {
26+
const apiKey = 'blt1234'
27+
const authToken = 'blt1234'
28+
const flags = {
29+
contentType: true,
30+
globalField: true,
31+
}
32+
contentstackSdk.client.mockImplementation(() => {
33+
return {
34+
stack: jest.fn().mockImplementation(() => {
35+
return {
36+
fetch: jest.fn().mockResolvedValue(Promise.resolve({stack: {}})),
37+
}
38+
}),
39+
}
40+
})
41+
await connectStack(flags, authToken, apiKey)
42+
expect(cli.action.start).toHaveBeenCalled()
43+
expect(processStack).toHaveBeenCalled()
44+
})
45+
46+
test('API Key is Invalid', async () => {
47+
try {
48+
const apiKey = 'blt1234'
49+
const authToken = 'blt1234'
50+
const flags = {
51+
contentType: true,
52+
globalField: true,
53+
}
54+
contentstackSdk.client.mockImplementation(() => {
55+
return {
56+
stack: jest.fn().mockImplementation(() => {
57+
return {
58+
fetch: jest.fn().mockRejectedValue(Promise.resolve(Error)),
59+
}
60+
}),
61+
}
62+
})
63+
await connectStack(flags, authToken, apiKey)
64+
expect(cli.action.start).toHaveBeenCalled()
65+
} catch (error) {
66+
expect(error.message).toBe(regexMessages.errors.stack.apiKey)
67+
}
68+
})
69+
})

0 commit comments

Comments
 (0)