@@ -73,23 +73,145 @@ describe('base API configuration', () => {
73
73
} ) ;
74
74
} ) ;
75
75
76
- describe ( 'getTemplateFiles ' , ( ) => {
76
+ describe ( 'with a mocked GitHub API ' , ( ) => {
77
77
const gitHubApi = nock ( 'https://api.github.com' ) ;
78
78
79
79
afterEach ( ( ) => {
80
80
gitHubApi . done ( ) ;
81
81
} ) ;
82
82
83
- test ( 'contacts the GitHub API' , async ( ) => {
84
- const { getTemplateFiles } = require ( '../../src/templating/data' ) ;
83
+ describe ( 'getTemplateFiles' , ( ) => {
84
+ test ( 'contacts the GitHub API' , async ( ) => {
85
+ const { getTemplateFiles } = require ( '../../src/templating/data' ) ;
85
86
86
- const basePath = '/repos/twilio-labs/function-templates/contents/blank' ;
87
- const templateContentScope = gitHubApi
88
- . get ( `${ basePath } ?ref=master` )
89
- . reply ( 200 , [ ] ) ;
90
- const result = await getTemplateFiles ( 'blank' ) ;
87
+ const basePath = '/repos/twilio-labs/function-templates/contents/blank' ;
88
+ const templateContentScope = gitHubApi
89
+ . get ( `${ basePath } ?ref=master` )
90
+ . reply ( 200 , [ ] ) ;
91
+ const result = await getTemplateFiles ( 'blank' ) ;
91
92
92
- expect ( result ) . toEqual ( [ ] ) ;
93
- expect ( templateContentScope . isDone ( ) ) . toBeTruthy ( ) ;
93
+ expect ( result ) . toEqual ( [ ] ) ;
94
+ expect ( templateContentScope . isDone ( ) ) . toBeTruthy ( ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ describe ( 'getFiles' , ( ) => {
99
+ const { getFiles } = require ( '../../src/templating/data' ) ;
100
+
101
+ test ( 'gets a template directory from GH and translate to template file info' , async ( ) => {
102
+ const basePath =
103
+ '/repos/twilio-labs/function-templates/contents/blank/functions' ;
104
+ const templateContentScope = gitHubApi
105
+ . get ( `${ basePath } ?ref=master` )
106
+ . reply ( 200 , [
107
+ {
108
+ name : 'blank.js' ,
109
+ path : 'blank/functions/blank.js' ,
110
+ sha : '8ffaf92aea1c5cd224fafa30165462c9eb0214bd' ,
111
+ size : 80 ,
112
+ url :
113
+ 'https://api.github.com/repos/twilio-labs/function-templates/contents/blank/functions/blank.js?ref=master' ,
114
+ html_url :
115
+ 'https://github.com/twilio-labs/function-templates/blob/master/blank/functions/blank.js' ,
116
+ git_url :
117
+ 'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd' ,
118
+ download_url :
119
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/master/blank/functions/blank.js' ,
120
+ type : 'file' ,
121
+ _links : {
122
+ self :
123
+ 'https://api.github.com/repos/twilio-labs/function-templates/contents/blank/functions/blank.js?ref=master' ,
124
+ git :
125
+ 'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd' ,
126
+ html :
127
+ 'https://github.com/twilio-labs/function-templates/blob/master/blank/functions/blank.js' ,
128
+ } ,
129
+ } ,
130
+ ] ) ;
131
+
132
+ const files = await getFiles ( 'blank' , 'functions' ) ;
133
+ expect ( files . length ) . toEqual ( 1 ) ;
134
+ const file = files [ 0 ] ;
135
+ expect ( file . directory ) . toEqual ( '' ) ;
136
+ expect ( file . name ) . toEqual ( 'blank.js' ) ;
137
+ expect ( file . type ) . toEqual ( 'functions' ) ;
138
+ expect ( file . content ) . toEqual (
139
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/master/blank/functions/blank.js'
140
+ ) ;
141
+ } ) ;
142
+
143
+ test ( 'gets a template directory with nested functions from GH and translate to template file info' , async ( ) => {
144
+ const basePath =
145
+ '/repos/twilio-labs/function-templates/contents/nested/functions' ;
146
+ gitHubApi . get ( `${ basePath } ?ref=master` ) . reply ( 200 , [
147
+ {
148
+ name : 'blank.js' ,
149
+ path : 'nested/functions/blank.js' ,
150
+ sha : '8ffaf92aea1c5cd224fafa30165462c9eb0214bd' ,
151
+ size : 80 ,
152
+ url :
153
+ 'https://api.github.com/repos/twilio-labs/function-templates/contents/nested/functions/blank.js?ref=master' ,
154
+ html_url :
155
+ 'https://github.com/twilio-labs/function-templates/blob/master/nested/functions/blank.js' ,
156
+ git_url :
157
+ 'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/8ffaf92aea1c5cd224fafa30165462c9eb0214bd' ,
158
+ download_url :
159
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/master/nested/functions/blank.js' ,
160
+ type : 'file' ,
161
+ } ,
162
+ {
163
+ name : 'admin' ,
164
+ path : 'blank/functions/admin' ,
165
+ sha : '2e5bf79f4c70ad016b93e5563374867b18078d60' ,
166
+ size : 0 ,
167
+ url :
168
+ 'https://api.github.com/repos/twilio-labs/function-templates/contents/nested/functions/admin' ,
169
+ html_url :
170
+ 'https://github.com/twilio-labs/function-templates/tree/nested-directory/nested-directory/functions/admin' ,
171
+ git_url :
172
+ 'https://api.github.com/repos/twilio-labs/function-templates/git/trees/2e5bf79f4c70ad016b93e5563374867b18078d60' ,
173
+ download_url : null ,
174
+ type : 'dir' ,
175
+ } ,
176
+ ] ) ;
177
+ gitHubApi
178
+ . get (
179
+ `/repos/twilio-labs/function-templates/contents/nested/functions/admin`
180
+ )
181
+ . reply ( 200 , [
182
+ {
183
+ name : 'admin.js' ,
184
+ path : 'nested/functions/admin/admin.js' ,
185
+ sha : '83f046ef49a8207b6876393074f638842590bccb' ,
186
+ size : 116 ,
187
+ url :
188
+ 'https://api.github.com/repos/twilio-labs/function-templates/contents/nested-directory/functions/admin/admin.js?ref=nested-directory' ,
189
+ html_url :
190
+ 'https://github.com/twilio-labs/function-templates/blob/nested-directory/nested-directory/functions/admin/admin.js' ,
191
+ git_url :
192
+ 'https://api.github.com/repos/twilio-labs/function-templates/git/blobs/83f046ef49a8207b6876393074f638842590bccb' ,
193
+ download_url :
194
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/nested-directory/nested-directory/functions/admin/admin.js' ,
195
+ type : 'file' ,
196
+ } ,
197
+ ] ) ;
198
+
199
+ const files = await getFiles ( 'nested' , 'functions' ) ;
200
+ expect ( files . length ) . toEqual ( 2 ) ;
201
+ const file = files [ 0 ] ;
202
+ expect ( file . directory ) . toEqual ( '' ) ;
203
+ expect ( file . name ) . toEqual ( 'blank.js' ) ;
204
+ expect ( file . type ) . toEqual ( 'functions' ) ;
205
+ expect ( file . content ) . toEqual (
206
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/master/nested/functions/blank.js'
207
+ ) ;
208
+ const nestedFile = files [ 1 ] ;
209
+ expect ( nestedFile . directory ) . toEqual ( 'admin' ) ;
210
+ expect ( nestedFile . name ) . toEqual ( 'admin.js' ) ;
211
+ expect ( nestedFile . type ) . toEqual ( 'functions' ) ;
212
+ expect ( nestedFile . content ) . toEqual (
213
+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/nested-directory/nested-directory/functions/admin/admin.js'
214
+ ) ;
215
+ } ) ;
94
216
} ) ;
95
217
} ) ;
0 commit comments