File tree Expand file tree Collapse file tree 6 files changed +141
-3
lines changed
Expand file tree Collapse file tree 6 files changed +141
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ module . exports = {
2+ clearMocks : true
3+ }
Original file line number Diff line number Diff line change 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." ,
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}
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 11const getOriginConfig = ( origin ) => {
22 const originConfig = {
3- Id : '' ,
4- DomainName : '' ,
3+ Id : origin ,
4+ DomainName : origin ,
55 CustomHeaders : {
66 Quantity : 0 ,
77 Items : [ ]
You can’t perform that action at this time.
0 commit comments