Repository: Yoctol/bottender
Pull request #214 (2018-03-19 11:21:32)

[Telegram] imeplement context.answerShippingQuery and context.answerP…
```js
await context.answerShippingQuery(true);
await context.answerPreCheckoutQuery(true);
```


Patch:
diff --git a/src/context/TelegramContext.js b/src/context/TelegramContext.js
index 16509fb4..907aca36 100644
--- a/src/context/TelegramContext.js
+++ b/src/context/TelegramContext.js
@@ -101,6 +101,38 @@ class TelegramContext extends Context implements PlatformContext {
     return null;
   }
 
+  async answerShippingQuery(ok: boolean, options?: {}) {
+    if (!this._event.isShippingQuery) {
+      warning(
+        false,
+        'answerShippingQuery: should only be called to answer ShippingQuery event'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const shippingQueryId = (this._event.shippingQuery: any).id;
+
+    return this._client.answerShippingQuery(shippingQueryId, ok, options);
+  }
+
+  async answerPreCheckoutQuery(ok: boolean, options?: {}) {
+    if (!this._event.isPreCheckoutQuery) {
+      warning(
+        false,
+        'answerPreCheckoutQuery: should only be called to answer PreCheckoutQuery event'
+      );
+      return;
+    }
+
+    this._isHandled = true;
+
+    const preCheckoutQueryId = (this._event.preCheckoutQuery: any).id;
+
+    return this._client.answerPreCheckoutQuery(preCheckoutQueryId, ok, options);
+  }
+
   async answerInlineQuery(results: Array<Object>, options?: {}) {
     if (!this._event.isInlineQuery) {
       warning(


Test patch:
diff --git a/src/context/__tests__/TelegramContext.spec.js b/src/context/__tests__/TelegramContext.spec.js
index f176312c..e0e45b41 100644
--- a/src/context/__tests__/TelegramContext.spec.js
+++ b/src/context/__tests__/TelegramContext.spec.js
@@ -34,6 +34,8 @@ const createMockTelegramClient = () => ({
   sendGame: jest.fn(),
   setGameScore: jest.fn(),
   getGameHighScores: jest.fn(),
+  answerShippingQuery: jest.fn(),
+  answerPreCheckoutQuery: jest.fn(),
   answerInlineQuery: jest.fn(),
 });
 
@@ -493,6 +495,116 @@ describe('#getGameHighScores', () => {
   });
 });
 
+describe('#answerShippingQuery', () => {
+  const shippingQuery = {
+    update_id: 141921690,
+    shipping_query: {
+      id: '123',
+      from: {
+        id: 427770117,
+        first_name: 'user_first',
+        last_name: 'user_last',
+        language_code: 'en',
+      },
+      invoice_payload: 'bot payload',
+      shipping_address: {
+        country_code: 'US',
+        state: 'New York',
+        city: 'New York',
+        street_line1: 'xx',
+        street_line2: 'xx',
+        post_code: '10001',
+      },
+    },
+  };
+
+  it('should to call client.answerShippingQuery', async () => {
+    const { context, client } = setup({ rawEvent: shippingQuery });
+
+    const response = {
+      ok: true,
+      result: true,
+    };
+
+    client.answerShippingQuery.mockReturnValue(Promise.resolve(response));
+
+    const result = await context.answerShippingQuery(true);
+
+    expect(client.answerShippingQuery).toBeCalledWith('123', true, undefined);
+    expect(result).toEqual(response);
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup({ rawEvent: shippingQuery });
+
+    await context.answerShippingQuery(true);
+
+    expect(context.isHandled).toBe(true);
+  });
+
+  it('should not call answerShippingQuery method if event type is not ShippingQuery', async () => {
+    const { context, client } = setup();
+
+    await context.answerShippingQuery(true);
+
+    expect(client.answerShippingQuery).not.toBeCalled();
+  });
+});
+
+describe('#answerPreCheckoutQuery', () => {
+  const preCheckoutQuery = {
+    update_id: 141921690,
+    pre_checkout_query: {
+      id: '123',
+      from: {
+        id: 427770117,
+        first_name: 'user_first',
+        last_name: 'user_last',
+        language_code: 'en',
+      },
+      currency: 'USD',
+      total_amount: 145,
+      invoice_payload: 'bot payload',
+    },
+  };
+
+  it('should to call client.answerPreCheckoutQuery', async () => {
+    const { context, client } = setup({ rawEvent: preCheckoutQuery });
+
+    const response = {
+      ok: true,
+      result: true,
+    };
+
+    client.answerPreCheckoutQuery.mockReturnValue(Promise.resolve(response));
+
+    const result = await context.answerPreCheckoutQuery(true);
+
+    expect(client.answerPreCheckoutQuery).toBeCalledWith(
+      '123',
+      true,
+      undefined
+    );
+    expect(result).toEqual(response);
+  });
+
+  it('should mark context as handled', async () => {
+    const { context } = setup({ rawEvent: preCheckoutQuery });
+
+    await context.answerPreCheckoutQuery(true);
+
+    expect(context.isHandled).toBe(true);
+  });
+
+  it('should not call answerPreCheckoutQuery method if event type is not PreCheckoutQuery', async () => {
+    const { context, client } = setup();
+
+    await context.answerPreCheckoutQuery(true);
+
+    expect(client.answerPreCheckoutQuery).not.toBeCalled();
+  });
+});
+
 describe('#answerInlineQuery', () => {
   const inlineQuery = {
     update_id: 141921700,
@@ -509,6 +621,7 @@ describe('#answerInlineQuery', () => {
       offset: '',
     },
   };
+
   it('should to call client.answerInlineQuery', async () => {
     const { context, client } = setup({ rawEvent: inlineQuery });