Repository: Yoctol/bottender
Pull request #326 (2018-08-22 04:28:40)

pass mockPlatform to context
pass mockPlatform to context, makes `context.platform` consistent with `context.session.platform` 

codecov-io: # [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=h1) Report
> Merging [#326](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=desc) into [master](https://codecov.io/gh/Yoctol/bottender/commit/90fce44a16ea0b9805411fc74458b1f1a089d013?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Yoctol/bottender/pull/326/graphs/tree.svg?token=JX3EGtxYH3&src=pr&width=650&height=150)](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #326      +/-   ##
==========================================
+ Coverage   90.29%   90.29%   +<.01%     
==========================================
  Files         112      112              
  Lines        3307     3308       +1     
  Branches      784      785       +1     
==========================================
+ Hits         2986     2987       +1     
  Misses        281      281              
  Partials       40       40
```


| [Impacted Files](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/bot/ConsoleConnector.js](https://codecov.io/gh/Yoctol/bottender/pull/326/diff?src=pr&el=tree#diff-c3JjL2JvdC9Db25zb2xlQ29ubmVjdG9yLmpz) | `100% <ø> (ø)` | :arrow_up: |
| [src/context/ConsoleContext.js](https://codecov.io/gh/Yoctol/bottender/pull/326/diff?src=pr&el=tree#diff-c3JjL2NvbnRleHQvQ29uc29sZUNvbnRleHQuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=footer). Last update [90fce44...a795778](https://codecov.io/gh/Yoctol/bottender/pull/326?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/bot/Bot.js b/src/bot/Bot.js
index 5eabea80..bf3b9413 100644
--- a/src/bot/Bot.js
+++ b/src/bot/Bot.js
@@ -13,6 +13,7 @@ import { type FunctionalHandler, type Builder } from '../handlers/Handler';
 import { type Connector } from './Connector';
 
 const debugRequest = debug('bottender:request');
+const debugResponse = debug('bottender:response');
 const debugSessionRead = debug('bottender:session:read');
 const debugSessionWrite = debug('bottender:session:write');
 
@@ -205,8 +206,13 @@ export default class Bot {
           console.error(err);
         }
 
-        // TODO: Any chances to merge multiple response from context?
-        return contexts[0].response;
+        // TODO: Any chances to merge multiple responses from context?
+        const response = contexts[0].response;
+        if (response && typeof response === 'object') {
+          debugResponse('Outgoing response:');
+          debugResponse(JSON.stringify(response, null, 2));
+        }
+        return response;
       }
       promises
         .then(() => {
diff --git a/src/bot/ConsoleConnector.js b/src/bot/ConsoleConnector.js
index b9bff51e..252700f3 100644
--- a/src/bot/ConsoleConnector.js
+++ b/src/bot/ConsoleConnector.js
@@ -80,6 +80,7 @@ export default class ConsoleConnector implements Connector<ConsoleRequestBody> {
       ...params,
       client: this._client,
       fallbackMethods: this._fallbackMethods,
+      mockPlatform: this._platform,
     });
   }
 }
diff --git a/src/context/ConsoleContext.js b/src/context/ConsoleContext.js
index d86144cc..d29ac406 100644
--- a/src/context/ConsoleContext.js
+++ b/src/context/ConsoleContext.js
@@ -15,6 +15,7 @@ type Options = {|
   initialState: ?Object,
   requestContext: ?Object,
   fallbackMethods: boolean,
+  mockPlatform: string,
 |};
 
 const methodBlackList = [
@@ -31,6 +32,8 @@ export default class ConsoleContext extends Context implements PlatformContext {
 
   _fallbackMethods: boolean = false;
 
+  _mockPlatform: string = 'console';
+
   constructor({
     client,
     event,
@@ -38,8 +41,10 @@ export default class ConsoleContext extends Context implements PlatformContext {
     initialState,
     requestContext,
     fallbackMethods,
+    mockPlatform,
   }: Options) {
     super({ client, event, session, initialState, requestContext });
+    this._mockPlatform = mockPlatform;
     this._fallbackMethods = fallbackMethods;
     if (fallbackMethods) {
       // $FlowExpectedError
@@ -67,7 +72,7 @@ export default class ConsoleContext extends Context implements PlatformContext {
    *
    */
   get platform(): string {
-    return 'console';
+    return this._mockPlatform || 'console';
   }
 
   /**


Test patch:
diff --git a/src/context/__tests__/ConsoleContext.spec.js b/src/context/__tests__/ConsoleContext.spec.js
index 5deb7d10..759c5ea6 100644
--- a/src/context/__tests__/ConsoleContext.spec.js
+++ b/src/context/__tests__/ConsoleContext.spec.js
@@ -29,7 +29,9 @@ const userSession = {
   },
 };
 
-const setup = ({ session, fallbackMethods } = { session: userSession }) => {
+const setup = (
+  { session, fallbackMethods, mockPlatform } = { session: userSession }
+) => {
   const client = {
     sendText: jest.fn(),
   };
@@ -38,6 +40,7 @@ const setup = ({ session, fallbackMethods } = { session: userSession }) => {
     event: new ConsoleEvent(rawEvent),
     session,
     fallbackMethods: fallbackMethods || false,
+    mockPlatform,
   });
   return {
     client,
@@ -51,11 +54,16 @@ it('be defined', () => {
   expect(context).toBeDefined();
 });
 
-it('#platform to be `console`', () => {
+it('#platform to be `console` by default', () => {
   const { context } = setup();
   expect(context.platform).toBe('console');
 });
 
+it('#platform to be `console` by mockPlatform', () => {
+  const { context } = setup({ mockPlatform: 'messenger' });
+  expect(context.platform).toBe('messenger');
+});
+
 it('get #session works', () => {
   const { context, session } = setup();
   expect(context.session).toBe(session);