@yoctol/kurator
Advanced tools
Comparing version 0.4.15 to 0.4.16
{ | ||
"name": "@yoctol/kurator", | ||
"license": "UNLICENSED", | ||
"version": "0.4.15", | ||
"version": "0.4.16", | ||
"main": "src/index.js", | ||
@@ -6,0 +6,0 @@ "files": [ |
const nock = require('nock'); | ||
const B = require('bottender-compose'); | ||
@@ -96,2 +97,31 @@ const Kurator = require('../Kurator'); | ||
describe('#registerAction', () => { | ||
it('should support local action', async () => { | ||
const { kurator } = await setup(); | ||
const context = { | ||
sendText: jest.fn(), | ||
}; | ||
kurator.registerAction('不要騙我', B.sendText('不要騙我')); | ||
const action = kurator.getAction('不要騙我'); | ||
await action(context); | ||
expect(context.sendText).toBeCalledWith('不要騙我'); | ||
}); | ||
it('should have displayName', async () => { | ||
const { kurator } = await setup(); | ||
kurator.registerAction('不要騙我', B.sendText('不要騙我')); | ||
expect(kurator.getAction('不要騙我')).toHaveProperty( | ||
'displayName', | ||
'不要騙我' | ||
); | ||
}); | ||
}); | ||
describe('#createBottenderMiddleware', () => { | ||
@@ -98,0 +128,0 @@ it('should support id link', async () => { |
@@ -79,13 +79,42 @@ const B = require('bottender-compose'); | ||
registerAction(name, fn) { | ||
// eslint-disable-next-line no-param-reassign | ||
fn.displayName = name; | ||
this._localActions[name] = fn; | ||
registerAction(actionId, fn) { | ||
if (!fn.displayName) { | ||
fn.displayName = actionId; // eslint-disable-line no-param-reassign | ||
} | ||
this._localActions[actionId] = fn; | ||
} | ||
getAction(actionId) { | ||
// TODO: fallback or warning | ||
getRemoteAction(actionId) { | ||
return this._remoteActions[actionId]; | ||
} | ||
getLocalAction(actionId) { | ||
return this._localActions[actionId]; | ||
} | ||
getActionFromAnyWhere(actionId) { | ||
return ( | ||
this.getRemoteAction(actionId) || | ||
this.getLocalAction(actionId) || | ||
B.warn(`Action ID: ${actionId} is not yet implemented`) | ||
); | ||
} | ||
getAction(actionId) { | ||
const wrappedAction = (context, ...otherArgs) => { | ||
const action = this.getActionFromAnyWhere(actionId); | ||
wrappedAction.displayName = action.displayName; | ||
return action(context, ...otherArgs); | ||
}; | ||
const action = this.getActionFromAnyWhere(actionId); | ||
if (action) { | ||
wrappedAction.displayName = action.displayName; | ||
} | ||
return wrappedAction; | ||
} | ||
createBottenderMiddleware() { | ||
@@ -92,0 +121,0 @@ return async (context, next) => { |
41759
1426