From df1c82850aa65e6f355ef80aa246cdc0815981fc Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Tue, 8 Sep 2020 08:54:49 +0000 Subject: [PATCH 01/11] Added 'MockPromptForString' class and changed 'doctor' to work with 'ValidatePathError' --- packages/cli/src/lib/cmds/doctor.ts | 4 +- .../cli/src/spec/mock/MockPromptForStrings.ts | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/spec/mock/MockPromptForStrings.ts diff --git a/packages/cli/src/lib/cmds/doctor.ts b/packages/cli/src/lib/cmds/doctor.ts index 1fe059c5..f81b91d9 100644 --- a/packages/cli/src/lib/cmds/doctor.ts +++ b/packages/cli/src/lib/cmds/doctor.ts @@ -21,10 +21,10 @@ import {enUS as messages} from '../strings'; async function jdkDoctor(config: Config, log: Log): Promise { const result = await JdkHelper.validatePath(config.jdkPath); if (result.isError()) { - if (result.unwrapError().message === 'jdkPathIsNotCorrect') { + if (result.unwrapError().getErrorCode() === 'PathIsNotCorrect') { log.error(messages.jdkPathIsNotCorrect); return false; - } else if (result.unwrapError().message === 'jdkIsNotSupported') { + } else if (result.unwrapError().getErrorCode() === 'PathIsNotSupported') { log.error(messages.jdkIsNotSupported); return false; } else { // Error while reading the file, will print the error message. diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts new file mode 100644 index 00000000..0c8a5744 --- /dev/null +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -0,0 +1,60 @@ +/* + * Copyright 2020 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Prompt} from '../../lib/Prompt'; + +/** + * A class which usef for testing and which mocks user's input. + */ +export class MockPromptForStrings implements Prompt { + async printMessage(): Promise { + // An empty function for testing. + } + + /** + * Sets the output to be the given message. + * @param message the message to be returned. + */ + async promptInput(message: string): Promise { + return message as unknown as T; + } + + /** + * Sets the output to be the given message. + * @param message the message to be returned. + */ + + async promptChoice(message: string): Promise { + return message as unknown as T; + } + + /** + * Sets the output to be the given message. + * @param defaultValue the value to be returned + */ + async promptConfirm(message: string, defaultValue: boolean): Promise { + return defaultValue; + } + + /** + * Sets the output to be the givven message. + * @param message the message to be returned. + */ + + async promptPassword(message: string): Promise { + return message as unknown as T; + } +} From 556dacdd7097cee39daa13665d13c1dfdca47237 Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Tue, 8 Sep 2020 09:10:27 +0000 Subject: [PATCH 02/11] Removed 'ValidatePathError' from 'index.ts' --- packages/core/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index db7695fb..7fcc4133 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -28,7 +28,6 @@ import {TwaGenerator} from './lib/TwaGenerator'; import {DigitalAssetLinks} from './lib/DigitalAssetLinks'; import * as util from './lib/util'; import {Result} from './lib/Result'; -import {ValidatePathError} from './lib/errors/ValidatePathError'; export {AndroidSdkTools, Config, @@ -48,5 +47,4 @@ export {AndroidSdkTools, util, Result, SigningKeyInfo, - ValidatePathError, }; From fac428df29b1e439a27289055ade7eb69fbdd5b8 Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Tue, 8 Sep 2020 09:19:41 +0000 Subject: [PATCH 03/11] Fixed a typo in 'MockPromptForStrings' --- packages/cli/src/spec/mock/MockPromptForStrings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts index 0c8a5744..f2f440ea 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -17,7 +17,7 @@ import {Prompt} from '../../lib/Prompt'; /** - * A class which usef for testing and which mocks user's input. + * A class which used for testing and which mocks user's input. */ export class MockPromptForStrings implements Prompt { async printMessage(): Promise { From acba2ead8d267243bbdd27a68ae7d47adaf3932c Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Tue, 8 Sep 2020 12:35:56 +0000 Subject: [PATCH 04/11] Changed the implementation of the class. Now in order to use it we need to insert the future answers and it will load them as aa user answer when a prompt is being done --- .../cli/src/spec/mock/MockPromptForStrings.ts | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts index f2f440ea..863147e1 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -14,12 +14,24 @@ * limitations under the License. */ -import {Prompt} from '../../lib/Prompt'; +import {Prompt, ValidateFunction} from '../../lib/Prompt'; /** * A class which used for testing and which mocks user's input. */ export class MockPromptForStrings implements Prompt { + private responses: string[] = []; + private lastMessageIndex = -1; + + /** + * Sets the next answer of this class to be the given message. + * @param message the message to be returned in the next prompt message. + */ + addMessage(message: string): void { + this.responses.push(message); + this.lastMessageIndex++; + } + async printMessage(): Promise { // An empty function for testing. } @@ -27,34 +39,66 @@ export class MockPromptForStrings implements Prompt { /** * Sets the output to be the given message. * @param message the message to be returned. + * @param {string | null} defaultValue a default value or null. + * @param {ValidateFunction} validateFunction a function to validate the input. */ - async promptInput(message: string): Promise { - return message as unknown as T; + async promptInput(message: string, + _defaultValue: string | null, + validateFunction: ValidateFunction): Promise { + if (this.lastMessageIndex < 0) { + throw new Error('No answer was given. Please use addMessage(NextResponse) before' + + ' using this function'); + } + const nextResponse = this.responses[this.lastMessageIndex--]; + this.responses.pop(); + return (await validateFunction(nextResponse)).unwrap(); } + /** * Sets the output to be the given message. * @param message the message to be returned. + * @param {string[]} choices a list of choices. Not important for testing. + * @param {string | null} defaultValue a default value or null. + * @param {ValidateFunction} validateFunction a function to validate the input. */ - async promptChoice(message: string): Promise { - return message as unknown as T; + async promptChoice( + _message: string, + _choices: string[], + _defaultValue: string | null, + validateFunction: ValidateFunction): Promise { + if (this.lastMessageIndex < 0) { + throw new Error('No answer was given. Please use addMessage(NextResponse) before' + + ' using this function'); + } + const nextResponse = this.responses[this.lastMessageIndex--]; + this.responses.pop(); + return (await validateFunction(nextResponse)).unwrap(); } /** * Sets the output to be the given message. * @param defaultValue the value to be returned */ - async promptConfirm(message: string, defaultValue: boolean): Promise { + async promptConfirm(_message: string, defaultValue: boolean): Promise { return defaultValue; } /** * Sets the output to be the givven message. * @param message the message to be returned. + * @param {ValidateFunction} validateFunction a function to validate the input. */ - async promptPassword(message: string): Promise { - return message as unknown as T; + async promptPassword(_message: string, validateFunction: ValidateFunction, + ): Promise { + if (this.lastMessageIndex < 0) { + throw new Error('No answer was given. Please use addMessage(NextResponse) before' + + ' using this function'); + } + const nextResponse = this.responses[this.lastMessageIndex--]; + this.responses.pop(); + return (await validateFunction(nextResponse)).unwrap(); } } From b3fc03cc3d8ca64ae3b7d82e3606c56bf6443846 Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Tue, 8 Sep 2020 14:53:45 +0000 Subject: [PATCH 05/11] Added 'getNextMessage' function and did some small syntactic changes --- .../cli/src/spec/mock/MockPromptForStrings.ts | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts index 863147e1..2868a992 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -21,7 +21,6 @@ import {Prompt, ValidateFunction} from '../../lib/Prompt'; */ export class MockPromptForStrings implements Prompt { private responses: string[] = []; - private lastMessageIndex = -1; /** * Sets the next answer of this class to be the given message. @@ -29,7 +28,6 @@ export class MockPromptForStrings implements Prompt { */ addMessage(message: string): void { this.responses.push(message); - this.lastMessageIndex++; } async printMessage(): Promise { @@ -38,67 +36,72 @@ export class MockPromptForStrings implements Prompt { /** * Sets the output to be the given message. - * @param message the message to be returned. + * @param message the message to be prompt. Not relevant for tests. * @param {string | null} defaultValue a default value or null. * @param {ValidateFunction} validateFunction a function to validate the input. + * @returns {Promise} a {@link Promise} that resolves to the validated loaded message, + * converted to `T` by the `validateFunction`. */ - async promptInput(message: string, + async promptInput(_message: string, _defaultValue: string | null, validateFunction: ValidateFunction): Promise { - if (this.lastMessageIndex < 0) { - throw new Error('No answer was given. Please use addMessage(NextResponse) before' + - ' using this function'); - } - const nextResponse = this.responses[this.lastMessageIndex--]; - this.responses.pop(); + const nextResponse = this.getNextMessage(); return (await validateFunction(nextResponse)).unwrap(); } - /** * Sets the output to be the given message. - * @param message the message to be returned. - * @param {string[]} choices a list of choices. Not important for testing. + * @param message the message to be prompt. Not relevant for tests. + * @param {string[]} choices a list of choices. Not relevant for testing. * @param {string | null} defaultValue a default value or null. * @param {ValidateFunction} validateFunction a function to validate the input. + * @returns {Promise} a {@link Promise} that resolves to the validated loaded message, + * converted to `T` by the `validateFunction`. */ - - async promptChoice( - _message: string, + async promptChoice(_message: string, _choices: string[], _defaultValue: string | null, validateFunction: ValidateFunction): Promise { - if (this.lastMessageIndex < 0) { - throw new Error('No answer was given. Please use addMessage(NextResponse) before' + - ' using this function'); - } - const nextResponse = this.responses[this.lastMessageIndex--]; - this.responses.pop(); + const nextResponse = this.getNextMessage(); return (await validateFunction(nextResponse)).unwrap(); } /** * Sets the output to be the given message. + * @param message the message to be prompt. Not relevant for tests. * @param defaultValue the value to be returned + * @returns {Promise} a {@link Promise} that resolves to a {@link boolean} value. The + * value will the `true` if the user answers `Yes` and `false` for `No`. */ async promptConfirm(_message: string, defaultValue: boolean): Promise { return defaultValue; } /** - * Sets the output to be the givven message. - * @param message the message to be returned. + * Sets the output to be the given message. + * @param message the message to be prompt. Not relevant for tests. * @param {ValidateFunction} validateFunction a function to validate the input. + * @returns {Promise} a {@link Promise} that resolves to the user input validated by + * `validateFunction`. */ - async promptPassword(_message: string, validateFunction: ValidateFunction, ): Promise { - if (this.lastMessageIndex < 0) { + const nextResponse = this.getNextMessage(); + return (await validateFunction(nextResponse)).unwrap(); + } + + /** + * Sets the output to be the given message. + * @param {ValidateFunction} validateFunction a function to validate the input. + * @returns {string} which is the next message to be prompted`. + */ + private getNextMessage(): string { + if (this.responses.length < 0) { throw new Error('No answer was given. Please use addMessage(NextResponse) before' + ' using this function'); } - const nextResponse = this.responses[this.lastMessageIndex--]; + const nextResponse = this.responses[this.responses.length]; this.responses.pop(); - return (await validateFunction(nextResponse)).unwrap(); + return nextResponse; } } From 1b4ffdf1acd7ae1719ca6568cc102beead91538a Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 06:53:18 +0000 Subject: [PATCH 06/11] Changed the order of the insertion of the messages. it is not in reversed order now --- packages/cli/src/spec/mock/MockPromptForStrings.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts index 2868a992..124e353b 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -27,7 +27,7 @@ export class MockPromptForStrings implements Prompt { * @param message the message to be returned in the next prompt message. */ addMessage(message: string): void { - this.responses.push(message); + this.responses.unshift(message); } async printMessage(): Promise { @@ -97,11 +97,11 @@ export class MockPromptForStrings implements Prompt { */ private getNextMessage(): string { if (this.responses.length < 0) { - throw new Error('No answer was given. Please use addMessage(NextResponse) before' + + throw new Error('No answer was given. Please use addMessage(nextMessage) before' + ' using this function'); } const nextResponse = this.responses[this.responses.length]; - this.responses.pop(); + this.responses.shift(); return nextResponse; } } From 2c18f9d7a8c0083c2d5093679b1e6b4e7910e852 Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 09:25:37 +0000 Subject: [PATCH 07/11] Added tests for the class and changed it's logic --- .../cli/src/spec/mock/MockPromptForStrings.ts | 6 ++-- packages/cli/src/spec/promptSpec.ts | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/spec/promptSpec.ts diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPromptForStrings.ts index 124e353b..f778b61e 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPromptForStrings.ts @@ -27,7 +27,7 @@ export class MockPromptForStrings implements Prompt { * @param message the message to be returned in the next prompt message. */ addMessage(message: string): void { - this.responses.unshift(message); + this.responses.push(message); } async printMessage(): Promise { @@ -96,11 +96,11 @@ export class MockPromptForStrings implements Prompt { * @returns {string} which is the next message to be prompted`. */ private getNextMessage(): string { - if (this.responses.length < 0) { + if (this.responses.length === 0) { throw new Error('No answer was given. Please use addMessage(nextMessage) before' + ' using this function'); } - const nextResponse = this.responses[this.responses.length]; + const nextResponse = this.responses[0]; this.responses.shift(); return nextResponse; } diff --git a/packages/cli/src/spec/promptSpec.ts b/packages/cli/src/spec/promptSpec.ts new file mode 100644 index 00000000..911d9ad8 --- /dev/null +++ b/packages/cli/src/spec/promptSpec.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Result} from '@bubblewrap/core'; +import {MockPromptForStrings} from '../spec/mock/MockPromptForStrings'; + +async function validationFunction(message: string): Promise> { + return Result.ok(message); +} + +describe('MockPromptForStrings', () => { + describe('#promptInput', () => { + it('Checks if the correct messages are being prompted using promptInput function', async () => { + const mock = new MockPromptForStrings(); + mock.addMessage('first'); + mock.addMessage('second'); + expect(await mock.promptInput('', null, validationFunction)).toBe('first'); + expect(await mock.promptInput('', null, validationFunction)).toBe('second'); + mock.addMessage('third'); + expect(await mock.promptInput('', null, validationFunction)).toBe('third'); + }); + }); +}); From 745c35cef6f9c808fbb2049b464c860f4d8000ff Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 09:41:27 +0000 Subject: [PATCH 08/11] Changed the name of the test to mockPromptSpec.ts --- packages/cli/src/spec/mockPromptSpec.ts | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/cli/src/spec/mockPromptSpec.ts diff --git a/packages/cli/src/spec/mockPromptSpec.ts b/packages/cli/src/spec/mockPromptSpec.ts new file mode 100644 index 00000000..4b0f9592 --- /dev/null +++ b/packages/cli/src/spec/mockPromptSpec.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Result} from '@bubblewrap/core'; +import {MockPromptForStrings} from './mock/MockPromptForStrings'; + +async function validationFunction(message: string): Promise> { + return Result.ok(message); +} + +describe('MockPromptForStrings', () => { + describe('#promptInput', () => { + it('Checks if the correct messages are being prompted using promptInput function', async () => { + const mock = new MockPromptForStrings(); + mock.addMessage('first'); + mock.addMessage('second'); + expect(await mock.promptInput('', null, validationFunction)).toBe('first'); + expect(await mock.promptInput('', null, validationFunction)).toBe('second'); + mock.addMessage('third'); + expect(await mock.promptInput('', null, validationFunction)).toBe('third'); + }); + }); +}); From 5897c8ef6050deffa416a1b2530ac36e1845441c Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 09:43:42 +0000 Subject: [PATCH 09/11] Removed the old named test --- packages/cli/src/spec/promptSpec.ts | 36 ----------------------------- 1 file changed, 36 deletions(-) delete mode 100644 packages/cli/src/spec/promptSpec.ts diff --git a/packages/cli/src/spec/promptSpec.ts b/packages/cli/src/spec/promptSpec.ts deleted file mode 100644 index 911d9ad8..00000000 --- a/packages/cli/src/spec/promptSpec.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import {Result} from '@bubblewrap/core'; -import {MockPromptForStrings} from '../spec/mock/MockPromptForStrings'; - -async function validationFunction(message: string): Promise> { - return Result.ok(message); -} - -describe('MockPromptForStrings', () => { - describe('#promptInput', () => { - it('Checks if the correct messages are being prompted using promptInput function', async () => { - const mock = new MockPromptForStrings(); - mock.addMessage('first'); - mock.addMessage('second'); - expect(await mock.promptInput('', null, validationFunction)).toBe('first'); - expect(await mock.promptInput('', null, validationFunction)).toBe('second'); - mock.addMessage('third'); - expect(await mock.promptInput('', null, validationFunction)).toBe('third'); - }); - }); -}); From 011f86d7f713d5099b58e566f951d990574ccc41 Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 12:55:09 +0000 Subject: [PATCH 10/11] Added tests for all of the functions and changed the classes name(removed 'ForStrings' part) --- .../{mockPromptSpec.ts => MockPromptSpec.ts} | 30 +++++++++++++++++-- ...{MockPromptForStrings.ts => MockPrompt.ts} | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) rename packages/cli/src/spec/{mockPromptSpec.ts => MockPromptSpec.ts} (52%) rename packages/cli/src/spec/mock/{MockPromptForStrings.ts => MockPrompt.ts} (98%) diff --git a/packages/cli/src/spec/mockPromptSpec.ts b/packages/cli/src/spec/MockPromptSpec.ts similarity index 52% rename from packages/cli/src/spec/mockPromptSpec.ts rename to packages/cli/src/spec/MockPromptSpec.ts index 4b0f9592..4880f18d 100644 --- a/packages/cli/src/spec/mockPromptSpec.ts +++ b/packages/cli/src/spec/MockPromptSpec.ts @@ -15,7 +15,7 @@ */ import {Result} from '@bubblewrap/core'; -import {MockPromptForStrings} from './mock/MockPromptForStrings'; +import {MockPrompt} from './mock/MockPrompt'; async function validationFunction(message: string): Promise> { return Result.ok(message); @@ -23,8 +23,8 @@ async function validationFunction(message: string): Promise { describe('#promptInput', () => { - it('Checks if the correct messages are being prompted using promptInput function', async () => { - const mock = new MockPromptForStrings(); + it('Checks if the correct messages are being prompted using promptInput', async () => { + const mock = new MockPrompt(); mock.addMessage('first'); mock.addMessage('second'); expect(await mock.promptInput('', null, validationFunction)).toBe('first'); @@ -33,4 +33,28 @@ describe('MockPromptForStrings', () => { expect(await mock.promptInput('', null, validationFunction)).toBe('third'); }); }); + + describe('#promptChoice', () => { + it('Checks if the correct messages are being prompted using promptChoice', async () => { + const mock = new MockPrompt(); + mock.addMessage('first'); + mock.addMessage('second'); + expect(await mock.promptChoice('', [], null, validationFunction)).toBe('first'); + expect(await mock.promptChoice('', [], null, validationFunction)).toBe('second'); + mock.addMessage('third'); + expect(await mock.promptChoice('', [], null, validationFunction)).toBe('third'); + }); + }); + + describe('#promptPassword', () => { + it('Checks if the correct messages are being prompted using promptPassword', async () => { + const mock = new MockPrompt(); + mock.addMessage('first'); + mock.addMessage('second'); + expect(await mock.promptPassword('', validationFunction)).toBe('first'); + expect(await mock.promptPassword('', validationFunction)).toBe('second'); + mock.addMessage('third'); + expect(await mock.promptPassword('', validationFunction)).toBe('third'); + }); + }); }); diff --git a/packages/cli/src/spec/mock/MockPromptForStrings.ts b/packages/cli/src/spec/mock/MockPrompt.ts similarity index 98% rename from packages/cli/src/spec/mock/MockPromptForStrings.ts rename to packages/cli/src/spec/mock/MockPrompt.ts index f778b61e..200be294 100644 --- a/packages/cli/src/spec/mock/MockPromptForStrings.ts +++ b/packages/cli/src/spec/mock/MockPrompt.ts @@ -19,7 +19,7 @@ import {Prompt, ValidateFunction} from '../../lib/Prompt'; /** * A class which used for testing and which mocks user's input. */ -export class MockPromptForStrings implements Prompt { +export class MockPrompt implements Prompt { private responses: string[] = []; /** From 6e1020d0a55ec3ee583f4a91e0681f319e1c8c0b Mon Sep 17 00:00:00 2001 From: Chen Levy Date: Wed, 9 Sep 2020 14:26:43 +0000 Subject: [PATCH 11/11] United 2 lines into one in 'getNextMessage' --- packages/cli/src/spec/MockPromptSpec.ts | 2 +- packages/cli/src/spec/mock/MockPrompt.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/spec/MockPromptSpec.ts b/packages/cli/src/spec/MockPromptSpec.ts index 4880f18d..1549bad5 100644 --- a/packages/cli/src/spec/MockPromptSpec.ts +++ b/packages/cli/src/spec/MockPromptSpec.ts @@ -21,7 +21,7 @@ async function validationFunction(message: string): Promise { +describe('MockPrompt', () => { describe('#promptInput', () => { it('Checks if the correct messages are being prompted using promptInput', async () => { const mock = new MockPrompt(); diff --git a/packages/cli/src/spec/mock/MockPrompt.ts b/packages/cli/src/spec/mock/MockPrompt.ts index 200be294..6a5cb10a 100644 --- a/packages/cli/src/spec/mock/MockPrompt.ts +++ b/packages/cli/src/spec/mock/MockPrompt.ts @@ -100,8 +100,7 @@ export class MockPrompt implements Prompt { throw new Error('No answer was given. Please use addMessage(nextMessage) before' + ' using this function'); } - const nextResponse = this.responses[0]; - this.responses.shift(); + const nextResponse = this.responses.shift()!; return nextResponse; } }