Skip to content

Commit

Permalink
test(plugin-flow-builder): create getContentsAfterPreAndBotonicInit t…
Browse files Browse the repository at this point in the history
…o reuse code
  • Loading branch information
Iru89 committed May 8, 2024
1 parent 0903b96 commit 58b0d20
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
29 changes: 17 additions & 12 deletions packages/botonic-plugin-flow-builder/tests/bot-action.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { INPUT } from '@botonic/core'
import { describe, test } from '@jest/globals'

import { BOT_ACTION_PAYLOAD_SEPARATOR } from '../src/constants'
import { FlowBuilderAction, FlowText } from '../src/index'
import { BOT_ACTION_PAYLOAD_PREFIX } from '../src/constants'
import { FlowText } from '../src/index'
import { ProcessEnvNodeEnvs } from '../src/types'
import { testFlow } from './helpers/flows'
import {
createFlowBuilderPlugin,
createRequest,
getActionRequest,
getContentsAfterPreAndBotonicInit,
} from './helpers/utils'

describe('Check the contents returned by the plugin', () => {
process.env.NODE_ENV = ProcessEnvNodeEnvs.PRODUCTION
const flowBuilderPlugin = createFlowBuilderPlugin(testFlow)

test('The user does the first interaction', async () => {
test('The starting content is displayed on the first interaction', async () => {
const request = createRequest({
input: { data: 'Hola', type: INPUT.TEXT },
isFirstInteraction: true,
Expand All @@ -24,9 +24,11 @@ describe('Check the contents returned by the plugin', () => {
flowBuilderPlugin,
},
})
await flowBuilderPlugin.pre(request)
const actionRequest = getActionRequest(request)
const { contents } = await FlowBuilderAction.botonicInit(actionRequest)

const { contents } = await getContentsAfterPreAndBotonicInit(
request,
flowBuilderPlugin
)

expect((contents[0] as FlowText).text).toBe('Welcome message')
})
Expand All @@ -49,18 +51,21 @@ describe('The user clicks on a button that is connected to a BotActionNode', ()
flowBuilderPlugin,
},
})
await flowBuilderPlugin.pre(request)
const actionRequest = getActionRequest(request)
const { contents } = await FlowBuilderAction.botonicInit(actionRequest)

const { contents } = await getContentsAfterPreAndBotonicInit(
request,
flowBuilderPlugin
)

const nextPaylod = (contents[0] as FlowText).buttons[0].payload
expect(nextPaylod).toBe(`${BOT_ACTION_PAYLOAD_SEPARATOR}${botActionUuid}`)
expect(nextPaylod).toBe(`${BOT_ACTION_PAYLOAD_PREFIX}${botActionUuid}`)
})

test('The bot routes receive the correct payload, in the custom action the payloadParmas defined in the BotActionNode are obtained', async () => {
const request = createRequest({
input: {
type: INPUT.POSTBACK,
payload: `${BOT_ACTION_PAYLOAD_SEPARATOR}${botActionUuid}`,
payload: `${BOT_ACTION_PAYLOAD_PREFIX}${botActionUuid}`,
},
plugins: {
// @ts-ignore
Expand Down
11 changes: 10 additions & 1 deletion packages/botonic-plugin-flow-builder/tests/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@botonic/core'
import { ActionRequest } from '@botonic/react'

import BotonicPluginFlowBuilder from '../../src'
import BotonicPluginFlowBuilder, { FlowBuilderAction } from '../../src'

export function createFlowBuilderPlugin(flow: any, locale: string = 'es') {
const flowBuilderPlugin = new BotonicPluginFlowBuilder({
Expand Down Expand Up @@ -66,3 +66,12 @@ export function getActionRequest(request: PluginPreRequest): ActionRequest {
params: {},
}
}

export async function getContentsAfterPreAndBotonicInit(
request: PluginPreRequest,
flowBuilderPlugin: BotonicPluginFlowBuilder
): Promise<any> {
await flowBuilderPlugin.pre(request)
const actionRequest = getActionRequest(request)
return await FlowBuilderAction.botonicInit(actionRequest)
}

0 comments on commit 58b0d20

Please sign in to comment.