[READ-ONLY] Mirror of https://github.com/bombshell-dev/clack. Effortlessly build beautiful command-line apps bomb.sh/docs/clack/basics/getting-started/
cli command-line command-line-app node prompt prompts
5

Configure Feed

Select the types of activity you want to include in your feed.

test: add tests for `SelectPrompt` (#260)

authored by

James Garbutt and committed by
GitHub
(Mar 30, 2025, 2:33 PM -0500) f87e7518 eed2780b

+105
+105
packages/core/test/prompts/select.test.ts
··· 1 + import color from 'picocolors'; 2 + import { cursor } from 'sisteransi'; 3 + import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; 4 + import { default as SelectPrompt } from '../../src/prompts/select.js'; 5 + import { MockReadable } from '../mock-readable.js'; 6 + import { MockWritable } from '../mock-writable.js'; 7 + 8 + describe('SelectPrompt', () => { 9 + let input: MockReadable; 10 + let output: MockWritable; 11 + 12 + beforeEach(() => { 13 + input = new MockReadable(); 14 + output = new MockWritable(); 15 + }); 16 + 17 + afterEach(() => { 18 + vi.restoreAllMocks(); 19 + }); 20 + 21 + test('renders render() result', () => { 22 + const instance = new SelectPrompt({ 23 + input, 24 + output, 25 + render: () => 'foo', 26 + options: [{ value: 'foo' }, { value: 'bar' }], 27 + }); 28 + instance.prompt(); 29 + expect(output.buffer).to.deep.equal([cursor.hide, 'foo']); 30 + }); 31 + 32 + describe('cursor', () => { 33 + test('cursor is index of selected item', () => { 34 + const instance = new SelectPrompt({ 35 + input, 36 + output, 37 + render: () => 'foo', 38 + options: [{ value: 'foo' }, { value: 'bar' }], 39 + }); 40 + 41 + instance.prompt(); 42 + 43 + expect(instance.cursor).to.equal(0); 44 + input.emit('keypress', 'down', { name: 'down' }); 45 + expect(instance.cursor).to.equal(1); 46 + }); 47 + 48 + test('cursor loops around', () => { 49 + const instance = new SelectPrompt({ 50 + input, 51 + output, 52 + render: () => 'foo', 53 + options: [{ value: 'foo' }, { value: 'bar' }, { value: 'baz' }], 54 + }); 55 + 56 + instance.prompt(); 57 + 58 + expect(instance.cursor).to.equal(0); 59 + input.emit('keypress', 'up', { name: 'up' }); 60 + expect(instance.cursor).to.equal(2); 61 + input.emit('keypress', 'down', { name: 'down' }); 62 + expect(instance.cursor).to.equal(0); 63 + }); 64 + 65 + test('left behaves as up', () => { 66 + const instance = new SelectPrompt({ 67 + input, 68 + output, 69 + render: () => 'foo', 70 + options: [{ value: 'foo' }, { value: 'bar' }, { value: 'baz' }], 71 + }); 72 + 73 + instance.prompt(); 74 + 75 + input.emit('keypress', 'left', { name: 'left' }); 76 + expect(instance.cursor).to.equal(2); 77 + }); 78 + 79 + test('right behaves as down', () => { 80 + const instance = new SelectPrompt({ 81 + input, 82 + output, 83 + render: () => 'foo', 84 + options: [{ value: 'foo' }, { value: 'bar' }], 85 + }); 86 + 87 + instance.prompt(); 88 + 89 + input.emit('keypress', 'left', { name: 'left' }); 90 + expect(instance.cursor).to.equal(1); 91 + }); 92 + 93 + test('initial value is selected', () => { 94 + const instance = new SelectPrompt({ 95 + input, 96 + output, 97 + render: () => 'foo', 98 + options: [{ value: 'foo' }, { value: 'bar' }], 99 + initialValue: 'bar', 100 + }); 101 + instance.prompt(); 102 + expect(instance.cursor).to.equal(1); 103 + }); 104 + }); 105 + });