Add more notebook extension tests (#11143)

* more tests

* More tests

* More tests

* Add prompt tests
This commit is contained in:
Chris LaFreniere
2020-06-30 11:01:51 -07:00
committed by GitHub
parent a32f42d475
commit a8a7559229
18 changed files with 295 additions and 101 deletions

View File

@@ -1,32 +1,17 @@
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
import { window } from 'vscode';
import PromptFactory from './factory';
import EscapeException from './escapeException';
import { IQuestion, IPrompter, IPromptCallback } from './question';
import { IQuestion, IPrompter } from './question';
import { ApiWrapper } from '../common/apiWrapper';
// Supports simple pattern for prompting for user input and acting on this
export default class CodeAdapter implements IPrompter {
// TODO define question interface
private fixQuestion(question: any): any {
if (question.type === 'checkbox' && Array.isArray(question.choices)) {
// For some reason when there's a choice of checkboxes, they aren't formatted properly
// Not sure where the issue is
question.choices = question.choices.map((item: any) => {
if (typeof (item) === 'string') {
return { checked: false, name: item, value: item };
} else {
return item;
}
});
}
}
public promptSingle<T>(question: IQuestion, ignoreFocusOut?: boolean): Promise<T> {
public promptSingle<T>(question: IQuestion, apiWrapper?: ApiWrapper): Promise<T> {
let questions: IQuestion[] = [question];
return this.prompt(questions, ignoreFocusOut).then((answers: { [key: string]: T }) => {
return this.prompt(questions, apiWrapper).then((answers: { [key: string]: T }) => {
if (answers) {
let response: T = answers[question.name];
return response || undefined;
@@ -35,15 +20,13 @@ export default class CodeAdapter implements IPrompter {
});
}
public prompt<T>(questions: IQuestion[], ignoreFocusOut?: boolean): Promise<{ [key: string]: T }> {
public prompt<T>(questions: IQuestion[], apiWrapper = new ApiWrapper()): Promise<{ [key: string]: T }> {
let answers: { [key: string]: T } = {};
// Collapse multiple questions into a set of prompt steps
let promptResult: Promise<{ [key: string]: T }> = questions.reduce((promise: Promise<{ [key: string]: T }>, question: IQuestion) => {
this.fixQuestion(question);
return promise.then(() => {
return PromptFactory.createPrompt(question, ignoreFocusOut);
return PromptFactory.createPrompt(question);
}).then(prompt => {
// Original Code: uses jQuery patterns. Keeping for reference
// if (!question.when || question.when(answers) === true) {
@@ -53,7 +36,7 @@ export default class CodeAdapter implements IPrompter {
// }
if (!question.shouldPrompt || question.shouldPrompt(answers) === true) {
return prompt.render().then((result: any) => {
return prompt.render(apiWrapper).then((result: any) => {
answers[question.name] = result;
if (question.onAnswered) {
@@ -71,17 +54,7 @@ export default class CodeAdapter implements IPrompter {
return undefined;
}
window.showErrorMessage(err.message);
});
}
// Helper to make it possible to prompt using callback pattern. Generally Promise is a preferred flow
public promptCallback(questions: IQuestion[], callback: IPromptCallback): void {
// Collapse multiple questions into a set of prompt steps
this.prompt(questions).then(answers => {
if (callback) {
callback(answers);
}
apiWrapper.showErrorMessage(err.message);
});
}
}

View File

@@ -3,18 +3,18 @@
// This code is originally from https://github.com/DonJayamanne/bowerVSCode
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
import { window } from 'vscode';
import Prompt from './prompt';
import LocalizedConstants = require('../common/localizedConstants');
import EscapeException from './escapeException';
import { ApiWrapper } from '../common/apiWrapper';
export default class ConfirmPrompt extends Prompt {
constructor(question: any, ignoreFocusOut?: boolean) {
super(question, ignoreFocusOut);
constructor(question: any) {
super(question);
}
public render(): any {
public render(apiWrapper: ApiWrapper): any {
let choices: { [id: string]: boolean } = {};
choices[LocalizedConstants.msgYes] = true;
choices[LocalizedConstants.msgNo] = false;
@@ -22,7 +22,7 @@ export default class ConfirmPrompt extends Prompt {
let options = this.defaultQuickPickOptions;
options.placeHolder = this._question.message;
return window.showQuickPick(Object.keys(choices), options)
return apiWrapper.showQuickPick(Object.keys(choices), options)
.then(result => {
if (result === undefined) {
throw new EscapeException();

View File

@@ -8,10 +8,10 @@ import ConfirmPrompt from './confirm';
export default class PromptFactory {
public static createPrompt(question: any, ignoreFocusOut?: boolean): Prompt {
public static createPrompt(question: any): Prompt {
switch (question.type) {
case 'confirm':
return new ConfirmPrompt(question, ignoreFocusOut);
return new ConfirmPrompt(question);
default:
throw new Error(`Could not find a prompt for question type ${question.type}`);
}

View File

@@ -4,6 +4,7 @@
// License: https://github.com/DonJayamanne/bowerVSCode/blob/master/LICENSE
import { InputBoxOptions, QuickPickOptions } from 'vscode';
import { ApiWrapper } from '../common/apiWrapper';
abstract class Prompt {
@@ -15,7 +16,7 @@ abstract class Prompt {
this._ignoreFocusOut = ignoreFocusOut ? ignoreFocusOut : false;
}
public abstract render(): any;
public abstract render(apiWrapper: ApiWrapper): any;
protected get defaultQuickPickOptions(): QuickPickOptions {
return {

View File

@@ -4,15 +4,9 @@
*--------------------------------------------------------------------------------------------*/
import vscode = require('vscode');
import { ApiWrapper } from '../common/apiWrapper';
export class QuestionTypes {
public static get input(): string { return 'input'; }
public static get password(): string { return 'password'; }
public static get list(): string { return 'list'; }
public static get confirm(): string { return 'confirm'; }
public static get checkbox(): string { return 'checkbox'; }
public static get expand(): string { return 'expand'; }
}
export const confirm = 'confirm';
// Question interface to clarify how to use the prompt feature
// based on Bower Question format: https://github.com/bower/bower/blob/89069784bb46bfd6639b4a75e98a0d7399a8c2cb/packages/bower-logger/README.md
@@ -54,15 +48,14 @@ export interface IQuestionHandler {
}
export interface IPrompter {
promptSingle<T>(question: IQuestion, ignoreFocusOut?: boolean): Promise<T>;
promptSingle<T>(question: IQuestion, apiWrapper?: ApiWrapper): Promise<T>;
/**
* Prompts for multiple questions
*
* @returns Map of question IDs to results, or undefined if
* the user canceled the question session
*/
prompt<T>(questions: IQuestion[], ignoreFocusOut?: boolean): Promise<{ [questionId: string]: any }>;
promptCallback(questions: IQuestion[], callback: IPromptCallback): void;
prompt<T>(questions: IQuestion[], apiWrapper?: ApiWrapper): Promise<{ [questionId: string]: any }>;
}
export interface IPromptCallback {