Repository: Yoctol/bottender
Pull request #23 (2017-11-15 01:57:46)

implement richmenu api methods on context
- getLinkedRichMenu
- linkRichMenu
- unlinkRichMenu


Patch:
diff --git a/src/context/LineContext.js b/src/context/LineContext.js
index 1c793813..64d9c5d3 100644
--- a/src/context/LineContext.js
+++ b/src/context/LineContext.js
@@ -72,6 +72,48 @@ class LineContext extends Context implements PlatformContext {
     const { type } = this._session;
     return this._client.pushText(this._session[type].id, text);
   }
+
+  /**
+   * Gets the ID of the rich menu linked to the user.
+   *
+   */
+  async getLinkedRichMenu(): Promise<any> {
+    if (this._session && this._session.user) {
+      return this._client.getLinkedRichMenu(this._session.user.id);
+    }
+    warning(
+      false,
+      'getLinkedRichMenu: should not be called in context without session user'
+    );
+  }
+
+  /**
+   * Links a rich menu to the user.
+   *
+   */
+  async linkRichMenu(richMenuId: string): Promise<any> {
+    if (this._session && this._session.user) {
+      return this._client.linkRichMenu(this._session.user.id, richMenuId);
+    }
+    warning(
+      false,
+      'linkRichMenu: should not be called in context without session user'
+    );
+  }
+
+  /**
+   * Unlinks a rich menu from the user.
+   *
+   */
+  async unlinkRichMenu(): Promise<any> {
+    if (this._session && this._session.user) {
+      return this._client.unlinkRichMenu(this._session.user.id);
+    }
+    warning(
+      false,
+      'unlinkRichMenu: should not be called in context without session user'
+    );
+  }
 }
 
 const types = [


Test patch:
diff --git a/src/context/__tests__/LineContext.spec.js b/src/context/__tests__/LineContext.spec.js
index f9cfadf9..aa1c6b73 100644
--- a/src/context/__tests__/LineContext.spec.js
+++ b/src/context/__tests__/LineContext.spec.js
@@ -50,6 +50,9 @@ const setup = ({ session } = { session: userSession }) => {
     pushConfirmTemplate: jest.fn(),
     pushImagemap: jest.fn(),
     pushCarouselTemplate: jest.fn(),
+    getLinkedRichMenu: jest.fn(),
+    linkRichMenu: jest.fn(),
+    unlinkRichMenu: jest.fn(),
   };
   const context = new LineContext({
     client,
@@ -791,6 +794,67 @@ describe('#sendCarouselTemplate', () => {
   });
 });
 
+describe('#getLinkedRichMenu', () => {
+  it('should call client.getLinkedRichMenu', async () => {
+    const { context, client, session } = setup();
+
+    client.getLinkedRichMenu.mockReturnValue(
+      Promise.resolve({
+        richMenuId: 'richMenuId',
+      })
+    );
+
+    const result = await context.getLinkedRichMenu();
+
+    expect(client.getLinkedRichMenu).toBeCalledWith(session.user.id);
+    expect(result.richMenuId).toEqual('richMenuId');
+  });
+
+  it('should warn without user', async () => {
+    const { context } = setup({ session: {} });
+
+    await context.getLinkedRichMenu();
+
+    expect(warning).toBeCalled();
+  });
+});
+
+describe('#linkRichMenu', () => {
+  it('should call client.linkRichMenu', async () => {
+    const { context, client, session } = setup();
+
+    await context.linkRichMenu('richMenuId');
+
+    expect(client.linkRichMenu).toBeCalledWith(session.user.id, 'richMenuId');
+  });
+
+  it('should warn without user', async () => {
+    const { context } = setup({ session: {} });
+
+    await context.linkRichMenu('richMenuId');
+
+    expect(warning).toBeCalled();
+  });
+});
+
+describe('#unlinkRichMenu', () => {
+  it('should call client.unlinkRichMenu', async () => {
+    const { context, client, session } = setup();
+
+    await context.unlinkRichMenu();
+
+    expect(client.unlinkRichMenu).toBeCalledWith(session.user.id);
+  });
+
+  it('should warn without user', async () => {
+    const { context } = setup({ session: {} });
+
+    await context.unlinkRichMenu();
+
+    expect(warning).toBeCalled();
+  });
+});
+
 describe('#typing', () => {
   it('avoid delay 0', async () => {
     const { context } = setup();