Skip to content

Commit b7f4b67

Browse files
authored
feat(adapter): add calls history support (#97)
1 parent 26459a9 commit b7f4b67

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

workspaces/adapter/src/testing/test-adapter.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type TestQuestion = {
1515
default?: any;
1616
};
1717

18+
type PromptCalls = {
19+
question: TestQuestion;
20+
answer: any;
21+
};
22+
1823
export type DummyPromptCallback = (answer: any, { question, answers }: { question: TestQuestion; answers: PromptAnswers }) => any;
1924

2025
export type DummyPromptOptions = {
@@ -47,7 +52,7 @@ const isValueSet = (type: string, answer: any) => {
4752
return Boolean(answer);
4853
};
4954

50-
const createDummyPrompt = (options: DummyPromptOptions = {}) => {
55+
const createDummyPrompt = ({ calls }: { calls: PromptCalls[] }, options: DummyPromptOptions = {}) => {
5156
const { mockedAnswers = {}, callback = answer => answer, throwOnMissingAnswer = false } = options;
5257
return createPrompt<any, TestQuestion>((config, done) => {
5358
let answer = mockedAnswers[config.name!];
@@ -68,6 +73,7 @@ const createDummyPrompt = (options: DummyPromptOptions = {}) => {
6873
answer = true;
6974
}
7075
}
76+
calls.push({ question: config, answer });
7177
done(callback(answer, { question: config, answers: { [config.name]: answer } }));
7278

7379
return config.message;
@@ -87,6 +93,7 @@ export class TestAdapter<LogType extends Logger = Logger, SpyType = any> impleme
8793
log: LogType & SpyType;
8894
registerDummyPrompt: (promptName: string, customPromptOptions?: DummyPromptOptions) => PromptModule;
8995
readonly mockedAnswers: PromptAnswers;
96+
readonly calls: PromptCalls[] = [];
9097

9198
private abortController = new AbortController();
9299
private readonly spyFactory: SpyFactory<SpyType>;
@@ -107,7 +114,6 @@ export class TestAdapter<LogType extends Logger = Logger, SpyType = any> impleme
107114
skipTTYChecks: true,
108115
signal: this.abortController.signal,
109116
});
110-
111117
this.mockedAnswers = {};
112118
this.addAnswers(mockedAnswers ?? {});
113119

@@ -116,7 +122,10 @@ export class TestAdapter<LogType extends Logger = Logger, SpyType = any> impleme
116122
this.registerDummyPrompt = (promptModuleName: string, customPromptOptions?: DummyPromptOptions) =>
117123
actualRegisterPrompt(
118124
promptModuleName,
119-
createDummyPrompt({ callback, mockedAnswers: this.mockedAnswers, throwOnMissingAnswer, ...customPromptOptions }),
125+
createDummyPrompt(
126+
{ calls: this.calls },
127+
{ callback, mockedAnswers: this.mockedAnswers, throwOnMissingAnswer, ...customPromptOptions },
128+
),
120129
);
121130

122131
this.promptModule.registerPrompt = (name: string) => this.registerDummyPrompt(name);

workspaces/adapter/test/test-adapter.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ describe('TestAdapter', () => {
9292
await expect(adapter.prompt([{ name: 'respuesta', message: 'foo', type: 'list' }])).resolves.toMatchObject({ respuesta: 'bar' });
9393
await expect(adapter.prompt([{ name: 'respuesta', message: 'foo', type: 'list' }])).resolves.toMatchObject({ respuesta: undefined });
9494
});
95+
it('adds the question to history', async () => {
96+
const adapter = new TestAdapter();
97+
adapter.addAnswers({ respuesta: 'foo' });
98+
99+
const question = { name: 'respuesta', message: 'foo', type: 'list' } as const;
100+
await adapter.prompt(question);
101+
102+
expect(adapter.calls).toMatchObject([{ question, answer: 'foo' }]);
103+
});
95104
});
96105
describe('#queue()', () => {
97106
it('should execute the callback', async () => {

0 commit comments

Comments
 (0)