Skip to content

Commit 0c8a2b7

Browse files
committed
refactor: move config class as a new file
1 parent 0cf1205 commit 0c8a2b7

File tree

5 files changed

+425
-373
lines changed

5 files changed

+425
-373
lines changed

__tests__/.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rules:
2+
"@typescript-eslint/no-explicit-any": off
3+
"no-new": off

__tests__/configUpdator.test.ts

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
import Updator from '../libs/configUpdator'
2+
3+
describe('libs/configUpdator.ts', () => {
4+
describe('constructor', () => {
5+
it('should set valid arn', () => {
6+
const c = new Updator('lambdaArn')
7+
expect(c.getLambdaArn()).toEqual('lambdaArn')
8+
})
9+
})
10+
describe('methods', () => {
11+
let c: any
12+
beforeEach(() => {
13+
c = new Updator('edge-lambda-arn')
14+
})
15+
describe('#getLambdaArn()', () => {
16+
it('should return lambda arn', () => {
17+
expect(c.getLambdaArn()).toEqual('edge-lambda-arn')
18+
})
19+
})
20+
describe('#updateEventType()', () => {
21+
it('should return default event type', () => {
22+
expect(c.getTargetEventType()).toEqual('viewer-request')
23+
})
24+
it('should update event type', () => {
25+
c.updateEventType('origin-request')
26+
expect(c.getTargetEventType()).toEqual('origin-request')
27+
})
28+
})
29+
describe('#createUpdateDistributionParam()', () => {
30+
const data = {
31+
Distribution: {
32+
Id: 'distribution id'
33+
},
34+
ETag: 'etag'
35+
}
36+
const config = {
37+
param: true
38+
}
39+
it('should return valid update params', () => {
40+
const params = c.createUpdateDistributionParam(data, config)
41+
expect(params).toEqual({
42+
Id: 'distribution id',
43+
IfMatch: 'etag',
44+
DistributionConfig: {
45+
param: true
46+
}
47+
})
48+
})
49+
})
50+
describe('detatchEdgeFunction', () => {
51+
it('return same param when given no associated edge lambda', () => {
52+
const distributionConfig = {
53+
DefaultCacheBehavior: {
54+
LambdaFunctionAssociations: {
55+
Quantity: 0,
56+
Items: []
57+
}
58+
}
59+
}
60+
const result = c.detatchEdgeFunction(distributionConfig)
61+
expect(
62+
{
63+
DefaultCacheBehavior: {
64+
LambdaFunctionAssociations: {
65+
Quantity: 0,
66+
Items: []
67+
}
68+
}
69+
}
70+
).toEqual(
71+
result
72+
)
73+
})
74+
it('return same param when given no associated edge lambda', () => {
75+
const distributionConfig = {
76+
DefaultCacheBehavior: {
77+
LambdaFunctionAssociations: {
78+
Quantity: 1,
79+
Items: [
80+
{
81+
EventType: 'hoge',
82+
LambdaFunctionARN: 'arn'
83+
}
84+
]
85+
}
86+
}
87+
}
88+
const result = c.detatchEdgeFunction(distributionConfig)
89+
expect(
90+
{
91+
DefaultCacheBehavior: {
92+
LambdaFunctionAssociations: {
93+
Quantity: 1,
94+
Items: [
95+
{
96+
EventType: 'hoge',
97+
LambdaFunctionARN: 'arn'
98+
}
99+
]
100+
}
101+
}
102+
}
103+
).toEqual(
104+
result
105+
)
106+
})
107+
it('return new param that removed edge Lambda when given associated edge lambda', () => {
108+
const distributionConfig = {
109+
DefaultCacheBehavior: {
110+
LambdaFunctionAssociations: {
111+
Quantity: 1,
112+
Items: [
113+
{
114+
EventType: 'viewer-request',
115+
LambdaFunctionARN: 'edge-lambda-arn'
116+
}
117+
]
118+
}
119+
}
120+
}
121+
const result = c.detatchEdgeFunction(distributionConfig)
122+
expect(
123+
{
124+
DefaultCacheBehavior: {
125+
LambdaFunctionAssociations: {
126+
Quantity: 0,
127+
Items: []
128+
}
129+
}
130+
}
131+
).toEqual(
132+
result
133+
)
134+
})
135+
})
136+
it('return new param that removed edge Lambda when given associated edge lambda and more', () => {
137+
const distributionConfig = {
138+
DefaultCacheBehavior: {
139+
LambdaFunctionAssociations: {
140+
Quantity: 2,
141+
Items: [
142+
{
143+
EventType: 'viewer-request',
144+
LambdaFunctionARN: 'edge-lambda-arn'
145+
},
146+
{
147+
EventType: 'hoge',
148+
LambdaFunctionARN: 'arn'
149+
}
150+
]
151+
}
152+
}
153+
}
154+
const result = c.detatchEdgeFunction(distributionConfig)
155+
expect(
156+
{
157+
DefaultCacheBehavior: {
158+
LambdaFunctionAssociations: {
159+
Quantity: 1,
160+
Items: [
161+
{
162+
EventType: 'hoge',
163+
LambdaFunctionARN: 'arn'
164+
}
165+
]
166+
}
167+
}
168+
}
169+
).toEqual(
170+
result
171+
)
172+
})
173+
describe('#attatchEdgeFunction()', () => {
174+
it('should attach edge lambda arn when given no associated lambda@edge', () => {
175+
const distributionConfig = {
176+
DefaultCacheBehavior: {
177+
LambdaFunctionAssociations: {
178+
Quantity: 0,
179+
Items: []
180+
}
181+
}
182+
}
183+
const result = c.attatchEdgeFunction(distributionConfig)
184+
expect(
185+
{
186+
DefaultCacheBehavior: {
187+
LambdaFunctionAssociations: {
188+
Quantity: 1,
189+
Items: [
190+
{
191+
EventType: 'viewer-request',
192+
LambdaFunctionARN: 'edge-lambda-arn'
193+
}
194+
]
195+
}
196+
}
197+
}).toEqual(
198+
result
199+
)
200+
})
201+
it('should attach edge lambda arn when given no associated lambda@edge', () => {
202+
const distributionConfig = {
203+
DefaultCacheBehavior: {
204+
LambdaFunctionAssociations: {
205+
Quantity: 2,
206+
Items: [
207+
{
208+
EventType: 'viewer-request',
209+
LambdaFunctionARN: 'edge-lambda-arn'
210+
},
211+
{
212+
EventType: 'hoge',
213+
LambdaFunctionARN: 'arn'
214+
}
215+
]
216+
}
217+
}
218+
}
219+
const result = c.attatchEdgeFunction(distributionConfig)
220+
expect(
221+
{
222+
DefaultCacheBehavior: {
223+
LambdaFunctionAssociations: {
224+
Quantity: 2,
225+
Items: [
226+
{
227+
EventType: 'hoge',
228+
LambdaFunctionARN: 'arn'
229+
},
230+
{
231+
EventType: 'viewer-request',
232+
LambdaFunctionARN: 'edge-lambda-arn'
233+
}
234+
]
235+
}
236+
}
237+
}).toEqual(
238+
result
239+
)
240+
})
241+
})
242+
})
243+
})

0 commit comments

Comments
 (0)