From d4c395520bc66f24325babbe53e6ab7ebdea4d3b Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Date: Wed, 17 Aug 2022 10:25:13 -0700 Subject: [PATCH] fix(interactions): fix configure default provider (#10215) * fix(interactions): fix configure default provider * chore: pin @types/lodash version in aws-amplify-angular Co-authored-by: Sridhar --- .../__tests__/Interactions-unit-test.ts | 50 +++++++++++++++++-- packages/interactions/src/Interactions.ts | 7 ++- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/packages/interactions/__tests__/Interactions-unit-test.ts b/packages/interactions/__tests__/Interactions-unit-test.ts index d1aba55ca3f..a7b27a7d7c6 100644 --- a/packages/interactions/__tests__/Interactions-unit-test.ts +++ b/packages/interactions/__tests__/Interactions-unit-test.ts @@ -13,6 +13,7 @@ import { InteractionsClass as Interactions } from '../src/Interactions'; import { AbstractInteractionsProvider } from '../src/Providers'; import { InteractionsOptions } from '../src/types'; +import { AWSLexProvider } from '../src/Providers'; (global as any).Response = () => {}; (global as any).Response.prototype.arrayBuffer = (blob: Blob) => { @@ -216,7 +217,12 @@ describe('Interactions', () => { expect.assertions(4); }); - test('Check if default provider is AWSLexProvider', async () => { + test('Configure bot with default provider (AWSLexProvider) using manual config', async () => { + const lexV1ConfigureSpy = jest.spyOn( + AWSLexProvider.prototype, + 'configure' + ); + const myBot = { MyBot: { name: 'MyBot', // default provider 'AWSLexProvider' @@ -230,10 +236,46 @@ describe('Interactions', () => { }, }; - expect(interactions.configure(myConfig)).toEqual({ - bots: myBot, + interactions.configure(myConfig); + + // check if provider's configure was called + expect(lexV1ConfigureSpy).toBeCalledTimes(Object.keys(myBot).length); + expect(lexV1ConfigureSpy).toHaveBeenCalledWith({ + MyBot: myBot.MyBot, }); - expect.assertions(1); + expect.assertions(2); + }); + + test('Configure bot with default provider (AWSLexProvider) using aws-exports config', async () => { + const lexV1ConfigureSpy = jest.spyOn( + AWSLexProvider.prototype, + 'configure' + ); + + const awsmobileBot = { + name: 'BookTripMOBILEHUB', + alias: '$LATEST', + region: 'us-east-1', + description: 'Bot to make reservations for a visit to a city.', + 'bot-template': 'bot-trips', + }; + const awsmobile = { + aws_bots: 'enable', + aws_bots_config: [awsmobileBot], + aws_project_name: 'bots', + aws_project_region: 'us-east-1', + }; + + interactions.configure(awsmobile); + + // check if provider's configure was called + expect(lexV1ConfigureSpy).toBeCalledTimes( + awsmobile.aws_bots_config.length + ); + expect(lexV1ConfigureSpy).toHaveBeenCalledWith({ + BookTripMOBILEHUB: awsmobileBot, + }); + expect.assertions(2); }); test('Configure bot belonging to non-existing plugin', async () => { diff --git a/packages/interactions/src/Interactions.ts b/packages/interactions/src/Interactions.ts index e965cd40733..44a1626bbe6 100644 --- a/packages/interactions/src/Interactions.ts +++ b/packages/interactions/src/Interactions.ts @@ -67,12 +67,17 @@ export class InteractionsClass { Object.keys(bots_config).forEach(botKey => { const bot = bots_config[botKey]; const providerName = bot.providerName || 'AWSLexProvider'; + + // add default provider if required if ( !this._pluggables.AWSLexProvider && providerName === 'AWSLexProvider' ) { this._pluggables.AWSLexProvider = new AWSLexProvider(); - } else if (this._pluggables[providerName]) { + } + + // configure bot with it's respective provider + if (this._pluggables[providerName]) { this._pluggables[providerName].configure({ [bot.name]: bot }); } else { logger.debug(