|
1 | 1 | import { jest } from '@jest/globals'; |
2 | | -import { inputFromTTY } from '../../src/ux/input.js'; |
3 | | -import readline from 'readline'; |
| 2 | +import EventEmitter from 'node:events'; |
| 3 | +import { mockConsole } from '../helpers.js'; |
4 | 4 |
|
5 | | -jest.mock('readline'); |
| 5 | +jest.mock('node:readline'); |
6 | 6 | jest.useFakeTimers(); |
7 | 7 |
|
8 | 8 | describe('UX: input tests', () => { |
9 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'f', {name: 'f'}), 10); |
| 9 | + const questionMock = jest.fn(); |
| 10 | + const emitKeypressEventsMock = jest.fn(); |
| 11 | + const rlOn = jest.fn(); |
| 12 | + const rlOff = jest.fn(); |
| 13 | + const readline = jest.createMockFromModule('readline'); |
| 14 | + jest.unstable_mockModule('readline', () => ({ default: readline })); |
| 15 | + |
| 16 | + const inputMock = new EventEmitter(); |
| 17 | + |
| 18 | + const closeMock = jest.fn().mockImplementation(() => undefined); |
| 19 | + |
| 20 | + const createInterface = jest.fn().mockReturnValue({ |
| 21 | + question: questionMock, |
| 22 | + close: closeMock, |
| 23 | + emitKeypressEvents: emitKeypressEventsMock, |
| 24 | + input: inputMock, |
| 25 | + on: rlOn, |
| 26 | + off: rlOff, |
| 27 | + }); |
| 28 | + |
| 29 | + readline.createInterface = createInterface; |
| 30 | + readline.emitKeypressEvents = emitKeypressEventsMock; |
| 31 | + readline.input = inputMock; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + mockConsole(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
10 | 41 | test('Will capture printable keys', async () => { |
11 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 11); |
12 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 12); |
13 | | - setTimeout(() => readline.__inputMock.emit('keypress', '\r', {name: 'return'}), 20); |
| 42 | + |
| 43 | + setTimeout(() => inputMock.emit('keypress', 'f', { name: 'f' }), 10); |
| 44 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 11); |
| 45 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 12); |
| 46 | + setTimeout(() => inputMock.emit('keypress', '\r', { name: 'return' }), 20); |
| 47 | + |
| 48 | + const { inputFromTTY } = await import('../../src/ux/input.js'); |
| 49 | + |
14 | 50 | const input = inputFromTTY({}); |
| 51 | + |
15 | 52 | jest.advanceTimersByTime(100); |
| 53 | + |
| 54 | + |
16 | 55 | const result = await Promise.resolve(input); |
| 56 | + |
17 | 57 | expect(result).toBe('foo'); |
18 | 58 | }); |
19 | 59 |
|
20 | 60 | test('Will delete characters with delete key', async () => { |
21 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'f', {name: 'f'}), 10); |
22 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 11); |
23 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 12); |
24 | | - setTimeout(() => readline.__inputMock.emit('keypress', '', {name: 'delete'}), 13); |
25 | | - setTimeout(() => readline.__inputMock.emit('keypress', '\r', {name: 'return'}), 20); |
| 61 | + |
| 62 | + setTimeout(() => inputMock.emit('keypress', 'f', { name: 'f' }), 10); |
| 63 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 11); |
| 64 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 12); |
| 65 | + setTimeout(() => inputMock.emit('keypress', '', { name: 'delete' }), 13); |
| 66 | + |
| 67 | + setTimeout(() => inputMock.emit('keypress', '\r', { name: 'return' }), 20); |
| 68 | + |
| 69 | + const { inputFromTTY } = await import('../../src/ux/input.js'); |
26 | 70 | const input = inputFromTTY({}); |
| 71 | + |
27 | 72 | jest.advanceTimersByTime(100); |
| 73 | + |
28 | 74 | const result = await Promise.resolve(input); |
| 75 | + |
29 | 76 | expect(result).toBe('fo'); |
30 | 77 | }); |
31 | 78 |
|
32 | 79 | test('Will delete characters with backspace key', async () => { |
33 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'f', {name: 'f'}), 10); |
34 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 11); |
35 | | - setTimeout(() => readline.__inputMock.emit('keypress', 'o', {name: 'o'}), 12); |
36 | | - setTimeout(() => readline.__inputMock.emit('keypress', '', {name: 'backspace'}), 13); |
37 | | - setTimeout(() => readline.__inputMock.emit('keypress', '\r', {name: 'return'}), 20); |
| 80 | + setTimeout(() => inputMock.emit('keypress', 'f', { name: 'f' }), 10); |
| 81 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 11); |
| 82 | + setTimeout(() => inputMock.emit('keypress', 'o', { name: 'o' }), 12); |
| 83 | + setTimeout(() => inputMock.emit('keypress', '', { name: 'backspace' }), 13); |
| 84 | + setTimeout(() => inputMock.emit('keypress', '\r', { name: 'return' }), 20); |
| 85 | + |
| 86 | + const { inputFromTTY } = await import('../../src/ux/input.js'); |
38 | 87 | const input = inputFromTTY({}); |
| 88 | + |
39 | 89 | jest.advanceTimersByTime(100); |
| 90 | + |
40 | 91 | const result = await Promise.resolve(input); |
41 | 92 | expect(result).toBe('fo'); |
42 | 93 | }); |
|
0 commit comments