Skip to content

Commit 9eb481b

Browse files
committed
tests: Further tests for the inquirer process to ensure fw is sent back okay. Helps #21
1 parent 72f5ea4 commit 9eb481b

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

test/inquire.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const SerialPort = require('serialport');
22
const MockBinding = require('@serialport/binding-mock');
33
const Inquire = require('../lib/inquire');
4+
const inquirer = require('inquirer');
45

56
const 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(/Cannot find ports/);
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

105134
inquirer_tests();

0 commit comments

Comments
 (0)