From 5b57717933136d3d080286eccf6433d3bfa6e7e9 Mon Sep 17 00:00:00 2001 From: Johnson Liang Date: Mon, 6 Nov 2023 13:57:57 +0800 Subject: [PATCH] fix(webhook): fix askingArticleSource test --- src/lib/__mocks__/ga.ts | 1 + ...ce.test.js => askingArticleSource.test.ts} | 36 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) rename src/webhook/handlers/__tests__/{askingArticleSource.test.js => askingArticleSource.test.ts} (92%) diff --git a/src/lib/__mocks__/ga.ts b/src/lib/__mocks__/ga.ts index 9b5b542b..14ff759a 100644 --- a/src/lib/__mocks__/ga.ts +++ b/src/lib/__mocks__/ga.ts @@ -22,4 +22,5 @@ const ga = Object.assign( } ); +export type MockedGa = typeof ga; export default ga; diff --git a/src/webhook/handlers/__tests__/askingArticleSource.test.js b/src/webhook/handlers/__tests__/askingArticleSource.test.ts similarity index 92% rename from src/webhook/handlers/__tests__/askingArticleSource.test.js rename to src/webhook/handlers/__tests__/askingArticleSource.test.ts index 1c500e06..8bfe7519 100644 --- a/src/webhook/handlers/__tests__/askingArticleSource.test.js +++ b/src/webhook/handlers/__tests__/askingArticleSource.test.ts @@ -1,20 +1,26 @@ jest.mock('src/lib/ga'); -import ga from 'src/lib/ga'; +import originalGa from 'src/lib/ga'; +import type { MockedGa } from 'src/lib/__mocks__/ga'; import askingArticleSource from '../askingArticleSource'; import { POSTBACK_YES, POSTBACK_NO } from '../utils'; +import { ChatbotPostbackHandlerParams } from 'src/types/chatbotState'; + +const ga = originalGa as MockedGa; beforeEach(() => { ga.clearAllMocks(); }); it('throws on incorrect input', async () => { - const incorrectParam = { - data: { searchedText: 'foo' }, - state: 'ASKING_ARTICLE_SOURCE', - event: { + const incorrectParam: ChatbotPostbackHandlerParams = { + data: { sessionId: 0, searchedText: 'foo' }, + postbackData: { + sessionId: 0, + state: 'ASKING_ARTICLE_SOURCE', input: 'Wrong', }, + userId: 'the-user-id', }; expect(askingArticleSource(incorrectParam)).rejects.toMatchInlineSnapshot( @@ -23,12 +29,14 @@ it('throws on incorrect input', async () => { }); it('returns instructions if user did not forward the whole message', async () => { - const didNotForwardParam = { - data: { searchedText: 'foo', sessionId: 'the-session-id' }, - state: 'ASKING_ARTICLE_SOURCE', - event: { + const didNotForwardParam: ChatbotPostbackHandlerParams = { + data: { searchedText: 'foo', sessionId: 0 }, + postbackData: { + sessionId: 0, + state: 'ASKING_ARTICLE_SOURCE', input: POSTBACK_NO, }, + userId: 'the-user-id', }; const { replies } = await askingArticleSource(didNotForwardParam); @@ -203,12 +211,14 @@ it('returns instructions if user did not forward the whole message', async () => }); it('sends user submission consent if user forwarded the whole message', async () => { - const didForwardParam = { - data: { searchedText: 'foo', sessionId: 'the-session-id' }, - state: 'ASKING_ARTICLE_SOURCE', - event: { + const didForwardParam: ChatbotPostbackHandlerParams = { + data: { searchedText: 'foo', sessionId: 0 }, + postbackData: { + sessionId: 0, + state: 'ASKING_ARTICLE_SOURCE', input: POSTBACK_YES, }, + userId: 'the-user-id', }; const { replies } = await askingArticleSource(didForwardParam);