-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin-flow-builder: fallback , keyword, smart-intent tests (#2820)
## Description - Split the bot-action and first-interaction tests into two different files. - Add tests for fallback, keyword and smart intent. - Refactor the function getSmartIntentNodeByInput into a SmartIntentApi class and add a mock for the private function getInference of this class ## Context We need to create tests to keep adding new features and refactor the plugin without changing the behaviour ## Approach taken / Explain the design For the smart intent test I have created a separate flow so that I don't always have to use the mock (we can discuss if this is the best way or if all tests should go in the same flow) ## Testing This PR only contains changes to add tests and refactors the getSmartIntentNodeByInput function into a class and then creates a mock that modifies only one function of this class. Add tests for fallback, keyword and smart intent
- Loading branch information
Showing
12 changed files
with
989 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/botonic-plugin-flow-builder/tests/__mocks__/smart-intent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { jest } from '@jest/globals' | ||
|
||
import { SmartIntentsApi } from '../../src/user-input/smart-intent' | ||
|
||
export function mockSmartIntent(intentName?: string) { | ||
// Spy on the private function getInference | ||
const getInferenceSpy = jest.spyOn( | ||
SmartIntentsApi.prototype as any, | ||
'getInference' | ||
) | ||
|
||
// Change the implementation of getInference | ||
getInferenceSpy.mockImplementation(async () => { | ||
return intentName ? { data: { intent_name: intentName } } : undefined | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/botonic-plugin-flow-builder/tests/fallback.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { INPUT } from '@botonic/core' | ||
import { describe, test } from '@jest/globals' | ||
|
||
import { FlowText } from '../src/index' | ||
import { ProcessEnvNodeEnvs } from '../src/types' | ||
import { basicFlow } from './helpers/flows/basic' | ||
import { | ||
createFlowBuilderPlugin, | ||
createRequest, | ||
getContentsAfterPreAndBotonicInit, | ||
} from './helpers/utils' | ||
|
||
describe('Check the content returned by the plugin when there is no match with payload or keyword or intents', () => { | ||
process.env.NODE_ENV = ProcessEnvNodeEnvs.PRODUCTION | ||
const flowBuilderPlugin = createFlowBuilderPlugin(basicFlow) | ||
|
||
test('The content displayed changes between the 1st and 2nd fallback', async () => { | ||
const request = createRequest({ | ||
input: { data: 'I want to cancel my flight', type: INPUT.TEXT }, | ||
plugins: { | ||
// @ts-ignore | ||
flowBuilderPlugin, | ||
}, | ||
}) | ||
|
||
const firstFallback = await getContentsAfterPreAndBotonicInit( | ||
request, | ||
flowBuilderPlugin | ||
) | ||
expect((firstFallback.contents[0] as FlowText).text).toBe( | ||
'fallback 1st message' | ||
) | ||
|
||
const secondFallback = await getContentsAfterPreAndBotonicInit( | ||
request, | ||
flowBuilderPlugin | ||
) | ||
expect((secondFallback.contents[0] as FlowText).text).toBe( | ||
'fallback 2nd message' | ||
) | ||
|
||
const thirdFallback = await getContentsAfterPreAndBotonicInit( | ||
request, | ||
flowBuilderPlugin | ||
) | ||
|
||
expect((thirdFallback.contents[0] as FlowText).text).toBe( | ||
'fallback 1st message' | ||
) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
packages/botonic-plugin-flow-builder/tests/first-interaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { INPUT } from '@botonic/core' | ||
import { describe, test } from '@jest/globals' | ||
|
||
import { FlowText } from '../src/index' | ||
import { ProcessEnvNodeEnvs } from '../src/types' | ||
import { basicFlow } from './helpers/flows/basic' | ||
import { | ||
createFlowBuilderPlugin, | ||
createRequest, | ||
getContentsAfterPreAndBotonicInit, | ||
} from './helpers/utils' | ||
|
||
describe('Check the contents returned by the plugin', () => { | ||
process.env.NODE_ENV = ProcessEnvNodeEnvs.PRODUCTION | ||
const flowBuilderPlugin = createFlowBuilderPlugin(basicFlow) | ||
|
||
test('The starting content is displayed on the first interaction', async () => { | ||
const request = createRequest({ | ||
input: { data: 'Hola', type: INPUT.TEXT }, | ||
isFirstInteraction: true, | ||
plugins: { | ||
// @ts-ignore | ||
flowBuilderPlugin, | ||
}, | ||
}) | ||
|
||
const { contents } = await getContentsAfterPreAndBotonicInit( | ||
request, | ||
flowBuilderPlugin | ||
) | ||
|
||
expect((contents[0] as FlowText).text).toBe('Welcome message') | ||
}) | ||
}) |
Oops, something went wrong.