Skip to content

Commit

Permalink
dev: context get/set/delete persistent level test case draft
Browse files Browse the repository at this point in the history
  • Loading branch information
hch903 committed Jun 9, 2020
1 parent bfc24e6 commit 2b4b7da
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/bottender/src/messenger/MessengerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,87 @@ class MessengerContext extends Context<MessengerClient, MessengerEvent> {
);
}

async getPersistentMenu(
options: MessengerTypes.SendOption = {}
): Promise<MessengerTypes.PersistentMenu | undefined> {
if (!this._session) {
warning(
false,
`getPersistentMenu: should not be called in context without session`
);
return;
}

if (this._batchQueue) {
return this._batchQueue.push(
MessengerBatch.getUserPersistentMenu(
this._session.user.id,
this._getSendMethodOptions(options)
)
);
}

return this._client.getUserPersistentMenu(
this._session.user.id,
this._getSendMethodOptions(options)
);
}

async setPersistentMenu(
attrs: MessengerTypes.PersistentMenu,
options: MessengerTypes.SendOption = {}
): Promise<MessengerTypes.MutationSuccessResponse | undefined> {
if (!this._session) {
warning(
false,
`setPersistentMenu: should not be called in context without session`
);
return;
}

if (this._batchQueue) {
return this._batchQueue.push(
MessengerBatch.setUserPersistentMenu(
this._session.user.id,
attrs,
this._getSendMethodOptions(options)
)
);
}

return this._client.setUserPersistentMenu(
this._session.user.id,
attrs,
this._getSendMethodOptions(options)
);
}

async deletePersistentMenu(
options: MessengerTypes.SendOption = {}
): Promise<MessengerTypes.MutationSuccessResponse | undefined> {
if (!this._session) {
warning(
false,
`deletePersistentMenu: should not be called in context without session`
);
return;
}

if (this._batchQueue) {
return this._batchQueue.push(
MessengerBatch.deleteUserPersistentMenu(
this._session.user.id,
this._getSendMethodOptions(options)
)
);
}

return this._client.deleteUserPersistentMenu(
this._session.user.id,
this._getSendMethodOptions(options),
);
}

/**
* Sender Actions
*
Expand Down
124 changes: 124 additions & 0 deletions packages/bottender/src/messenger/__tests__/MessengerContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,130 @@ describe('#getUserProfile', () => {
});
});

describe('Persistent Menu', () => {
describe('#getPersistentMenu', () => {
it('should call client getUserPersistentMenu', async () => {
const { context, client, session } = setup();

const persistentMenu = [{
locale: 'default',
composer_input_disabled: false,
call_to_actions: [
{
type: 'postback',
title: 'Restart Conversation',
payload: 'RESTART',
},
{
type: 'web_url',
title: 'Powered by ALOHA.AI, Yoctol',
url: 'https://www.yoctol.com/',
},
],
}];

client.getUserPersistentMenu.mockResolvedValue(persistentMenu);

const result = await context.getPersistentMenu();

expect(client.getUserPersistentMenu).toBeCalledWith(session.user.id, {
accessToken: undefined,
});
expect(result).toEqual(persistentMenu);
});

it('should call warning and not to send if dont have session', async () => {
const { context, client } = setup({ session: false });

await context.getPersistentMenu();

expect(warning).toBeCalledWith(
false,
'getPersistentMenu: should not be called in context without session'
);
expect(client.getPersistentMenu).not.toBeCalled();
});
})

describe('#setPersistentMenu', () => {
it('should call client setPersistentMenu', async () => {
const { context, client, session } = setup();

const reply = {
result: 'success',
};

const persistentMenu = [{
locale: 'default',
composer_input_disabled: false,
call_to_actions: [
{
type: 'postback',
title: 'Restart Conversation',
payload: 'RESTART',
},
{
type: 'web_url',
title: 'Powered by ALOHA.AI, Yoctol',
url: 'https://www.yoctol.com/',
},
],
}];

const result = await context.setPersistentMenu(persistentMenu);

expect(client.setUserPersistentMenu).toBeCalledWith(session.user.id,
persistentMenu, {
accessToken: undefined,
});

expect(result).toEqual(reply);
});

it('should call warning and not to send if dont have session', async () => {
const { context, client } = setup({ session: false });

await context.setPersistentMenu();

expect(warning).toBeCalledWith(
false,
'setPersistentMenu: should not be called in context without session'
);
expect(client.setPersistentMenu).not.toBeCalled();
});
})

describe('#deletePersistentMenu', () => {
it('should call client deletePersistentMenu', async () => {
const { context, client, session } = setup();

const reply = {
result: 'success',
};

const result = await context.deletePersistentMenu();

expect(client.deleteUserPersistentMenu).toBeCalledWith(session.user.id, {
accessToken: undefined,
});

expect(result).toEqual(reply);
});

it('should call warning and not to send if dont have session', async () => {
const { context, client } = setup({ session: false });

await context.deletePersistentMenu();

expect(warning).toBeCalledWith(
false,
'deletePersistentMenu: should not be called in context without session'
);
expect(client.deletePersistentMenu).not.toBeCalled();
});
})
})

describe('#passThreadControl', () => {
it('should call to pass user thread control to other app', async () => {
const { context, client, session } = setup();
Expand Down

0 comments on commit 2b4b7da

Please sign in to comment.