|  | 
| 1 | 1 | const {resolve} = require('path') | 
| 2 |  | -const isRunning = require('is-running') | 
| 3 |  | -const {render} = require('../pure') | 
|  | 2 | +const {render, cleanup} = require('../pure') | 
| 4 | 3 | const {fireEvent} = require('../events') | 
| 5 | 4 | const {waitFor} = require("../wait-for"); | 
| 6 | 5 | const {default: userEvent} = require("../user-event"); | 
| 7 | 6 | 
 | 
|  | 7 | +afterEach(async () => { | 
|  | 8 | +    await cleanup(); | 
|  | 9 | +}) | 
|  | 10 | + | 
| 8 | 11 | test('fireEvent write works', async () => { | 
| 9 |  | -  const props = await render('node', [ | 
| 10 |  | -    resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
| 11 |  | -  ]) | 
|  | 12 | +    const props = await render('node', [ | 
|  | 13 | +        resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
|  | 14 | +    ]) | 
| 12 | 15 | 
 | 
| 13 |  | -  const {clear, findByText} = props; | 
|  | 16 | +    const {clear, findByText} = props; | 
| 14 | 17 | 
 | 
| 15 |  | -  const instance = await findByText('First option') | 
|  | 18 | +    const instance = await findByText('First option') | 
| 16 | 19 | 
 | 
| 17 |  | -  expect(instance).toBeTruthy() | 
|  | 20 | +    expect(instance).toBeTruthy() | 
| 18 | 21 | 
 | 
| 19 |  | -  // Windows uses ">", Linux/MacOS use "❯" | 
| 20 |  | -  expect(await findByText(/[❯>] One/)).toBeTruthy() | 
|  | 22 | +    // Windows uses ">", Linux/MacOS use "❯" | 
|  | 23 | +    expect(await findByText(/[❯>] One/)).toBeTruthy() | 
| 21 | 24 | 
 | 
| 22 |  | -  clear() | 
|  | 25 | +    clear() | 
| 23 | 26 | 
 | 
| 24 |  | -  const down = "\x1B\x5B\x42"; | 
| 25 |  | -  fireEvent(instance, 'write', {value: down}) | 
|  | 27 | +    const down = "\x1B\x5B\x42"; | 
|  | 28 | +    fireEvent(instance, 'write', {value: down}) | 
| 26 | 29 | 
 | 
| 27 |  | -  expect(await findByText(/[❯>] Two/)).toBeTruthy() | 
|  | 30 | +    expect(await findByText(/[❯>] Two/)).toBeTruthy() | 
| 28 | 31 | 
 | 
| 29 |  | -  clear() | 
|  | 32 | +    clear() | 
| 30 | 33 | 
 | 
| 31 |  | -  const enter = "\x0D"; | 
| 32 |  | -  fireEvent(instance, 'write', {value: enter}) | 
|  | 34 | +    const enter = "\x0D"; | 
|  | 35 | +    fireEvent(instance, 'write', {value: enter}) | 
| 33 | 36 | 
 | 
| 34 |  | -  expect(await findByText('First option: Two')).toBeTruthy() | 
|  | 37 | +    expect(await findByText('First option: Two')).toBeTruthy() | 
| 35 | 38 | }) | 
| 36 | 39 | 
 | 
| 37 | 40 | // Refactor to use `fireEvent` and not `userEvent` style | 
| 38 |  | -test('FireEvent SigTerm works', async () => { | 
| 39 |  | -  const {findByText} = await render('node', [ | 
| 40 |  | -    resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
| 41 |  | -  ]) | 
|  | 41 | +test.only('FireEvent SigTerm works', async () => { | 
|  | 42 | +    const {findByText} = await render('node', [ | 
|  | 43 | +        resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
|  | 44 | +    ]) | 
| 42 | 45 | 
 | 
| 43 |  | -  const instance = await findByText('First option') | 
|  | 46 | +    const instance = await findByText('First option') | 
| 44 | 47 | 
 | 
| 45 |  | -  expect(instance).toBeTruthy() | 
|  | 48 | +    expect(instance).toBeTruthy() | 
| 46 | 49 | 
 | 
| 47 |  | -  fireEvent.sigterm(instance); | 
|  | 50 | +    await fireEvent.sigterm(instance); | 
| 48 | 51 | 
 | 
| 49 |  | -  await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy()) | 
|  | 52 | +    await waitFor(() => expect(instance.hasExit()).toBeTruthy()) | 
| 50 | 53 | }) | 
| 51 | 54 | 
 | 
| 52 | 55 | test('userEvent basic keyboard works', async () => { | 
| 53 |  | -  const {findByText} = await render('node', [ | 
| 54 |  | -    resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'), | 
| 55 |  | -  ]) | 
|  | 56 | +    const {findByText} = await render('node', [ | 
|  | 57 | +        resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'), | 
|  | 58 | +    ]) | 
| 56 | 59 | 
 | 
| 57 |  | -  const instance = await findByText('What is your name?'); | 
| 58 |  | -  expect(instance).toBeTruthy(); | 
|  | 60 | +    const instance = await findByText('What is your name?'); | 
|  | 61 | +    expect(instance).toBeTruthy(); | 
| 59 | 62 | 
 | 
| 60 |  | -  userEvent.keyboard(instance, "Test") | 
|  | 63 | +    userEvent.keyboard(instance, "Test") | 
| 61 | 64 | 
 | 
| 62 |  | -  expect(await findByText('Test')).toBeTruthy(); | 
|  | 65 | +    expect(await findByText('Test')).toBeTruthy(); | 
| 63 | 66 | }) | 
| 64 | 67 | 
 | 
| 65 | 68 | test('userEvent basic keyboard works when bound', async () => { | 
| 66 |  | -  const {findByText, userEvent: userEventLocal} = await render('node', [ | 
| 67 |  | -    resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'), | 
| 68 |  | -  ]) | 
|  | 69 | +    const {findByText, userEvent: userEventLocal} = await render('node', [ | 
|  | 70 | +        resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'), | 
|  | 71 | +    ]) | 
| 69 | 72 | 
 | 
| 70 |  | -  const instance = await findByText('What is your name?'); | 
| 71 |  | -  expect(instance).toBeTruthy(); | 
|  | 73 | +    const instance = await findByText('What is your name?'); | 
|  | 74 | +    expect(instance).toBeTruthy(); | 
| 72 | 75 | 
 | 
| 73 |  | -  userEventLocal.keyboard("Test") | 
|  | 76 | +    userEventLocal.keyboard("Test") | 
| 74 | 77 | 
 | 
| 75 |  | -  expect(await findByText('Test')).toBeTruthy(); | 
|  | 78 | +    expect(await findByText('Test')).toBeTruthy(); | 
| 76 | 79 | }) | 
| 77 | 80 | 
 | 
| 78 | 81 | test("UserEvent.keyboard enter key works", async () => { | 
| 79 |  | -  const props = await render('node', [ | 
| 80 |  | -    resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
| 81 |  | -  ]) | 
|  | 82 | +    const props = await render('node', [ | 
|  | 83 | +        resolve(__dirname, './execute-scripts/stdio-inquirer.js'), | 
|  | 84 | +    ]) | 
| 82 | 85 | 
 | 
| 83 |  | -  const {clear, findByText, userEvent: userEventLocal} = props; | 
|  | 86 | +    const {clear, findByText, userEvent: userEventLocal} = props; | 
| 84 | 87 | 
 | 
| 85 |  | -  const instance = await findByText('First option') | 
|  | 88 | +    const instance = await findByText('First option') | 
| 86 | 89 | 
 | 
| 87 |  | -  expect(instance).toBeTruthy() | 
|  | 90 | +    expect(instance).toBeTruthy() | 
| 88 | 91 | 
 | 
| 89 |  | -  // Windows uses ">", Linux/MacOS use "❯" | 
| 90 |  | -  expect(await findByText(/[❯>] One/)).toBeTruthy() | 
|  | 92 | +    // Windows uses ">", Linux/MacOS use "❯" | 
|  | 93 | +    expect(await findByText(/[❯>] One/)).toBeTruthy() | 
| 91 | 94 | 
 | 
| 92 |  | -  clear() | 
|  | 95 | +    clear() | 
| 93 | 96 | 
 | 
| 94 |  | -  userEventLocal.keyboard('[ArrowDown]') | 
|  | 97 | +    userEventLocal.keyboard('[ArrowDown]') | 
| 95 | 98 | 
 | 
| 96 |  | -  expect(await findByText(/[❯>] Two/)).toBeTruthy() | 
|  | 99 | +    expect(await findByText(/[❯>] Two/)).toBeTruthy() | 
| 97 | 100 | 
 | 
| 98 |  | -  clear() | 
|  | 101 | +    clear() | 
| 99 | 102 | 
 | 
| 100 |  | -  userEventLocal.keyboard('[Enter]') | 
|  | 103 | +    userEventLocal.keyboard('[Enter]') | 
| 101 | 104 | 
 | 
| 102 |  | -  expect(await findByText('First option: Two')).toBeTruthy() | 
|  | 105 | +    expect(await findByText('First option: Two')).toBeTruthy() | 
| 103 | 106 | }) | 
0 commit comments