Skip to content

Commit

Permalink
[lib] sendMessage batching unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed May 18, 2021
1 parent ec23199 commit 47942f3
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/lib/__tests__/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rollbar from 'src/lib/rollbar';
import sendMessage from '../sendMessage';

beforeEach(() => {
fetch.mockClear();
fetch.mockReset();
rollbar.error.mockClear();
});

Expand All @@ -29,8 +29,12 @@ it('send message using LINE Notify', () => {
`);
});

it('send message using multicast api', () => {
sendMessage.multicast(
it('send message using multicast api', async () => {
fetch.mockImplementation(() =>
Promise.resolve({ json: () => Promise.resolve({ status: 200 }) })
);

await sendMessage.multicast(
['userId1', 'userId2', 'userId3'],
[{ type: 'text', text: 'message' }]
);
Expand All @@ -49,6 +53,30 @@ it('send message using multicast api', () => {
],
]
`);

// Test batching
fetch.mockClear();
await sendMessage.multicast(
Array.from(Array(501)).map((_, id) => `user${id}`),
[{ type: 'text', text: 'message' }]
);

expect(fetch.mock.calls).toHaveLength(2);

// The snapshot of the "second batch", which should only contain user500
expect(fetch.mock.calls[1]).toMatchInlineSnapshot(`
Array [
"https://api.line.me/v2/bot/message/multicast",
Object {
"body": "{\\"to\\":[\\"user500\\"],\\"messages\\":[{\\"type\\":\\"text\\",\\"text\\":\\"message\\"}]}",
"headers": Object {
"Authorization": "Bearer ",
"Content-Type": "application/json",
},
"method": "POST",
},
]
`);
});

it('send message using push api', () => {
Expand Down

0 comments on commit 47942f3

Please sign in to comment.