|
1 | 1 | import { cursor } from 'sisteransi'; |
2 | 2 | import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
3 | 3 | import { default as AutocompletePrompt } from '../../src/prompts/autocomplete.js'; |
| 4 | +import { isCancel } from '../../src/utils/index.js'; |
4 | 5 | import { MockReadable } from '../mock-readable.js'; |
5 | 6 | import { MockWritable } from '../mock-writable.js'; |
6 | 7 |
|
@@ -328,4 +329,120 @@ describe('AutocompletePrompt', () => { |
328 | 329 |
|
329 | 330 | expect(instance.userInput).to.equal('ban'); |
330 | 331 | }); |
| 332 | + |
| 333 | + test('escape clears the search input instead of cancelling', async () => { |
| 334 | + const instance = new AutocompletePrompt({ |
| 335 | + input, |
| 336 | + output, |
| 337 | + render: () => 'foo', |
| 338 | + options: testOptions, |
| 339 | + }); |
| 340 | + |
| 341 | + const promise = instance.prompt(); |
| 342 | + input.emit('keypress', 'b', { name: 'b' }); |
| 343 | + input.emit('keypress', 'a', { name: 'a' }); |
| 344 | + expect(instance.filteredOptions.length).to.equal(1); |
| 345 | + |
| 346 | + input.emit('keypress', '', { name: 'escape' }); |
| 347 | + |
| 348 | + expect(instance.state).to.equal('active'); |
| 349 | + expect(instance.userInput).to.equal(''); |
| 350 | + expect(instance.filteredOptions.length).to.equal(testOptions.length); |
| 351 | + |
| 352 | + input.emit('keypress', 'c', { name: 'c' }); |
| 353 | + expect(instance.userInput).to.equal('c'); |
| 354 | + |
| 355 | + input.emit('keypress', '', { name: 'return' }); |
| 356 | + expect(await promise).to.equal('cherry'); |
| 357 | + }); |
| 358 | + |
| 359 | + test('escape cancels once the search input is empty', async () => { |
| 360 | + const instance = new AutocompletePrompt({ |
| 361 | + input, |
| 362 | + output, |
| 363 | + render: () => 'foo', |
| 364 | + options: testOptions, |
| 365 | + }); |
| 366 | + |
| 367 | + const promise = instance.prompt(); |
| 368 | + input.emit('keypress', 'b', { name: 'b' }); |
| 369 | + input.emit('keypress', '', { name: 'escape' }); |
| 370 | + input.emit('keypress', '', { name: 'escape' }); |
| 371 | + |
| 372 | + expect(isCancel(await promise)).to.equal(true); |
| 373 | + expect(instance.state).to.equal('cancel'); |
| 374 | + }); |
| 375 | + |
| 376 | + test('escape clears the whole search input when the cursor is not at the end', () => { |
| 377 | + const instance = new AutocompletePrompt({ |
| 378 | + input, |
| 379 | + output, |
| 380 | + render: () => 'foo', |
| 381 | + options: testOptions, |
| 382 | + }); |
| 383 | + |
| 384 | + instance.prompt(); |
| 385 | + input.emit('keypress', 'b', { name: 'b' }); |
| 386 | + input.emit('keypress', 'a', { name: 'a' }); |
| 387 | + input.emit('keypress', 'n', { name: 'n' }); |
| 388 | + input.emit('keypress', '', { name: 'left' }); |
| 389 | + input.emit('keypress', '', { name: 'left' }); |
| 390 | + input.emit('keypress', '', { name: 'escape' }); |
| 391 | + |
| 392 | + expect(instance.userInput).to.equal(''); |
| 393 | + |
| 394 | + input.emit('keypress', 'g', { name: 'g' }); |
| 395 | + expect(instance.userInput).to.equal('g'); |
| 396 | + }); |
| 397 | + |
| 398 | + test('escape keeps selections when clearing the search input in multiple mode', async () => { |
| 399 | + const instance = new AutocompletePrompt({ |
| 400 | + input, |
| 401 | + output, |
| 402 | + render: () => 'foo', |
| 403 | + options: testOptions, |
| 404 | + multiple: true, |
| 405 | + }); |
| 406 | + |
| 407 | + const promise = instance.prompt(); |
| 408 | + input.emit('keypress', 'b', { name: 'b' }); |
| 409 | + input.emit('keypress', '\t', { name: 'tab' }); |
| 410 | + input.emit('keypress', '', { name: 'escape' }); |
| 411 | + input.emit('keypress', 'c', { name: 'c' }); |
| 412 | + input.emit('keypress', '\t', { name: 'tab' }); |
| 413 | + input.emit('keypress', '', { name: 'return' }); |
| 414 | + |
| 415 | + expect(await promise).to.deep.equal(['banana', 'cherry']); |
| 416 | + }); |
| 417 | + |
| 418 | + test('ctrl-c cancels even when the search input has text', async () => { |
| 419 | + const instance = new AutocompletePrompt({ |
| 420 | + input, |
| 421 | + output, |
| 422 | + render: () => 'foo', |
| 423 | + options: testOptions, |
| 424 | + }); |
| 425 | + |
| 426 | + const promise = instance.prompt(); |
| 427 | + input.emit('keypress', 'b', { name: 'b' }); |
| 428 | + input.emit('keypress', '\x03', { name: 'c', ctrl: true }); |
| 429 | + |
| 430 | + expect(isCancel(await promise)).to.equal(true); |
| 431 | + }); |
| 432 | + |
| 433 | + test('escape cancels with search input when clearOnCancel is false', async () => { |
| 434 | + const instance = new AutocompletePrompt({ |
| 435 | + input, |
| 436 | + output, |
| 437 | + render: () => 'foo', |
| 438 | + options: testOptions, |
| 439 | + clearOnCancel: false, |
| 440 | + }); |
| 441 | + |
| 442 | + const promise = instance.prompt(); |
| 443 | + input.emit('keypress', 'b', { name: 'b' }); |
| 444 | + input.emit('keypress', '', { name: 'escape' }); |
| 445 | + |
| 446 | + expect(isCancel(await promise)).to.equal(true); |
| 447 | + }); |
331 | 448 | }); |
0 commit comments