Skip to content

Commit

Permalink
Chore: Add tests covering usePrefix from @inquirer/core
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Mar 9, 2024
1 parent 05a009f commit 0f2474a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
53 changes: 53 additions & 0 deletions packages/core/core.test.mts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { AsyncResource } from 'node:async_hooks';
import { describe, it, expect, vi } from 'vitest';
import { render } from '@inquirer/testing';
import stripAnsi from 'strip-ansi';
import ansiEscapes from 'ansi-escapes';
import spinners from 'cli-spinners';
import {
createPrompt,
useEffect,
useKeypress,
useState,
useRef,
useMemo,
usePrefix,
isDownKey,
isUpKey,
isEnterKey,
isSpaceKey,
Separator,
makeTheme,

Check failure on line 20 in packages/core/core.test.mts

View workflow job for this annotation

GitHub Actions / Linting

'makeTheme' is defined but never used
type KeypressEvent,
} from './src/index.mjs';

Expand Down Expand Up @@ -355,6 +359,55 @@ describe('createPrompt()', () => {
await expect(answer).resolves.toEqual('b');
});

it.only('usePrefix() renders loader and prefix', async () => {
vi.useFakeTimers();
const { interval } = spinners.dots;
const totalDuration = interval * spinners.dots.frames.length;

const Prompt = (config: { message: string }, done: (value: string) => void) => {
const [status, setStatus] = useState('loading');
const prefix = usePrefix({ isLoading: status === 'loading' });

useEffect(() => {
setTimeout(
AsyncResource.bind(() => {
setStatus('idle');
}),
totalDuration,
);
}, []);

useKeypress((event: KeypressEvent) => {
if (isEnterKey(event)) {
done('');
}
});

return `${prefix} ${config.message}`;
};

const prompt = createPrompt(Prompt);
const { answer, events, getScreen } = await render(prompt, { message: 'Question' });
expect(getScreen()).toMatchInlineSnapshot(`"⠋ Question"`);

vi.advanceTimersByTime(interval);
expect(getScreen()).toMatchInlineSnapshot(`"⠙ Question"`);

vi.advanceTimersByTime(interval);
expect(getScreen()).toMatchInlineSnapshot(`"⠹ Question"`);

vi.advanceTimersByTime(interval);
expect(getScreen()).toMatchInlineSnapshot(`"⠸ Question"`);

vi.advanceTimersByTime(totalDuration);
expect(getScreen()).toMatchInlineSnapshot(`"? Question"`);

vi.useRealTimers();

events.keypress('enter');
await expect(answer).resolves.toEqual('');
});

it('allow cancelling the prompt', async () => {
const Prompt = (config: { message: string }, done: (value: string) => void) => {
useKeypress((key: KeypressEvent) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/lib/use-prefix.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AsyncResource } from 'node:async_hooks';
import { useState } from './use-state.mjs';
import { useEffect } from './use-effect.mjs';
import { makeTheme } from './make-theme.mjs';
Expand All @@ -15,9 +16,12 @@ export function usePrefix({

useEffect((): void | (() => unknown) => {
if (isLoading) {
const timeout = setTimeout(() => {
setTick(tick + 1);
}, spinner.interval);
const timeout = setTimeout(
AsyncResource.bind(() => {
setTick(tick + 1);
}),
spinner.interval,
);

return () => clearTimeout(timeout);
}
Expand Down

0 comments on commit 0f2474a

Please sign in to comment.