@@ -4,12 +4,13 @@ const {
44} = require ( '../src/create-twilio-function/install-dependencies' ) ;
55const inquirer = require ( 'inquirer' ) ;
66const ora = require ( 'ora' ) ;
7- const boxen = require ( 'boxen ' ) ;
7+ const nock = require ( 'nock ' ) ;
88const fs = require ( 'fs' ) ;
99const { promisify } = require ( 'util' ) ;
1010const rimraf = promisify ( require ( 'rimraf' ) ) ;
1111const mkdir = promisify ( fs . mkdir ) ;
1212const stat = promisify ( fs . stat ) ;
13+ const readdir = promisify ( fs . readdir ) ;
1314
1415jest . mock ( 'inquirer' ) ;
1516jest . mock ( 'ora' ) ;
@@ -30,6 +31,11 @@ console.log = jest.fn();
3031
3132beforeAll ( async ( ) => {
3233 await rimraf ( './scratch' ) ;
34+ nock . disableNetConnect ( ) ;
35+ } ) ;
36+
37+ afterAll ( ( ) => {
38+ nock . enableNetConnect ( ) ;
3339} ) ;
3440
3541beforeEach ( async ( ) => {
@@ -38,10 +44,15 @@ beforeEach(async () => {
3844
3945afterEach ( async ( ) => {
4046 await rimraf ( './scratch' ) ;
47+ nock . cleanAll ( ) ;
4148} ) ;
4249
4350describe ( 'createTwilioFunction' , ( ) => {
44- beforeEach ( ( ) => jest . clearAllMocks ( ) ) ;
51+ beforeEach ( ( ) => {
52+ nock ( 'https://raw.githubusercontent.com' )
53+ . get ( '/github/gitignore/master/Node.gitignore' )
54+ . reply ( 200 , '*.log\n.env' ) ;
55+ } ) ;
4556
4657 test ( 'it scaffolds a Twilio Function' , async ( ) => {
4758 inquirer . prompt = jest . fn ( ( ) =>
@@ -84,6 +95,96 @@ describe('createTwilioFunction', () => {
8495 expect ( console . log ) . toHaveBeenCalledWith ( 'success message' ) ;
8596 } ) ;
8697
98+ test ( 'it scaffolds a Twilio Function with a template' , async ( ) => {
99+ inquirer . prompt = jest . fn ( ( ) =>
100+ Promise . resolve ( {
101+ accountSid : 'test-sid' ,
102+ authToken : 'test-auth-token'
103+ } )
104+ ) ;
105+
106+ const gitHubAPI = nock ( 'https://api.github.com' ) ;
107+ gitHubAPI
108+ . get ( '/repos/twilio-labs/function-templates/contents/blank?ref=next' )
109+ . reply ( 200 , [
110+ {
111+ name : 'functions'
112+ } ,
113+ {
114+ name : '.env' ,
115+ download_url :
116+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/next/blank/.env'
117+ }
118+ ] ) ;
119+ gitHubAPI
120+ . get (
121+ '/repos/twilio-labs/function-templates/contents/blank/functions?ref=next'
122+ )
123+ . reply ( 200 , [
124+ {
125+ name : 'blank.js' ,
126+ download_url :
127+ 'https://raw.githubusercontent.com/twilio-labs/function-templates/next/blank/functions/blank.js'
128+ }
129+ ] ) ;
130+ const gitHubRaw = nock ( 'https://raw.githubusercontent.com' ) ;
131+ gitHubRaw
132+ . get ( '/twilio-labs/function-templates/next/blank/functions/blank.js' )
133+ . reply (
134+ 200 ,
135+ `exports.handler = function(context, event, callback) {
136+ callback(null, {});
137+ };`
138+ ) ;
139+ gitHubRaw
140+ . get ( '/github/gitignore/master/Node.gitignore' )
141+ . reply ( 200 , 'node_modules/' ) ;
142+ gitHubRaw
143+ . get ( '/twilio-labs/function-templates/next/blank/.env' )
144+ . reply ( 200 , '' ) ;
145+
146+ const name = 'test-function' ;
147+ await createTwilioFunction ( {
148+ name,
149+ path : './scratch' ,
150+ template : 'blank'
151+ } ) ;
152+
153+ const dir = await stat ( `./scratch/${ name } ` ) ;
154+ expect ( dir . isDirectory ( ) ) ;
155+ const env = await stat ( `./scratch/${ name } /.env` ) ;
156+ expect ( env . isFile ( ) ) ;
157+ const nvmrc = await stat ( `./scratch/${ name } /.nvmrc` ) ;
158+ expect ( nvmrc . isFile ( ) ) ;
159+
160+ const packageJSON = await stat ( `./scratch/${ name } /package.json` ) ;
161+ expect ( packageJSON . isFile ( ) ) ;
162+
163+ const gitignore = await stat ( `./scratch/${ name } /.gitignore` ) ;
164+ expect ( gitignore . isFile ( ) ) ;
165+
166+ const functions = await stat ( `./scratch/${ name } /functions` ) ;
167+ expect ( functions . isDirectory ( ) ) ;
168+
169+ const assets = await stat ( `./scratch/${ name } /assets` ) ;
170+ expect ( assets . isDirectory ( ) ) ;
171+
172+ const exampleFiles = await readdir ( `./scratch/${ name } /functions` ) ;
173+ expect ( exampleFiles ) . toEqual (
174+ expect . not . arrayContaining ( [ 'hello-world.js' ] )
175+ ) ;
176+
177+ const templateFunction = await stat ( `./scratch/${ name } /functions/blank.js` ) ;
178+ expect ( templateFunction . isFile ( ) ) ;
179+
180+ const exampleAssets = await readdir ( `./scratch/${ name } /assets` ) ;
181+ expect ( exampleAssets ) . toEqual ( expect . not . arrayContaining ( [ 'index.html' ] ) ) ;
182+
183+ expect ( installDependencies ) . toHaveBeenCalledWith ( `./scratch/${ name } ` ) ;
184+
185+ expect ( console . log ) . toHaveBeenCalledWith ( 'success message' ) ;
186+ } ) ;
187+
87188 it ( "doesn't scaffold if the target folder name already exists" , async ( ) => {
88189 const name = 'test-function' ;
89190 await mkdir ( './scratch/test-function' ) ;
0 commit comments