Skip to content

Commit

Permalink
stub inquirer's registerPrompt to allow custom prompt modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 25, 2023
1 parent a951faa commit 623019a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 12 additions & 4 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class TestAdapter implements InputOutputAdapter {
promptModule: PromptModule;
diff: any;
log: Logger;
registerDummyPrompt: (promptName: string, customPromptOptions?: DummyPromptOptions) => PromptModule;

constructor(options: TestAdapterOptions = {}) {
const { log = createLogger(), ...promptOptions } = options;
Expand All @@ -86,15 +87,22 @@ export class TestAdapter implements InputOutputAdapter {
skipTTYChecks: true,
});

for (const promptName of Object.keys(this.promptModule.prompts)) {
this.promptModule.registerPrompt(
promptName,
const actualRegisterPrompt = this.promptModule.registerPrompt.bind(this.promptModule);

this.registerDummyPrompt = (promptModuleName: string, customPromptOptions?: DummyPromptOptions) =>
actualRegisterPrompt(
promptModuleName,
class CustomDummyPrompt extends DummyPrompt {
constructor(question: PromptQuestion, rl: any, answers: PromptAnswers) {
super(question, rl, answers, promptOptions);
super(question, rl, answers, customPromptOptions ?? promptOptions);
}
} as any,
);

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

for (const promptName of Object.keys(this.promptModule.prompts)) {
this.promptModule.registerPrompt(promptName, undefined as any);
}

this.diff = sinonSpy();
Expand Down
12 changes: 3 additions & 9 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,11 @@ export class YeomanTest {
throw new Error('environment is not an Environment instance');
}

const { promptModule } = environment.adapter as TestAdapter;
const testAdapter = environment.adapter as TestAdapter;
const { promptModule } = testAdapter;

for (const name of Object.keys(promptModule.prompts)) {
promptModule.registerPrompt(
name,
class CustomDummyPrompt extends DummyPrompt {
constructor(question: PromptQuestion, rl: any, answers: PromptAnswers) {
super(question, rl, answers, { ...options, mockedAnswers });
}
} as any,
);
testAdapter.registerDummyPrompt(name, { ...options, mockedAnswers });
}
}

Expand Down

0 comments on commit 623019a

Please sign in to comment.