Skip to content

Commit

Permalink
page: fix backlinks() and transclusions() to work for missing and orp…
Browse files Browse the repository at this point in the history
…haned pages

Fixes #76
Fixes #77
  • Loading branch information
siddharthvp committed Sep 10, 2024
1 parent 8cb8d79 commit a11b508
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,8 @@ export default function (bot: Mwn): MwnPageStatic {
lhlimit: 'max',
})
.then((jsons) => {
let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), []);
let page = pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
return page.linkshere.map((pg: ApiPage) => pg.title);
let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), [] as ApiPage[]);
return (pages[0].linkshere || []).map((pg) => pg.title);
});
}

Expand All @@ -285,12 +281,8 @@ export default function (bot: Mwn): MwnPageStatic {
tilimit: 'max',
})
.then((jsons) => {
let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), []);
let page = pages[0];
if (page.missing) {
return Promise.reject(new MwnError.MissingPage());
}
return page.transcludedin.map((pg: ApiPage) => pg.title);
let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), [] as ApiPage[]);
return (pages[0].transcludedin || []).map((pg) => pg.title);
});
}

Expand Down
14 changes: 14 additions & 0 deletions tests/page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,27 @@ describe('Page', async function () {
});
});

it('backlinks for non-existing page', function () {
return new bot.Page('29wdijopsk239esijd123').backlinks().then((backlinks) => {
expect(backlinks).to.be.instanceOf(Array);
expect(backlinks.length).to.equal(0);
});
});

it('transclusions', function () {
return page.transclusions().then((transclusions) => {
expect(transclusions).to.be.instanceOf(Array);
expect(transclusions.length).to.be.gte(1);
});
});

it('transclusions for non-existing page', function () {
return new bot.Page('29wdijopsk239esijd123').transclusions().then((transclusions) => {
expect(transclusions).to.be.instanceOf(Array);
expect(transclusions.length).to.equal(0);
});
});

it('history', function () {
return page.history().then((history) => {
expect(history).to.be.instanceOf(Array);
Expand Down

0 comments on commit a11b508

Please sign in to comment.