Skip to content

Commit 9698a0a

Browse files
committed
Added Unit Tests for Process Stack Util Function
1 parent 3a823c1 commit 9698a0a

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

test/utils/process-stack.test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import {cli} from 'cli-ux'
2+
import processStack from '../../src/utils/process-stack'
3+
import generateOutput from '../../src/utils/output'
4+
const validDocument = require('../data/validDocument.json')
5+
const regexMessages = require('../../messages/index.json').validateRegex
6+
7+
jest.mock('../../src/utils/output.ts')
8+
9+
/* @ts-ignore */
10+
cli = ({
11+
debug: jest.fn(),
12+
error: jest.fn(),
13+
action: {
14+
start: jest.fn(),
15+
stop: jest.fn(),
16+
}})
17+
18+
describe('Process Stack', () => {
19+
beforeEach(() => {
20+
jest.restoreAllMocks()
21+
})
22+
23+
test('Process Stack with Content Type & Global Field selected & valid Data', async () => {
24+
const stack = {
25+
name: 'stack',
26+
contentType: jest.fn().mockImplementation(() => {
27+
return {
28+
query: jest.fn().mockImplementation(() => {
29+
return {
30+
find: jest.fn().mockResolvedValue(Promise.resolve({items: [validDocument]})),
31+
}
32+
}),
33+
}
34+
}),
35+
globalField: jest.fn().mockImplementation(() => {
36+
return {
37+
query: jest.fn().mockImplementation(() => {
38+
return {
39+
find: jest.fn().mockResolvedValue(Promise.resolve({items: [validDocument]})),
40+
}
41+
}),
42+
}
43+
}),
44+
}
45+
const startTime = Date.now()
46+
await processStack({contentType: true}, stack, startTime)
47+
await processStack({globalField: true}, stack, startTime)
48+
expect(cli.action.stop).toHaveBeenCalled()
49+
expect(cli.action.start).toHaveBeenCalled()
50+
expect(cli.action.stop).toHaveBeenCalled()
51+
expect(generateOutput).toHaveBeenCalled()
52+
})
53+
54+
test('Process Stack with Content Type selected & invalid Content Type Data', async () => {
55+
const contentTypeData = {
56+
title: 'Regex Fields',
57+
uid: 'regex_fields',
58+
}
59+
const stack = {
60+
name: 'stack',
61+
contentType: jest.fn().mockImplementation(() => {
62+
return {
63+
query: jest.fn().mockImplementation(() => {
64+
return {
65+
find: jest.fn().mockResolvedValue(Promise.resolve({items: [contentTypeData]})),
66+
}
67+
}),
68+
}
69+
}),
70+
}
71+
try {
72+
const startTime = Date.now()
73+
await processStack({contentType: true}, stack, startTime)
74+
expect(cli.action.stop).toHaveBeenCalled()
75+
expect(cli.action.start).toHaveBeenCalled()
76+
expect(cli.action.stop).toHaveBeenCalled()
77+
expect(generateOutput).not.toHaveBeenCalled()
78+
} catch (error) {
79+
expect(error.message).toBe(regexMessages.errors.contentTypes)
80+
}
81+
})
82+
83+
test('Process Stack with Global Field selected & Invalid Global Field Data', async () => {
84+
const globalFieldData = {
85+
title: 'Regex Fields',
86+
uid: 'regex_fields',
87+
}
88+
const stack = {
89+
name: 'stack',
90+
globalField: jest.fn().mockImplementation(() => {
91+
return {
92+
query: jest.fn().mockImplementation(() => {
93+
return {
94+
find: jest.fn().mockResolvedValue(Promise.resolve({items: [globalFieldData]})),
95+
}
96+
}),
97+
}
98+
}),
99+
}
100+
try {
101+
const startTime = Date.now()
102+
await processStack({globalField: true}, stack, startTime)
103+
expect(cli.action.stop).toHaveBeenCalled()
104+
expect(cli.action.start).toHaveBeenCalled()
105+
expect(cli.action.stop).toHaveBeenCalled()
106+
expect(generateOutput).not.toHaveBeenCalled()
107+
} catch (error) {
108+
expect(error.message).toBe(regexMessages.errors.globalFields)
109+
}
110+
})
111+
})

0 commit comments

Comments
 (0)