1
1
const path = require ( "path" ) ;
2
- const fs = require ( "fs" ) ;
2
+ const fse = require ( "fs-extra " ) ;
3
3
const { when } = require ( "jest-when" ) ;
4
4
const uploadStaticAssets = require ( "../uploadStaticAssets" ) ;
5
5
const parseNextConfiguration = require ( "../parseNextConfiguration" ) ;
6
6
const parsedNextConfigurationFactory = require ( "../../utils/test/parsedNextConfigurationFactory" ) ;
7
7
const ServerlessPluginBuilder = require ( "../../utils/test/ServerlessPluginBuilder" ) ;
8
8
const uploadDirToS3Factory = require ( "../../utils/s3/upload" ) ;
9
9
10
- jest . mock ( "fs" ) ;
10
+ jest . mock ( "fs-extra " ) ;
11
11
jest . mock ( "../../utils/s3/upload" ) ;
12
12
jest . mock ( "../parseNextConfiguration" ) ;
13
13
@@ -19,7 +19,7 @@ describe("uploadStaticAssets", () => {
19
19
uploadDirToS3Factory . mockReturnValue ( uploadDirToS3 ) ;
20
20
} ) ;
21
21
22
- it ( "should NOT upload build assets when there isn't a bucket available" , ( ) => {
22
+ it ( "does NOT upload build assets when there isn't a bucket available" , ( ) => {
23
23
parseNextConfiguration . mockReturnValueOnce (
24
24
parsedNextConfigurationFactory ( { } , null )
25
25
) ;
@@ -31,7 +31,7 @@ describe("uploadStaticAssets", () => {
31
31
} ) ;
32
32
} ) ;
33
33
34
- it ( "should upload next build assets" , ( ) => {
34
+ it ( "uploads next build assets" , ( ) => {
35
35
const distDir = "build" ;
36
36
parseNextConfiguration . mockReturnValueOnce (
37
37
parsedNextConfigurationFactory ( {
@@ -54,7 +54,7 @@ describe("uploadStaticAssets", () => {
54
54
} ) ;
55
55
} ) ;
56
56
57
- it ( "should upload next build assets using bucketName from plugin config" , ( ) => {
57
+ it ( "uploads next build assets using bucketName from plugin config" , ( ) => {
58
58
const distDir = "build" ;
59
59
parseNextConfiguration . mockReturnValueOnce (
60
60
parsedNextConfigurationFactory ( {
@@ -80,80 +80,59 @@ describe("uploadStaticAssets", () => {
80
80
} ) ;
81
81
} ) ;
82
82
83
- it ( "should upload staticDir" , ( ) => {
84
- const staticDir = "/path/to/assets" ;
83
+ it ( "uploads static directory" , ( ) => {
84
+ const plugin = new ServerlessPluginBuilder ( ) . build ( ) ;
85
+ const staticDir = path . join ( plugin . nextConfigDir , "static" ) ;
85
86
86
- when ( fs . readdirSync )
87
+ when ( fse . pathExists )
87
88
. calledWith ( staticDir )
88
- . mockReturnValueOnce ( [ "foo/bar.js" ] ) ;
89
+ . mockResolvedValueOnce ( true ) ;
89
90
90
91
parseNextConfiguration . mockReturnValueOnce (
91
92
parsedNextConfigurationFactory ( )
92
93
) ;
93
94
94
- const plugin = new ServerlessPluginBuilder ( )
95
- . withPluginConfig ( {
96
- staticDir
97
- } )
98
- . build ( ) ;
99
-
100
95
return uploadStaticAssets . call ( plugin ) . then ( ( ) => {
101
96
expect ( uploadDirToS3 ) . toBeCalledWith ( staticDir , {
102
97
bucket : "my-bucket" ,
103
- truncate : "assets "
98
+ truncate : "static "
104
99
} ) ;
105
100
} ) ;
106
101
} ) ;
107
102
108
- it ( "should upload publicDir" , ( ) => {
109
- const publicDir = "/path/to/assets" ;
103
+ it ( "uploads public directory" , ( ) => {
104
+ const plugin = new ServerlessPluginBuilder ( ) . build ( ) ;
105
+ const publicDir = path . join ( plugin . nextConfigDir , "public" ) ;
110
106
111
- when ( fs . readdirSync )
107
+ when ( fse . pathExists )
112
108
. calledWith ( publicDir )
113
- . mockReturnValueOnce ( [ "foo/bar.js" ] ) ;
109
+ . mockResolvedValueOnce ( true ) ;
114
110
115
111
parseNextConfiguration . mockReturnValueOnce (
116
112
parsedNextConfigurationFactory ( )
117
113
) ;
118
114
119
- const plugin = new ServerlessPluginBuilder ( )
120
- . withPluginConfig ( {
121
- publicDir
122
- } )
123
- . build ( ) ;
124
-
125
115
return uploadStaticAssets . call ( plugin ) . then ( ( ) => {
126
116
expect ( uploadDirToS3 ) . toBeCalledWith ( publicDir , {
127
117
bucket : "my-bucket" ,
128
- truncate : "assets "
118
+ truncate : "public "
129
119
} ) ;
130
120
} ) ;
131
121
} ) ;
132
122
133
- it ( "should not upload build assets" , ( ) => {
134
- const staticDir = "/path/to/assets" ;
135
-
136
- when ( fs . readdirSync )
137
- . calledWith ( staticDir )
138
- . mockReturnValueOnce ( [ "foo/bar.js" ] ) ;
139
-
123
+ it ( "does not upload build assets" , ( ) => {
140
124
parseNextConfiguration . mockReturnValueOnce (
141
125
parsedNextConfigurationFactory ( )
142
126
) ;
143
127
144
128
const plugin = new ServerlessPluginBuilder ( )
145
129
. withPluginConfig ( {
146
- uploadBuildAssets : false ,
147
- staticDir
130
+ uploadBuildAssets : false
148
131
} )
149
132
. build ( ) ;
150
133
151
134
return uploadStaticAssets . call ( plugin ) . then ( ( ) => {
152
- expect ( uploadDirToS3 ) . toBeCalledTimes ( 1 ) ;
153
- expect ( uploadDirToS3 ) . toBeCalledWith ( staticDir , {
154
- bucket : "my-bucket" ,
155
- truncate : "assets"
156
- } ) ;
135
+ expect ( uploadDirToS3 ) . not . toBeCalled ( ) ;
157
136
} ) ;
158
137
} ) ;
159
138
} ) ;
0 commit comments