Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beforeEach(() => {
/* eslint-disable global-require */
Line = require('messaging-api-line').Line;
LineClient = require('messaging-api-line').LineClient;
LineContext = require('../LineContext').default;
LineEvent = require('../LineEvent').default;
sleep = require('delay');
warning = require('warning');
/* eslint-enable global-require */
});
beforeEach(() => {
/* eslint-disable global-require */
Line = require('messaging-api-line').Line;
LineClient = require('messaging-api-line').LineClient;
LineContext = require('../LineContext').default;
LineEvent = require('../LineEvent').default;
sleep = require('delay');
warning = require('warning');
/* eslint-enable global-require */
});
it('should delete all online rich menus if using force', async () => {
const ctx = setup(true);
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
},
{
richMenuId: '0987654321',
},
]);
await deleteLineMenu(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(print.mock.calls.length).toBe(1);
});
it('should not call deleteRichMenu when online rich menus and local rich menus are same', async () => {
const ctx = setup();
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
it('should exit when failed to find rich menu', async () => {
const ctx = setup(false);
LineClient.connect().getRichMenuList.mockResolvedValueOnce(null);
await deleteLineMenu(ctx);
expect(error).toBeCalled();
expect(process.exit).toBeCalled();
});
it('should print rich menus', async () => {
const ctx = setup();
LineClient.connect().getRichMenuList.mockResolvedValueOnce([
{
richMenuId: '1234567890',
size: {
width: 2500,
height: 1686,
},
selected: false,
name: 'Nice richmenu',
chatBarText: 'Tap here',
areas: [
{
bounds: {
x: 0,
y: 0,
width: 2500,
height: 1686,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=sell&itemid=321',
},
},
],
},
]);
await setLineMenus(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(LineClient.connect().createRichMenu.mock.calls.length).toBe(1);
expect(print.mock.calls.length).toBe(1);
expect(log.mock.calls.length).toBe(1);
});
});
y: 0,
width: 2500,
height: 1686,
},
action: {
type: 'postback',
data: 'action=sell&itemid=321',
},
},
],
},
]);
await setLineMenus(ctx);
expect(LineClient.connect().deleteRichMenu.mock.calls.length).toBe(2);
expect(LineClient.connect().createRichMenu.mock.calls.length).toBe(1);
expect(print.mock.calls.length).toBe(1);
expect(log.mock.calls.length).toBe(1);
});
});
export async function setLineMenus(_: CliContext) {
try {
const config = getConfig('line');
const { richMenus: localRichMenus } = config;
invariant(config.accessToken, 'accessToken is not found in config file');
const accessToken = config.accessToken;
const client = LineClient.connect(accessToken);
const onlineRichMenus = await client.getRichMenuList();
invariant(
onlineRichMenus,
`Failed to get ${bold('LINE rich menu')} response.`
);
const existedRichMenus = onlineRichMenus.map(richMenu =>
omit(richMenu, 'richMenuId')
);
const shouldDeleteRichMenus = differenceWith(
existedRichMenus,
localRichMenus,
isEqual
);
constructor(options: ConstructorOptions) {
const {
mapDestinationToAccessToken,
shouldBatch,
sendMethod,
skipProfile,
} = options;
if ('client' in options) {
this._client = options.client;
this._channelSecret = '';
} else {
const { accessToken, channelSecret, origin } = options;
this._client = LineClient.connect({
accessToken,
channelSecret,
origin,
});
this._channelSecret = channelSecret;
}
this._mapDestinationToAccessToken = mapDestinationToAccessToken || null;
this._shouldBatch = typeof shouldBatch === 'boolean' ? shouldBatch : true;
warning(
!sendMethod || sendMethod === 'reply' || sendMethod === 'push',
'sendMethod should be one of `reply` or `push`'
);
this._sendMethod = sendMethod || 'reply';