Skip to content

Commit

Permalink
test: add tests from #195
Browse files Browse the repository at this point in the history
  • Loading branch information
orochaa committed Nov 26, 2024
1 parent 070c251 commit 8d4360e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 330 deletions.
2 changes: 2 additions & 0 deletions packages/core/__tests__/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mock-readable.js';
export * from './mock-writable.js';
File renamed without changes.
File renamed without changes.
95 changes: 91 additions & 4 deletions packages/core/__tests__/prompts/prompt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { randomUUID } from 'crypto';
import { cursor } from 'sisteransi';
import { mockPrompt, setGlobalAliases } from '../../src';
import Prompt, { PromptOptions } from '../../src/prompts/prompt';
import { MockReadable, MockWritable } from '../mocks';

const outputSpy = jest.spyOn(process.stdout, 'write').mockImplementation();

Expand All @@ -17,8 +19,15 @@ const makeSut = (opts?: Omit<PromptOptions<Prompt>, 'render'>, trackValue?: bool
};

describe('Prompt', () => {
let input: MockReadable;
let output: MockWritable;
const mock = mockPrompt();

beforeEach(() => {
input = new MockReadable();
output = new MockWritable();
});

afterEach(() => {
mock.close();
});
Expand Down Expand Up @@ -139,15 +148,22 @@ describe('Prompt', () => {
});

it('should allow tab completion for placeholders', () => {
const placeholder = randomUUID();
makeSut({ initialValue: '', placeholder });
makeSut({ initialValue: '', placeholder: 'bar' });

mock.pressKey('\t', { name: 'tab' });

expect(mock.value).toBe('bar');
});

it('should not allow tab completion if value is set', () => {
makeSut({ initialValue: 'foo', placeholder: 'bar' });

mock.pressKey('\t', { name: 'tab' });

expect(mock.value).toBe(placeholder);
expect(mock.value).toBe('foo');
});

it('should render prompt', () => {
it('should render prompt on default output', () => {
mock.setIsTestMode(false);
const value = randomUUID();

Expand All @@ -158,6 +174,31 @@ describe('Prompt', () => {
expect(outputSpy).toHaveBeenCalledWith(value);
});

test('should render prompt on custom output', () => {
mock.setIsTestMode(false);
const value = 'foo';

makeSut({ input, output, initialValue: value });

expect(output.buffer).toStrictEqual([cursor.hide, 'foo']);
});

it('should re-render prompt on resize', () => {
const renderFn = jest.fn().mockImplementation(() => 'foo');
const instance = new Prompt({
input,
output,
render: renderFn,
});
instance.prompt();

expect(renderFn).toHaveBeenCalledTimes(1);

output.emit('resize');

expect(renderFn).toHaveBeenCalledTimes(2);
});

it('should update single line', () => {
mock.setIsTestMode(false);
const value = randomUUID();
Expand Down Expand Up @@ -188,4 +229,50 @@ describe('Prompt', () => {
expect(outputSpy).toHaveBeenCalledWith(value);
expect(outputSpy).toHaveBeenCalledWith(newValue);
});

it('should emit cursor events for movement keys', () => {
const keys = ['up', 'down', 'left', 'right'];
const eventSpy = jest.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
});

instance.on('cursor', eventSpy);

instance.prompt();

for (const key of keys) {
input.emit('keypress', key, { name: key });
expect(eventSpy).toBeCalledWith(key);
}
});

it('should emit cursor events for movement key aliases when not tracking', () => {
const keys = [
['k', 'up'],
['j', 'down'],
['h', 'left'],
['l', 'right'],
];
const eventSpy = jest.fn();
const instance = new Prompt(
{
input,
output,
render: () => 'foo',
},
false
);

instance.on('cursor', eventSpy);

instance.prompt();

for (const [alias, key] of keys) {
input.emit('keypress', alias, { name: alias });
expect(eventSpy).toBeCalledWith(key);
}
});
});
231 changes: 0 additions & 231 deletions packages/core/test/prompts/prompt.test.ts

This file was deleted.

Loading

0 comments on commit 8d4360e

Please sign in to comment.