Skip to content

Commit

Permalink
Add tests for inline queries
Browse files Browse the repository at this point in the history
Added implementation of test events for inline queries.
Added test for inline queries.
  • Loading branch information
edloidas committed Sep 10, 2017
1 parent 217563d commit c625549
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/EnhancedTelegramTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class EnhancedTelegramTest extends TelegramTest {
constructor(bot, messageId = 1000, updateId = 0) {
super(bot, messageId, updateId);
this.setChatOptions();

// Creating stub for testing purposes
// eslint-disable-next-line no-param-reassign
bot.answerInlineQuery = (queryId, articles = [], options = {}) => {
bot.emit('testInline', queryId, articles, options);
};
}

setChatOptions(
Expand Down Expand Up @@ -33,6 +39,33 @@ class EnhancedTelegramTest extends TelegramTest {
.then(data => data.text)
.catch(error => error.message);
}

createInlineMessage(query) {
this.messageId += 1;
return {
id: this.messageId,
query
};
}

sendInlineQuery(query = '') {
const bot = this.bot;
const self = this;

return new Promise(resolve => {
bot.on('testInline', function handler(inlineQueryId, results, options) {
bot.removeListener('testInline', handler);
resolve({ results, options });
});
bot.emit('inline_query', self.createInlineMessage(query));
});
}

inline(query) {
return this.sendInlineQuery(query)
.then(data => data.results)
.catch(error => error);
}
}

module.exports = EnhancedTelegramTest;
40 changes: 40 additions & 0 deletions test/query/inline.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { createTestServer } = require('../utils');

const server = createTestServer();

const createResults = (classic, wod) =>
[
{ title: 'Classic', description: classic },
{ title: 'World of Darkness', description: wod },
{ title: 'Random', description: 'd100' }
].filter(result => !!result.description);

async function matchResults(query, expected) {
const results = await server.inline(query);
expect(results.length).toEqual(3);
results.forEach((result, index) =>
expect(result).toMatchObject(expected[index])
);
}

describe('Inline queries', () => {
test('should return default articles for empty query', async () => {
expect.assertions(12);

const expected = createResults('d20', 'd10>6', 'd100');

matchResults(undefined, expected);
matchResults('', expected);
matchResults(' ', expected);
});
/*
test('should return `random` article for invalid query', async () => {
expect.assertions(4);
const expected = createResults();
// should return null for classic parser
matchResults('abc', expected);
});
*/
});

0 comments on commit c625549

Please sign in to comment.