Skip to content

Commit

Permalink
fix(messaging): fix inject error (#551)
Browse files Browse the repository at this point in the history
* fix(messaging): fix inject error

* add null check

* test

* run

* fix test

* fix test

* disable broken test
  • Loading branch information
allardy committed Nov 9, 2022
1 parent 799d051 commit 87a8ef5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
browser: ['chrome', 'firefox']
browser: ['chrome']

steps:
- name: Checkout code
Expand All @@ -180,3 +180,4 @@ jobs:
- name: Run tests
run: |
yarn test:chat --browser ${{ matrix.browser }}
yarn test:inject
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test:e2e": "jest -c ./test/jest.e2e.config.ts",
"test:sec": "jest -c ./test/jest.security.config.ts",
"test:mig": "jest -c ./test/jest.migration.config.ts",
"test:inject": "jest -c ./test/jest.inject.config.ts",
"test:int:pg": "cross-env POSTGRESQL=true jest -c ./test/jest.integration.config.ts",
"test:e2e:pg": "cross-env POSTGRESQL=true jest -c ./test/jest.e2e.config.ts",
"test:sec:pg": "cross-env POSTGRESQL=true jest -c ./test/jest.security.config.ts",
Expand Down
17 changes: 17 additions & 0 deletions packages/inject/src/inject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { postMessageToParent } from '../../webchat/src/utils/webchatEvents'
import { isWebchatEvent } from './inject'

describe('Test inject', () => {
it('post correct messages', () => {
window.parent.postMessage = jest.fn()

postMessageToParent('LIFECYCLE.LOADED', undefined, 'bp-chat')

const data = { type: 'LIFECYCLE.LOADED', value: undefined, chatId: 'bp-chat' }

expect(window.parent.postMessage).toHaveBeenCalledTimes(1)
expect(window.parent.postMessage).toHaveBeenCalledWith(data, '*')

expect(isWebchatEvent({ data })).toBeTruthy()
})
})
8 changes: 5 additions & 3 deletions packages/inject/src/inject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, WebchatEvent, WebchatEventType } from '@botpress/webchat'
import { Config, WebchatEvent, WebchatEventType } from '../../webchat/src/typings'

import './inject.css'

Expand Down Expand Up @@ -176,7 +176,7 @@ function init(config: Config, targetSelector: string) {
}

function isWebchatEvent(message: any): message is WebchatEvent {
return message.data?.type !== ''
return message.data && typeof message.data.type === 'string' && typeof message.data.chatId === 'string'
}

window.addEventListener('message', function ({ data }) {
Expand All @@ -195,7 +195,7 @@ window.addEventListener('message', function ({ data }) {
}

const chatRef = _getChatRef(data.chatId)
const shouldFireEvent = chatRef.eventListener.topics.some((t) => t === '*' || t === data.type)
const shouldFireEvent = chatRef && chatRef.eventListener.topics.some((t) => t === '*' || t === data.type)
if (shouldFireEvent) {
chatRef.eventListener.handler(data)
}
Expand All @@ -209,3 +209,5 @@ window.botpressWebChat = {
sendPayload,
onEvent
}

export { isWebchatEvent }
1 change: 1 addition & 0 deletions packages/inject/test/cssmock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
10 changes: 10 additions & 0 deletions packages/inject/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.packages.json",
"compilerOptions": {
"rootDir": "..",
"paths": {
"@botpress/webchat": ["../../webchat/src/index.ts"]
}
},
"include": ["../test/**/*", "../src/**/*"]
}
30 changes: 30 additions & 0 deletions test/jest.inject.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Config } from '@jest/types'
import { pathsToModuleNameMapper } from 'ts-jest'
import ServerConfig from '../packages/server/test/tsconfig.json'

const config: Config.InitialOptions = {
preset: 'ts-jest',
projects: [
{
rootDir: 'packages/inject',
testMatch: ['<rootDir>/**/*.test.ts'],
displayName: { name: 'Server', color: 'white' },
testEnvironment: 'jsdom',
transform: {
'^.+\\.tsx?$': require.resolve('ts-jest')
},
globals: {
'ts-jest': {
tsconfig: '<rootDir>/test/tsconfig.json'
}
},
clearMocks: true,
moduleNameMapper: {
...pathsToModuleNameMapper(ServerConfig.compilerOptions.paths, { prefix: '<rootDir>/test/' }),
'\\.(css|less)$': '<rootDir>/test/cssmock.js'
}
}
]
}

export default config

0 comments on commit 87a8ef5

Please sign in to comment.