77const assert = require ( 'yeoman-assert' ) ;
88const yeoman = require ( 'yeoman-environment' ) ;
99const sinon = require ( 'sinon' ) ;
10+ const chalk = require ( 'chalk' ) ;
1011var fs = require ( 'mem-fs-editor' ) . create ( require ( 'mem-fs' ) . create ( ) ) ;
1112
1213module . exports = function ( artiGenerator ) {
@@ -18,17 +19,20 @@ module.exports = function(artiGenerator) {
1819 testSetUpGen ( { args : '2foobar' } ) ;
1920 } , Error ) ;
2021 } ) ;
22+
2123 it ( 'succeeds if no arg is provided' , ( ) => {
2224 assert . doesNotThrow ( ( ) => {
2325 testSetUpGen ( ) ;
2426 } , Error ) ;
2527 } ) ;
28+
2629 it ( 'succeeds if arg is valid' , ( ) => {
2730 assert . doesNotThrow ( ( ) => {
2831 testSetUpGen ( { args : [ 'foobar' ] } ) ;
2932 } , Error ) ;
3033 } ) ;
3134 } ) ;
35+
3236 it ( 'has name argument set up' , ( ) => {
3337 let gen = testSetUpGen ( ) ;
3438 let helpText = gen . help ( ) ;
@@ -37,12 +41,14 @@ module.exports = function(artiGenerator) {
3741 assert ( helpText . match ( / T y p e : S t r i n g / ) ) ;
3842 assert ( helpText . match ( / R e q u i r e d : f a l s e / ) ) ;
3943 } ) ;
44+
4045 it ( 'sets up artifactInfo' , ( ) => {
4146 let gen = testSetUpGen ( { args : [ 'test' ] } ) ;
4247 assert ( gen . artifactInfo ) ;
4348 assert ( gen . artifactInfo . name == 'test' ) ;
4449 } ) ;
4550 } ) ;
51+
4652 describe ( 'usage' , ( ) => {
4753 it ( 'prints lb4' , ( ) => {
4854 let gen = testSetUpGen ( ) ;
@@ -51,22 +57,24 @@ module.exports = function(artiGenerator) {
5157 assert ( ! helpText . match ( / l o o p b a c k 4 : / ) ) ;
5258 } ) ;
5359 } ) ;
60+
5461 describe ( 'checkLoopBackProject' , ( ) => {
5562 testCheckLoopBack (
5663 'throws an error if no package.json is present' ,
5764 undefined ,
58- / u n a b l e t o l o a d p a c k a g e .j s o n /
65+ / N o p a c k a g e .j s o n f o u n d /
5966 ) ;
6067 testCheckLoopBack (
6168 'throws an error if "keywords" key does not exist' ,
6269 { foobar : 'test' } ,
63- / d o e s n o t m a p t o l o o p b a c k /
70+ / N o ` l o o p b a c k ` k e y w o r d f o u n d /
6471 ) ;
6572 testCheckLoopBack (
6673 'throws an error if "keywords" key does not map to an array with "loopback" as a member' ,
6774 { keywords : [ 'foobar' , 'test' ] } ,
68- / d o e s n o t m a p t o l o o p b a c k /
75+ / N o ` l o o p b a c k ` k e y w o r d f o u n d /
6976 ) ;
77+
7078 it ( 'passes if "keywords" maps to "loopback"' , ( ) => {
7179 let gen = testSetUpGen ( ) ;
7280 gen . fs . readJSON = sinon . stub ( fs , 'readJSON' ) ;
@@ -76,18 +84,28 @@ module.exports = function(artiGenerator) {
7684 } , Error ) ;
7785 gen . fs . readJSON . restore ( ) ;
7886 } ) ;
87+
7988 function testCheckLoopBack ( testName , obj , expected ) {
8089 it ( testName , ( ) => {
8190 let gen = testSetUpGen ( ) ;
91+ let logs = [ ] ;
92+ gen . log = function ( ...args ) {
93+ logs = logs . concat ( args ) ;
94+ } ;
8295 gen . fs . readJSON = sinon . stub ( fs , 'readJSON' ) ;
8396 gen . fs . readJSON . returns ( obj ) ;
84- assert . throws ( ( ) => {
85- gen . checkLoopBackProject ( ) ;
86- } , expected ) ;
97+ gen . checkLoopBackProject ( ) ;
98+ assert ( gen . exitGeneration instanceof Error ) ;
99+ assert ( gen . exitGeneration . message . match ( expected ) ) ;
100+ gen . end ( ) ;
101+ assert . deepEqual ( logs , [
102+ chalk . red ( 'Generation is aborted:' , gen . exitGeneration ) ,
103+ ] ) ;
87104 gen . fs . readJSON . restore ( ) ;
88105 } ) ;
89106 }
90107 } ) ;
108+
91109 describe ( 'promptArtifactName' , ( ) => {
92110 it ( 'incorporates user input into artifactInfo' , ( ) => {
93111 let gen = testSetUpGen ( ) ;
@@ -100,6 +118,7 @@ module.exports = function(artiGenerator) {
100118 } ) ;
101119 } ) ;
102120 } ) ;
121+
103122 // returns the generator
104123 function testSetUpGen ( arg ) {
105124 arg = arg || { } ;
0 commit comments