11const SerialPort = require ( 'serialport' ) ;
22const MockBinding = require ( '@serialport/binding-mock' ) ;
33const Inquire = require ( '../lib/inquire' ) ;
4+ const inquirer = require ( 'inquirer' ) ;
45
56const inquirer_tests = ( ) => describe ( '1. CLI for Interchange' , ( ) => {
67 beforeAll ( ( ) => {
@@ -86,6 +87,7 @@ const inquirer_tests = () => describe('1. CLI for Interchange', () => {
8687 SerialPort . Binding = MockBinding ;
8788
8889 // set up low level regection for the SP
90+ const backup = SerialPort . Binding . list ;
8991 SerialPort . Binding . list = jest . fn ( ) . mockImplementation ( ( ) => {
9092 return Promise . reject ( new Error ( 'Cannot find ports' ) ) ;
9193 } ) ;
@@ -94,12 +96,39 @@ const inquirer_tests = () => describe('1. CLI for Interchange', () => {
9496 return new Inquire ( ( ) => { } )
9597 . catch ( err => {
9698 expect ( err . toString ( ) ) . toMatch ( / C a n n o t f i n d p o r t s / ) ;
99+ // reset the binding mock.
100+ SerialPort . Binding . list = backup ;
97101 done ( ) ;
98102 } ) ;
99103 } ) ;
100104
101- // TODO: mock the inquirer.prompt call and then in the callback function
102- // test that the firmware is set appropriately etc.
105+ test ( '1.10 .promptQuestions() calls the inquire callback with answers filled' , ( done ) => {
106+ MockBinding . createPort ( '/dev/dummy' , { echo : true , record : true } ) ;
107+ SerialPort . Binding = MockBinding ;
108+
109+ inquirer . prompt = jest . fn ( ) . mockImplementation ( ( ) => {
110+ return Promise . resolve ( {
111+ firmware : 'test_firmware' ,
112+ avr : 'nano' ,
113+ port : '/dev/dummy' ,
114+ firmata : false ,
115+ address : '0x27'
116+ } ) ;
117+ } ) ;
118+
119+ expect . assertions ( 6 ) ;
120+ return new Inquire ( ( fw , opts ) => {
121+ expect ( fw ) . toBe ( 'test_firmware' ) ;
122+ expect ( opts ) . toBeDefined ( ) ;
123+ expect ( opts . board ) . toBe ( 'nano' ) ;
124+ expect ( opts . port ) . toBe ( '/dev/dummy' ) ;
125+ expect ( opts . address ) . toBe ( '0x27' ) ;
126+ expect ( opts . firmata ) . toBe ( false ) ;
127+ done ( ) ;
128+ } ) . then ( ( inquire ) => {
129+ inquire . prompt ( ) ;
130+ } ) ;
131+ } ) ;
103132} ) ;
104133
105134inquirer_tests ( ) ;
0 commit comments