1- const { readFile } = require ( 'fs' ) . promises ;
2- const { relative , resolve } = require ( 'path' ) ;
1+ const { access , readFile } = require ( 'fs' ) . promises ;
2+ const { join , relative } = require ( 'path' ) ;
33const { create } = require ( './lib/cli' ) ;
44const { expand } = require ( './lib/utils' ) ;
55const snapshots = require ( './images/create' ) ;
6+ const shell = require ( 'shelljs' ) ;
67
78describe ( 'preact create' , ( ) => {
8- it ( ` scaffolds the ' default' official template` , async ( ) => {
9+ it ( ' scaffolds the ` default` official template' , async ( ) => {
910 let dir = await create ( 'default' ) ;
1011
1112 let output = await expand ( dir ) . then ( arr => {
@@ -15,29 +16,44 @@ describe('preact create', () => {
1516 expect ( output . sort ( ) ) . toEqual ( snapshots . default ) ;
1617 } ) ;
1718
18- it ( ` should use template.html from the github repo` , async ( ) => {
19+ it ( ' should use template.html from the github repo' , async ( ) => {
1920 let dir = await create ( 'netlify' ) ;
20-
21- const templateFilePath = resolve ( __dirname , dir , 'src' , 'template.html' ) ;
22- const template = await readFile ( templateFilePath , 'utf8' ) ;
23-
21+ const template = await readFile ( join ( dir , 'src/template.html' ) , 'utf8' ) ;
2422 expect ( template . includes ( 'twitter:card' ) ) . toEqual ( true ) ;
2523 } ) ;
2624
27- it ( `should have 'apple-touch-icon' meta tag` , async ( ) => {
28- let dir = await create ( 'simple' ) ;
25+ describe ( 'CLI Options' , ( ) => {
26+ it ( '--name' , async ( ) => {
27+ let dir = await create ( 'simple' , { name : 'renamed' } ) ;
28+ const packageJson = await readFile ( join ( dir , 'package.json' ) , 'utf8' ) ;
2929
30- const templateFilePath = resolve ( __dirname , dir , 'src' , 'template.html' ) ;
31- const template = await readFile ( templateFilePath , 'utf8' ) ;
30+ expect ( JSON . parse ( packageJson ) . name ) . toBe ( 'renamed' ) ;
3231
33- expect ( template . includes ( 'apple-touch-icon' ) ) . toEqual ( true ) ;
34- } ) ;
32+ // @ts -ignore
33+ const mockExit = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => { } ) ;
34+ await create ( 'simple' , { name : '*()@!#!$-Invalid-Name' } ) ;
35+ expect ( mockExit ) . toHaveBeenCalledWith ( 1 ) ;
36+ mockExit . mockRestore ( ) ;
37+ } ) ;
3538
36- it ( 'should fail given an invalid name' , async ( ) => {
37- // @ts -ignore
38- const exit = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => { } ) ;
39- await create ( 'simple' , '*()@!#!$-Invalid-Name' ) ;
39+ it ( '--git' , async ( ) => {
40+ let dir = await create ( 'simple' , { git : true } ) ;
41+ expect ( await access ( join ( dir , '.git' ) ) ) . toBeUndefined ( ) ;
4042
41- expect ( exit ) . toHaveBeenCalledWith ( 1 ) ;
43+ dir = await create ( 'simple' , { git : false } ) ;
44+ await expect ( access ( join ( dir , '.git' ) ) ) . rejects . toThrow (
45+ 'no such file or directory'
46+ ) ;
47+ } ) ;
48+
49+ it ( '--invalid-arg' , ( ) => {
50+ const { code, stderr } = shell . exec (
51+ `node ${ join ( __dirname , '../src/index.js' ) } create --invalid-arg`
52+ ) ;
53+ expect ( stderr ) . toMatch (
54+ "Invalid argument '--invalid-arg' passed to create."
55+ ) ;
56+ expect ( code ) . toBe ( 1 ) ;
57+ } ) ;
4258 } ) ;
4359} ) ;
0 commit comments