@@ -6,8 +6,14 @@ import { beforeEach, afterEach, describe, expect, test, vi, afterAll } from 'vit
66import BaseCommand from '../../../../src/commands/base-command.ts'
77import { createSitesFromTemplateCommand } from '../../../../src/commands/sites/sites.ts'
88import { deployedSiteExists , fetchTemplates , getTemplateName } from '../../../../src/utils/sites/create-template.ts'
9- import { getTemplatesFromGitHub , validateTemplate , createRepo } from '../../../../src/utils/sites/utils.ts'
9+ import {
10+ getTemplatesFromGitHub ,
11+ validateTemplate ,
12+ createRepo ,
13+ callLinkSite ,
14+ } from '../../../../src/utils/sites/utils.ts'
1015import { getEnvironmentVariables , withMockApi } from '../../utils/mock-api.js'
16+ import { chalk } from '../../../../src/utils/command-helpers.ts'
1117
1218vi . mock ( '../../../../src/utils/init/config-github.ts' )
1319vi . mock ( '../../../../src/utils/sites/utils.ts' )
@@ -49,13 +55,16 @@ describe('sites:create-template', () => {
4955 vi
5056 . fn ( )
5157 . mockImplementationOnce ( ( ) => Promise . resolve ( { accountSlug : 'test-account' } ) )
52- . mockImplementationOnce ( ( ) => Promise . resolve ( { name : 'test-name' } ) ) ,
58+ . mockImplementationOnce ( ( ) => Promise . resolve ( { name : 'test-name' } ) )
59+ . mockImplementationOnce ( ( ) => Promise . resolve ( { cloneConfirm : true } ) )
60+ . mockImplementationOnce ( ( ) => Promise . resolve ( { linkConfirm : true } ) ) ,
5361 {
5462 prompts : inquirer . prompt ?. prompts || { } ,
5563 registerPrompt : inquirer . prompt ?. registerPrompt || vi . fn ( ) ,
5664 restoreDefaultPrompts : inquirer . prompt ?. restoreDefaultPrompts || vi . fn ( ) ,
5765 } ,
5866 )
67+
5968 vi . mocked ( fetchTemplates ) . mockResolvedValue ( [
6069 {
6170 name : 'mockTemplateName' ,
@@ -82,6 +91,7 @@ describe('sites:create-template', () => {
8291 full_name : 'mockName' ,
8392 private : true ,
8493 default_branch : 'mockBranch' ,
94+ name : 'repoName' ,
8595 } )
8696 } )
8797
@@ -145,4 +155,95 @@ describe('sites:create-template', () => {
145155 } )
146156 expect ( stdoutwriteSpy ) . toHaveBeenCalledWith ( 'A site with that name already exists on your account\n' )
147157 } )
158+
159+ test ( 'it should automatically link to the site when the user clones the template repo' , async ( t ) => {
160+ const mockSuccessfulLinkOutput = `
161+ Directory Linked
162+
163+ Admin url: https://app.netlify.com/sites/site-name
164+ Site url: https://site-name.netlify.app
165+
166+ You can now run other \`netlify\` cli commands in this directory
167+ `
168+ vi . mocked ( callLinkSite ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockSuccessfulLinkOutput ) )
169+
170+ const autoLinkRoutes = [
171+ {
172+ path : 'accounts' ,
173+ response : [ { slug : 'test-account' } ] ,
174+ } ,
175+ {
176+ path : 'sites' ,
177+ response : [ { name : 'test-name-unique' } ] ,
178+ } ,
179+ {
180+ path : 'test-account/sites' ,
181+ response : siteInfo ,
182+ method : 'post' ,
183+ } ,
184+ ]
185+
186+ const stdoutwriteSpy = vi . spyOn ( process . stdout , 'write' )
187+ await withMockApi ( autoLinkRoutes , async ( { apiUrl } ) => {
188+ Object . assign ( process . env , getEnvironmentVariables ( { apiUrl } ) )
189+
190+ const program = new BaseCommand ( 'netlify' )
191+
192+ vi . mocked ( deployedSiteExists ) . mockResolvedValue ( false )
193+
194+ createSitesFromTemplateCommand ( program )
195+
196+ await program . parseAsync ( [ '' , '' , 'sites:create-template' ] )
197+ } )
198+
199+ expect ( stdoutwriteSpy ) . toHaveBeenCalledWith (
200+ `\nDirectory ${ chalk . cyanBright ( 'repoName' ) } linked to site ${ chalk . cyanBright (
201+ 'https://site-name.netlify.app' ,
202+ ) } \n\n`,
203+ )
204+ } )
205+
206+ test ( 'it should output instructions if a site is already linked' , async ( t ) => {
207+ const mockUnsuccessfulLinkOutput = `
208+ Site already linked to \"site-name\"
209+ Admin url: https://app.netlify.com/sites/site-name
210+
211+ To unlink this site, run: netlify unlink
212+ `
213+
214+ vi . mocked ( callLinkSite ) . mockImplementationOnce ( ( ) => Promise . resolve ( mockUnsuccessfulLinkOutput ) )
215+
216+ const autoLinkRoutes = [
217+ {
218+ path : 'accounts' ,
219+ response : [ { slug : 'test-account' } ] ,
220+ } ,
221+ {
222+ path : 'sites' ,
223+ response : [ { name : 'test-name-unique' } ] ,
224+ } ,
225+ {
226+ path : 'test-account/sites' ,
227+ response : siteInfo ,
228+ method : 'post' ,
229+ } ,
230+ ]
231+
232+ const stdoutwriteSpy = vi . spyOn ( process . stdout , 'write' )
233+ await withMockApi ( autoLinkRoutes , async ( { apiUrl } ) => {
234+ Object . assign ( process . env , getEnvironmentVariables ( { apiUrl } ) )
235+
236+ const program = new BaseCommand ( 'netlify' )
237+
238+ vi . mocked ( deployedSiteExists ) . mockResolvedValue ( false )
239+
240+ createSitesFromTemplateCommand ( program )
241+
242+ await program . parseAsync ( [ '' , '' , 'sites:create-template' ] )
243+ } )
244+
245+ expect ( stdoutwriteSpy ) . toHaveBeenCalledWith (
246+ `\nThis directory appears to be linked to ${ chalk . cyanBright ( `"site-name"` ) } \n` ,
247+ )
248+ } )
148249} )
0 commit comments