• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// emitKeypressEvents is thoroughly tested in test-readline-keys.js.
3// However, that test calls it implicitly. This is just a quick sanity check
4// to verify that it works when called explicitly.
5
6require('../common');
7const assert = require('assert');
8const readline = require('readline');
9const PassThrough = require('stream').PassThrough;
10
11const expectedSequence = ['f', 'o', 'o'];
12const expectedKeys = [
13  { sequence: 'f', name: 'f', ctrl: false, meta: false, shift: false },
14  { sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
15  { sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
16];
17
18{
19  const stream = new PassThrough();
20  const sequence = [];
21  const keys = [];
22
23  readline.emitKeypressEvents(stream);
24  stream.on('keypress', (s, k) => {
25    sequence.push(s);
26    keys.push(k);
27  });
28  stream.write('foo');
29
30  assert.deepStrictEqual(sequence, expectedSequence);
31  assert.deepStrictEqual(keys, expectedKeys);
32}
33
34{
35  const stream = new PassThrough();
36  const sequence = [];
37  const keys = [];
38
39  stream.on('keypress', (s, k) => {
40    sequence.push(s);
41    keys.push(k);
42  });
43  readline.emitKeypressEvents(stream);
44  stream.write('foo');
45
46  assert.deepStrictEqual(sequence, expectedSequence);
47  assert.deepStrictEqual(keys, expectedKeys);
48}
49
50{
51  const stream = new PassThrough();
52  const sequence = [];
53  const keys = [];
54  const keypressListener = (s, k) => {
55    sequence.push(s);
56    keys.push(k);
57  };
58
59  stream.on('keypress', keypressListener);
60  readline.emitKeypressEvents(stream);
61  stream.removeListener('keypress', keypressListener);
62  stream.write('foo');
63
64  assert.deepStrictEqual(sequence, []);
65  assert.deepStrictEqual(keys, []);
66
67  stream.on('keypress', keypressListener);
68  stream.write('foo');
69
70  assert.deepStrictEqual(sequence, expectedSequence);
71  assert.deepStrictEqual(keys, expectedKeys);
72}
73