Skip to content

Commit e2f0dd0

Browse files
add tests setup and basic suites
1 parent 2cddbf2 commit e2f0dd0

File tree

6 files changed

+141
-3
lines changed

6 files changed

+141
-3
lines changed

__mocks__/aws-sdk.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const promisifyMock = (mockFn) => {
2+
const promise = jest.fn()
3+
mockFn.mockImplementation(() => ({
4+
promise
5+
}))
6+
7+
return promise
8+
}
9+
10+
const mockCreateDistribution = jest.fn()
11+
const mockCreateDistributionPromise = promisifyMock(mockCreateDistribution)
12+
13+
const mockGetDistributionConfig = jest.fn()
14+
const mockGetDistributionConfigPromise = promisifyMock(mockGetDistributionConfig)
15+
16+
const mockDeleteDistribution = jest.fn()
17+
const mockDeleteDistributionPromise = promisifyMock(mockDeleteDistribution)
18+
19+
jest.mock('aws-sdk', () => ({
20+
CloudFront: jest.fn(() => ({
21+
createDistribution: mockCreateDistribution,
22+
getDistributionConfig: mockGetDistributionConfig,
23+
deleteDistribution: mockDeleteDistribution
24+
}))
25+
}))
26+
27+
module.exports = {
28+
mockCreateDistribution,
29+
mockGetDistributionConfig,
30+
mockDeleteDistribution,
31+
mockCreateDistributionPromise,
32+
mockGetDistributionConfigPromise,
33+
mockDeleteDistributionPromise
34+
}

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
clearMocks: true
3+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"access": "public"
77
},
88
"scripts": {
9-
"test": "echo \"Error: no test specified\" && exit 1",
9+
"test": "jest tests",
1010
"lint": "eslint . --fix --cache"
1111
},
1212
"author": "Serverless, Inc.",
@@ -22,6 +22,7 @@
2222
"eslint-config-prettier": "^3.6.0",
2323
"eslint-plugin-import": "^2.14.0",
2424
"eslint-plugin-prettier": "^3.0.1",
25+
"jest": "^24.9.0",
2526
"prettier": "^1.15.3"
2627
}
2728
}

tests/s3-origin.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const {
2+
mockCreateDistribution,
3+
mockCreateDistributionPromise,
4+
mockGetDistributionConfigPromise
5+
} = require('aws-sdk')
6+
7+
const CloudFrontComponent = require('../serverless')
8+
9+
describe('S3 origin', () => {
10+
let component
11+
12+
beforeEach(async () => {
13+
component = new CloudFrontComponent()
14+
15+
mockGetDistributionConfigPromise.mockResolvedValueOnce({
16+
ETag: 'etag'
17+
})
18+
mockCreateDistributionPromise.mockResolvedValueOnce({
19+
Distribution: {
20+
Id: 'xyz'
21+
}
22+
})
23+
24+
await component.init()
25+
await component.default({
26+
origins: ['https://mybucket.s3.amazonaws.com']
27+
})
28+
})
29+
30+
afterEach(async () => {
31+
await component.remove()
32+
})
33+
34+
it('creates CloudFront distribution with origin domain name "mybucket.s3.amazonaws.com"', () => {
35+
expect(mockCreateDistribution).toBeCalledWith(
36+
expect.objectContaining({
37+
DistributionConfig: expect.objectContaining({
38+
Origins: expect.objectContaining({
39+
Items: [
40+
expect.objectContaining({
41+
Id: 'mybucket',
42+
DomainName: 'mybucket.s3.amazonaws.com'
43+
})
44+
]
45+
})
46+
})
47+
})
48+
)
49+
})
50+
})

tests/simple-origin.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const {
2+
mockCreateDistribution,
3+
mockCreateDistributionPromise,
4+
mockGetDistributionConfigPromise
5+
} = require('aws-sdk')
6+
7+
const CloudFrontComponent = require('../serverless')
8+
9+
describe('simple origin', () => {
10+
let component
11+
12+
beforeEach(async () => {
13+
component = new CloudFrontComponent()
14+
15+
mockGetDistributionConfigPromise.mockResolvedValueOnce({
16+
ETag: 'etag'
17+
})
18+
mockCreateDistributionPromise.mockResolvedValueOnce({
19+
Distribution: {
20+
Id: 'xyz'
21+
}
22+
})
23+
24+
await component.init()
25+
await component.default({
26+
origins: ['https://exampleorigin.com']
27+
})
28+
})
29+
30+
afterEach(async () => {
31+
await component.remove()
32+
})
33+
34+
it(`creates CloudFront distribution with origin domain name "https://exampleorigin.com"`, () => {
35+
expect(mockCreateDistribution).toBeCalledWith(
36+
expect.objectContaining({
37+
DistributionConfig: expect.objectContaining({
38+
Origins: expect.objectContaining({
39+
Items: [
40+
expect.objectContaining({
41+
Id: 'https://exampleorigin.com',
42+
DomainName: 'https://exampleorigin.com'
43+
})
44+
]
45+
})
46+
})
47+
})
48+
)
49+
})
50+
})

utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const getOriginConfig = (origin) => {
22
const originConfig = {
3-
Id: '',
4-
DomainName: '',
3+
Id: origin,
4+
DomainName: origin,
55
CustomHeaders: {
66
Quantity: 0,
77
Items: []

0 commit comments

Comments
 (0)