Skip to content

Commit

Permalink
fix(webhook): fix askingArticleSource test
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Nov 6, 2023
1 parent 9d74cea commit 5b57717
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/lib/__mocks__/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ const ga = Object.assign(
}
);

export type MockedGa = typeof ga;
export default ga;
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5b57717

Please sign in to comment.