Repository: Yoctol/bottender
Pull request #482 (2019-10-17 04:33:45)

feat: router props propagate
allow router to receive props and propagate

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

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

```diff
@@             Coverage Diff             @@
##           develop     #482      +/-   ##
===========================================
+ Coverage    82.14%   82.15%   +<.01%     
===========================================
  Files           97       97              
  Lines         3927     3928       +1     
  Branches       933      944      +11     
===========================================
+ Hits          3226     3227       +1     
+ Misses         669      663       -6     
- Partials        32       38       +6
```


| [Impacted Files](https://codecov.io/gh/Yoctol/bottender/pull/482?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/bottender/src/router/index.ts](https://codecov.io/gh/Yoctol/bottender/pull/482/diff?src=pr&el=tree#diff-cGFja2FnZXMvYm90dGVuZGVyL3NyYy9yb3V0ZXIvaW5kZXgudHM=) | `87.8% <75%> (+0.3%)` | :arrow_up: |
| [packages/bottender/src/chain.ts](https://codecov.io/gh/Yoctol/bottender/pull/482/diff?src=pr&el=tree#diff-cGFja2FnZXMvYm90dGVuZGVyL3NyYy9jaGFpbi50cw==) | `76.92% <0%> (ø)` | :arrow_up: |
| [packages/create-bottender-app/src/shared/log.ts](https://codecov.io/gh/Yoctol/bottender/pull/482/diff?src=pr&el=tree#diff-cGFja2FnZXMvY3JlYXRlLWJvdHRlbmRlci1hcHAvc3JjL3NoYXJlZC9sb2cudHM=) | `0% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Yoctol/bottender/pull/482?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/482?src=pr&el=footer). Last update [ad03ee6...97652a2](https://codecov.io/gh/Yoctol/bottender/pull/482?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/bottender/src/router/index.ts b/packages/bottender/src/router/index.ts
index b53e4ac8..80bd2c7c 100644
--- a/packages/bottender/src/router/index.ts
+++ b/packages/bottender/src/router/index.ts
@@ -16,16 +16,19 @@ type Route = {
 };
 
 function router(routes: Route[]) {
-  return async function Router(context: Context, { next }: Props = {}) {
+  return async function Router(context: Context, props: Props = {}) {
     for (const r of routes) {
       // eslint-disable-next-line no-await-in-loop
       const match = await r.predicate(context);
       if (match) {
-        return r.action.bind(null, context, { next, match });
+        return r.action.bind(null, context, {
+          ...props,
+          match,
+        });
       }
     }
 
-    return next;
+    return props.next;
   };
 }
 


Test patch:
diff --git a/packages/bottender/src/router/__tests__/router.spec.ts b/packages/bottender/src/router/__tests__/router.spec.ts
index fb1348c2..77733ef2 100644
--- a/packages/bottender/src/router/__tests__/router.spec.ts
+++ b/packages/bottender/src/router/__tests__/router.spec.ts
@@ -104,6 +104,33 @@ describe('#router', () => {
 
     expect(context.sendText).toBeCalledWith('next');
   });
+
+  it('should work with props', async () => {
+    let receivedContext;
+    let receivedProps;
+    const action = (ctx, props) => {
+      receivedContext = ctx;
+      receivedProps = props;
+    };
+
+    const Router = router([
+      {
+        predicate: () => true,
+        action,
+      },
+    ]);
+
+    const app = run(Router);
+    const context = textContext('hi');
+    const props = { a: 1 };
+    await app(context, props);
+
+    expect(receivedContext).toEqual(context);
+    expect(receivedProps).toEqual({
+      a: 1,
+      match: true,
+    });
+  });
 });
 
 describe('#route', () => {