-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): split and sort routing tests
- Loading branch information
1 parent
765c569
commit a0eb609
Showing
14 changed files
with
1,098 additions
and
1,066 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
2 changes: 1 addition & 1 deletion
2
packages/botonic-core/tests/helpers.ts → ...ges/botonic-core/tests/helpers/parsing.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
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,17 @@ | ||
import { PATH_PAYLOAD_IDENTIFIER, PROVIDER, Session } from '../../src' | ||
|
||
export function testRoute(): any { | ||
return {} | ||
} | ||
|
||
export function testSession(): Session { | ||
return { | ||
user: { id: 'userid', provider: PROVIDER.DEV }, | ||
bot: { id: 'bot_id' }, | ||
is_first_interaction: true, | ||
__retries: 0, | ||
} | ||
} | ||
|
||
export const createPathPayload = (pathWithParams: string): string => | ||
`${PATH_PAYLOAD_IDENTIFIER}${pathWithParams}` |
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
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
75 changes: 75 additions & 0 deletions
75
packages/botonic-core/tests/routing/router.get-route-by-path.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,75 @@ | ||
import { Router } from '../../src' | ||
|
||
describe('TEST: Get route by path', () => { | ||
const externalRoutes = [ | ||
{ path: '', action: 'Flow1.2' }, | ||
{ path: 'child', action: 'ChildAction' }, | ||
] | ||
const router = new Router([ | ||
{ path: 'initial', action: 'Initial' }, | ||
{ | ||
path: 'flow-1', | ||
action: 'Flow1', | ||
childRoutes: [ | ||
{ | ||
path: '1', | ||
action: 'Flow1.1', | ||
childRoutes: [ | ||
{ path: '1', action: 'Flow1.1.1' }, | ||
{ path: '2', action: 'Flow1.1.2' }, | ||
{ path: '3', action: 'Flow1.1.3' }, | ||
], | ||
}, | ||
{ path: '2', childRoutes: externalRoutes }, | ||
{ | ||
path: '3', | ||
action: 'Flow1.3', | ||
childRoutes: [ | ||
{ path: '1', action: 'Flow1.3.1' }, | ||
{ path: '2', action: 'Flow1.3.2' }, | ||
{ path: '3', action: 'Flow1.3.3' }, | ||
], | ||
}, | ||
], | ||
}, | ||
{ path: '404', action: '404Action' }, | ||
]) | ||
|
||
it('path exists', () => { | ||
expect(router.getRouteByPath('initial')).toEqual({ | ||
path: 'initial', | ||
action: 'Initial', | ||
}) | ||
expect(router.getRouteByPath('flow-1/1')).toEqual({ | ||
path: '1', | ||
action: 'Flow1.1', | ||
childRoutes: [ | ||
{ path: '1', action: 'Flow1.1.1' }, | ||
{ path: '2', action: 'Flow1.1.2' }, | ||
{ path: '3', action: 'Flow1.1.3' }, | ||
], | ||
}) | ||
expect(router.getRouteByPath('flow-1/3/2')).toEqual({ | ||
action: 'Flow1.3.2', | ||
path: '2', | ||
}) | ||
}) | ||
it('path exists in composed child routes', () => { | ||
expect(router.getRouteByPath('flow-1/2')).toEqual({ | ||
path: '2', | ||
childRoutes: [ | ||
{ action: 'Flow1.2', path: '' }, | ||
{ action: 'ChildAction', path: 'child' }, | ||
], | ||
}) | ||
expect(router.getRouteByPath('flow-1/2/child')).toEqual({ | ||
path: 'child', | ||
action: 'ChildAction', | ||
}) | ||
}) | ||
it('path does not exist', () => { | ||
expect(router.getRouteByPath('')).toBeNull() | ||
expect(router.getRouteByPath('foo')).toBeNull() | ||
expect(router.getRouteByPath('flow-1/3/2/6')).toBeNull() | ||
}) | ||
}) |
172 changes: 172 additions & 0 deletions
172
packages/botonic-core/tests/routing/router.match-route.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,172 @@ | ||
import { BotRequest, Input } from '../../src' | ||
import { Router } from '../../src/routing' | ||
import { testRoute, testSession } from '../helpers/routing' | ||
|
||
const textInput: Input = { type: 'text', text: 'hi' } | ||
const textInputComplex: Input = { type: 'text', text: 'Cömplêx input &% 🚀' } | ||
const textPayloadInput: Input = { type: 'text', text: 'hi', payload: 'foo' } | ||
const postbackInput: Input = { type: 'postback', payload: 'foo' } | ||
|
||
const audioInput: Input = { | ||
type: 'audio', | ||
src: 'data:audio/mpeg;base64,iVBORw0KG', | ||
} | ||
const documentInput: Input = { | ||
type: 'document', | ||
src: 'data:application/pdf;base64,iVBORw0KG', | ||
} | ||
const imageInput: Input = { | ||
type: 'image', | ||
src: 'data:image/png;base64,iVBORw0KG', | ||
} | ||
const videoInput: Input = { | ||
type: 'video', | ||
src: 'data:video/mp4;base64,iVBORw0KG', | ||
} | ||
|
||
const requestInput: BotRequest = { | ||
input: textInput, | ||
session: { ...testSession(), organization: 'myOrg' }, | ||
lastRoutePath: 'initial', | ||
} | ||
|
||
describe('TEST: Match route by MATCHER <> INPUT', () => { | ||
const router = new Router([]) | ||
const matchTextProp = (matcher, textInput) => | ||
router.matchRoute( | ||
testRoute(), | ||
'text', | ||
matcher, | ||
textInput, | ||
testSession(), | ||
null | ||
) | ||
const matchPayloadProp = (matcher, payload) => | ||
router.matchRoute( | ||
testRoute(), | ||
'payload', | ||
matcher, | ||
payload, | ||
testSession(), | ||
null | ||
) | ||
const matchTypeProp = (type, input) => | ||
router.matchRoute(testRoute(), 'type', type, input, testSession(), null) | ||
const matchRequestProp = (matcher, request) => | ||
router.matchRoute( | ||
testRoute(), | ||
'request', | ||
matcher, | ||
request.input, | ||
request.session, | ||
request.lastRoutePath | ||
) | ||
it('text <> text', () => { | ||
expect(matchTextProp('hi', textInput)).toBeTruthy() | ||
expect(matchTextProp('hii', textInput)).toBeFalsy() | ||
expect(matchTextProp('bye', textInput)).toBeFalsy() | ||
expect(matchTextProp('', textInput)).toBeFalsy() | ||
expect(matchTextProp(null, textInput)).toBeFalsy() | ||
expect(matchTextProp('Cömplêx input &% 🚀', textInputComplex)).toBeTruthy() | ||
expect(matchTextProp(' Cömplêx input &% 🚀', textInputComplex)).toBeFalsy() // has a space at the beginning | ||
}) | ||
it('regex <> text', () => { | ||
expect(matchTextProp(/hi/, textInput)).toBeTruthy() | ||
expect(matchTextProp(/bye/, textInput)).toBeFalsy() | ||
expect(matchTextProp(/🚀/, textInputComplex)).toBeTruthy() | ||
expect(matchTextProp(/complex/, textInputComplex)).toBeFalsy() | ||
}) | ||
it('function <> text', () => { | ||
expect(matchTextProp(v => v.startsWith('hi'), textInput)).toBeTruthy() | ||
expect(matchTextProp(v => !v.startsWith('hi'), textInput)).toBeFalsy() | ||
}) | ||
it('input <> text', () => { | ||
expect( | ||
router.matchRoute( | ||
testRoute(), | ||
'input', | ||
i => i.text.startsWith('hi'), | ||
textInput, | ||
testSession(), | ||
null | ||
) | ||
).toBeTruthy() | ||
expect( | ||
router.matchRoute( | ||
testRoute(), | ||
'input', | ||
i => !i.text.startsWith('hi'), | ||
textInput, | ||
testSession(), | ||
null | ||
) | ||
).toBeFalsy() | ||
}) | ||
it('text <> text payload', () => { | ||
expect(matchPayloadProp('foo', textPayloadInput)).toBeTruthy() | ||
expect(matchPayloadProp('fooo', textPayloadInput)).toBeFalsy() | ||
expect(matchPayloadProp('bar', textPayloadInput)).toBeFalsy() | ||
expect(matchPayloadProp('', textPayloadInput)).toBeFalsy() | ||
expect(matchPayloadProp(null, textPayloadInput)).toBeFalsy() | ||
}) | ||
it('regex <> text payload', () => { | ||
expect(matchPayloadProp(/foo/, textPayloadInput)).toBeTruthy() | ||
expect(matchPayloadProp(/bar/, textPayloadInput)).toBeFalsy() | ||
}) | ||
it('function <> text payload', () => { | ||
expect( | ||
matchPayloadProp(v => v.startsWith('fo'), textPayloadInput) | ||
).toBeTruthy() | ||
expect( | ||
matchPayloadProp(v => !v.startsWith('fo'), textPayloadInput) | ||
).toBeFalsy() | ||
}) | ||
it('text <> postback', () => { | ||
expect(matchPayloadProp('foo', postbackInput)).toBeTruthy() | ||
expect(matchPayloadProp('fooo', postbackInput)).toBeFalsy() | ||
expect(matchPayloadProp('bar', postbackInput)).toBeFalsy() | ||
expect(matchPayloadProp('', postbackInput)).toBeFalsy() | ||
expect(matchPayloadProp(null, postbackInput)).toBeFalsy() | ||
}) | ||
it('regex <> postback', () => { | ||
expect(matchPayloadProp(/foo/, postbackInput)).toBeTruthy() | ||
expect(matchPayloadProp(/bar/, postbackInput)).toBeFalsy() | ||
}) | ||
it('function <> postback', () => { | ||
expect( | ||
matchPayloadProp(v => v.startsWith('fo'), postbackInput) | ||
).toBeTruthy() | ||
expect( | ||
matchPayloadProp(v => !v.startsWith('fo'), postbackInput) | ||
).toBeFalsy() | ||
}) | ||
it('function <> request', () => { | ||
expect( | ||
matchRequestProp( | ||
request => | ||
request.input.text === 'hi' && | ||
request.session.organization === 'myOrg' && | ||
request.lastRoutePath === 'initial', | ||
requestInput | ||
) | ||
).toBeTruthy() | ||
expect( | ||
matchRequestProp( | ||
request => | ||
request.input.text === 'hello' && | ||
request.session.organization === 'myOrg' && | ||
request.lastRoutePath === 'initial', | ||
requestInput | ||
) | ||
).toBeFalsy() | ||
}) | ||
it('type <> audio, document, image, video', () => { | ||
expect(matchTypeProp('audio', audioInput)).toBeTruthy() | ||
expect(matchTypeProp('document', documentInput)).toBeTruthy() | ||
expect(matchTypeProp('image', imageInput)).toBeTruthy() | ||
expect(matchTypeProp('video', videoInput)).toBeTruthy() | ||
}) | ||
it('type <> other inputs', () => { | ||
expect(matchTypeProp(/.*/, { type: 'anyOtherInput' })).toBeTruthy() | ||
}) | ||
}) |
Oops, something went wrong.