Skip to content

Commit

Permalink
test: improve readline/emitKeypressEvents.js coverage
Browse files Browse the repository at this point in the history
PR-URL: #42908
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
y1d7ng authored and juanarbol committed May 31, 2022
1 parent 115a04f commit 4c4cd31
Showing 1 changed file with 59 additions and 15 deletions.
74 changes: 59 additions & 15 deletions test/parallel/test-readline-emit-keypress-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,66 @@ require('../common');
const assert = require('assert');
const readline = require('readline');
const PassThrough = require('stream').PassThrough;
const stream = new PassThrough();
const sequence = [];
const keys = [];

readline.emitKeypressEvents(stream);

stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});

stream.write('foo');

assert.deepStrictEqual(sequence, ['f', 'o', 'o']);
assert.deepStrictEqual(keys, [
const expectedSequence = ['f', 'o', 'o'];
const expectedKeys = [
{ sequence: 'f', name: 'f', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
]);
];

{
const stream = new PassThrough();
const sequence = [];
const keys = [];

readline.emitKeypressEvents(stream);
stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}

{
const stream = new PassThrough();
const sequence = [];
const keys = [];

stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});
readline.emitKeypressEvents(stream);
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}

{
const stream = new PassThrough();
const sequence = [];
const keys = [];
const keypressListener = (s, k) => {
sequence.push(s);
keys.push(k);
};

stream.on('keypress', keypressListener);
readline.emitKeypressEvents(stream);
stream.removeListener('keypress', keypressListener);
stream.write('foo');

assert.deepStrictEqual(sequence, []);
assert.deepStrictEqual(keys, []);

stream.on('keypress', keypressListener);
stream.write('foo');

assert.deepStrictEqual(sequence, expectedSequence);
assert.deepStrictEqual(keys, expectedKeys);
}

0 comments on commit 4c4cd31

Please sign in to comment.