test: add empty item test case

This commit is contained in:
DIYgod
2019-06-13 12:01:07 +08:00
parent cbcfc03207
commit 76add4785a
4 changed files with 21 additions and 2 deletions

View File

@@ -4,7 +4,7 @@ module.exports = async (ctx, next) => {
await next(); await next();
if (ctx.state.data && (!ctx.state.data.item || ctx.state.data.item.length === 0)) { if (ctx.state.data && (!ctx.state.data.item || ctx.state.data.item.length === 0)) {
throw Error('该路由目前获取内容为空,请检查源站情况或前往 https://github.com/DIYgod/RSSHub/issues 反馈该问题'); throw Error('this route is empty, please check the origin site or create an issue to feedback on https://github.com/DIYgod/RSSHub/issues/new/choose');
} }
// decode HTML entities // decode HTML entities

View File

@@ -5,7 +5,7 @@ module.exports = async (ctx) => {
if (ctx.params.id === '0') { if (ctx.params.id === '0') {
throw Error('Error test'); throw Error('Error test');
} }
const item = []; let item = [];
if (ctx.params.id === 'long') { if (ctx.params.id === 'long') {
item.push({ item.push({
title: `Long Title `.repeat(10), title: `Long Title `.repeat(10),
@@ -34,6 +34,11 @@ module.exports = async (ctx) => {
author: `DIYgod${i}`, author: `DIYgod${i}`,
}); });
} }
if (ctx.params.id === 'empty') {
item = null;
}
ctx.state.data = { ctx.state.data = {
title: `Test ${ctx.params.id}`, title: `Test ${ctx.params.id}`,
link: 'https://github.com/DIYgod/RSSHub', link: 'https://github.com/DIYgod/RSSHub',

View File

@@ -9,6 +9,7 @@ afterAll(() => {
describe('error', () => { describe('error', () => {
it(`error`, async () => { it(`error`, async () => {
const response = await request.get('/test/0'); const response = await request.get('/test/0');
expect(response.status).toBe(404);
expect(response.text).toMatch(/RSSHub 发生了一些意外: <pre>Error: Error test/); expect(response.text).toMatch(/RSSHub 发生了一些意外: <pre>Error: Error test/);
}); });
}); });

View File

@@ -97,3 +97,16 @@ describe('tgiv', () => {
expect(parsed.items[1].link).toBe(`https://t.me/iv?url=https%3A%2F%2Fgithub.com%2FDIYgod%2FRSSHub%2Fissues%2F2&rhash=test`); expect(parsed.items[1].link).toBe(`https://t.me/iv?url=https%3A%2F%2Fgithub.com%2FDIYgod%2FRSSHub%2Fissues%2F2&rhash=test`);
}); });
}); });
describe('empty', () => {
it(`empty`, async () => {
const response1 = await request.get('/test/empty');
expect(response1.status).toBe(404);
expect(response1.text).toMatch(/RSSHub 发生了一些意外: <pre>Error: this route is empty/);
const response2 = await request.get('/test/1?limit=0');
expect(response2.status).toBe(200);
const parsed = await parser.parseString(response2.text);
expect(parsed.items.length).toBe(0);
});
});