@@ -2,13 +2,15 @@ const inquirer = require('inquirer');
22const interactiveMode = require ( '../src/interactive-mode' ) ;
33
44describe ( 'interactive-mode' , ( ) => {
5+ let createdInquirerPromptStub ;
56 let inquirerCreatePromptModuleStub ;
67 let inquirerPromptStub ;
78 let values ;
89
910 beforeAll ( ( ) => {
10- inquirerPromptStub = jest . fn ( ) ;
11- inquirerCreatePromptModuleStub = jest . spyOn ( inquirer , 'createPromptModule' ) . mockReturnValue ( inquirerPromptStub ) ;
11+ createdInquirerPromptStub = jest . fn ( ) ;
12+ inquirerCreatePromptModuleStub = jest . spyOn ( inquirer , 'createPromptModule' ) . mockReturnValue ( createdInquirerPromptStub ) ;
13+ inquirerPromptStub = jest . spyOn ( inquirer , 'prompt' ) . mockResolvedValue ( { } ) ;
1214 } ) ;
1315
1416 describe ( 'with no values' , ( ) => {
@@ -22,7 +24,7 @@ describe('interactive-mode', () => {
2224 } ) ;
2325
2426 test ( 'should call prompt' , ( ) => {
25- expect ( inquirerPromptStub ) . toHaveBeenCalled ( ) ;
27+ expect ( createdInquirerPromptStub ) . toHaveBeenCalled ( ) ;
2628 } ) ;
2729 } ) ;
2830
@@ -48,11 +50,59 @@ describe('interactive-mode', () => {
4850 } ) ;
4951
5052 test ( 'should call prompt' , ( ) => {
53+ expect ( createdInquirerPromptStub ) . toHaveBeenCalled ( ) ;
54+ } ) ;
55+
56+ test ( 'should properly transform the values to inquirer values' , ( ) => {
57+ const args = createdInquirerPromptStub . mock . calls [ 1 ] [ 0 ] ;
58+ expect ( args . length ) . toEqual ( Object . keys ( values ) . length ) ;
59+
60+ args . forEach ( ( question ) => {
61+ const inputValues = values [ question . name ] ;
62+ expect ( inputValues ) . toBeTruthy ( ) ;
63+ expect ( question . type ) . toBe ( inputValues . type ) ;
64+ expect ( question . message ) . toBe ( inputValues . describe ) ;
65+ expect ( question . default ) . toBe ( inputValues . default ) ;
66+ expect ( question . choices ) . toBe ( inputValues . choices ) ;
67+ } ) ;
68+ } ) ;
69+ } ) ;
70+
71+ describe ( 'with support of inquirer plugins' , ( ) => {
72+ beforeAll ( ( ) => {
73+ const inquirerOptions = {
74+ allowInquirerPlugins : true ,
75+ } ;
76+
77+ values = {
78+ title : {
79+ type : 'input' ,
80+ describe : 'Message to display' ,
81+ default : 'default value' ,
82+ } ,
83+ message : {
84+ type : 'list' ,
85+ describe : 'Welcome message' ,
86+ choices : [ 'hi' , 'hello' , 'hola!' ] ,
87+ }
88+ } ;
89+ interactiveMode ( values , inquirerOptions ) ;
90+ } ) ;
91+
92+ test ( 'should not have called createPromptModule' , ( ) => {
93+ expect ( inquirerCreatePromptModuleStub ) . not . toHaveBeenCalled ( ) ;
94+ } ) ;
95+
96+ test ( 'should not have called prompt generated by createPromptModule' , ( ) => {
97+ expect ( createdInquirerPromptStub ) . not . toHaveBeenCalled ( ) ;
98+ } ) ;
99+
100+ test ( 'should call the standard inquirer.prompt() method' , ( ) => {
51101 expect ( inquirerPromptStub ) . toHaveBeenCalled ( ) ;
52102 } ) ;
53103
54104 test ( 'should properly transform the values to inquirer values' , ( ) => {
55- const args = inquirerPromptStub . mock . calls [ 1 ] [ 0 ] ;
105+ const args = inquirerPromptStub . mock . calls [ 0 ] [ 0 ] ;
56106 expect ( args . length ) . toEqual ( Object . keys ( values ) . length ) ;
57107
58108 args . forEach ( ( question ) => {
0 commit comments