-
Notifications
You must be signed in to change notification settings - Fork 123
/
utils.test.ts
132 lines (117 loc) · 3.35 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { parseTags, isUrl, parseParameters } from '../src/utils'
import * as path from 'path'
jest.mock('@actions/core')
describe('Determine a valid url', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('returns true on a valid url', async () => {
const truthy = isUrl(
'https://s3.amazonaws.com/templates/myTemplate.template?versionId=123ab1cdeKdOW5IH4GAcYbEngcpTJTDW'
)
expect(truthy).toBeTruthy()
})
test('returns false on path', async () => {
const falsy = isUrl('./template.json')
expect(falsy).toBeFalsy()
})
})
describe('Parse Tags', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('returns undefined on non valid JSON', async () => {
const json = parseTags('')
expect(json).toBeUndefined()
})
test('returns valid Array on valid JSON', async () => {
const json = parseTags(JSON.stringify([{ Key: 'Test', Value: 'Value' }]))
expect(json).toEqual([{ Key: 'Test', Value: 'Value' }])
})
})
describe('Parse Parameters', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('returns parameters list from string', async () => {
const json = parseParameters('MyParam1=myValue1,MyParam2=myValue2')
expect(json).toEqual([
{
ParameterKey: 'MyParam1',
ParameterValue: 'myValue1'
},
{
ParameterKey: 'MyParam2',
ParameterValue: 'myValue2'
}
])
})
test('returns parameters list from string', async () => {
const json = parseParameters(
'MyParam1=myValue1,MyParam2=myValue2,MyParam2=myValue3'
)
expect(json).toEqual([
{
ParameterKey: 'MyParam1',
ParameterValue: 'myValue1'
},
{
ParameterKey: 'MyParam2',
ParameterValue: 'myValue2,myValue3'
}
])
})
test('returns parameters list with an extra equal', async () => {
const json = parseParameters(
'MyParam1=myValue1,MyParam2=myValue2=myValue3,MyParam2=myValue4'
)
expect(json).toEqual([
{
ParameterKey: 'MyParam1',
ParameterValue: 'myValue1'
},
{
ParameterKey: 'MyParam2',
ParameterValue: 'myValue2=myValue3,myValue4'
}
])
})
test('returns parameters list from multiple lists with quotes', async () => {
const json = parseParameters(
'MyParam1=myValue1,MyParam2="myValue2,myValue3",MyParam2=myValue4'
)
expect(json).toEqual([
{
ParameterKey: 'MyParam1',
ParameterValue: 'myValue1'
},
{
ParameterKey: 'MyParam2',
ParameterValue: 'myValue2,myValue3,myValue4'
}
])
})
test('returns parameters list from file', async () => {
const filename = 'file://' + path.join(__dirname, 'params.test.json')
const json = parseParameters(filename)
expect(json).toEqual([
{
ParameterKey: 'MyParam1',
ParameterValue: 'myValue1'
},
{
ParameterKey: 'MyParam2',
ParameterValue: 'myValue2'
}
])
})
test('throws error if file is not found', async () => {
const filename = 'file://' + path.join(__dirname, 'params.tezt.json')
expect(() => parseParameters(filename)).toThrow()
})
test('throws error if json in file cannot be parsed', async () => {
const filename =
'file://' + path.join(__dirname, 'params-invalid.test.json')
expect(() => parseParameters(filename)).toThrow()
})
})