From 4d45134e1f6c3f0eb8078e124122b015fc9e6d92 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Fri, 12 Mar 2021 19:03:56 +0100 Subject: [PATCH] fix webmention get as a string --- package.json | 2 +- src/webmention/get.js | 9 +++++++-- test/__mocks__/got.js | 3 ++- test/webmention/get-e2e.test.js | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test/webmention/get-e2e.test.js diff --git a/package.json b/package.json index 594e740..d484336 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.6", + "version": "1.0.7", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" diff --git a/src/webmention/get.js b/src/webmention/get.js index 27bf078..7cb02c6 100644 --- a/src/webmention/get.js +++ b/src/webmention/get.js @@ -10,12 +10,17 @@ async function getWebmentions(domain) { const url = `${config.serveMyJamEndpoint}/webmention/${domain}/${config.serveMyJamToken}` const result = await got(url) - result.body.json.forEach(mention => { + if(!result.body || !result.body) { + return [] + } + const json = JSON.parse(result.body).json + + json.forEach(mention => { mention.publishedFromNow = dayjs(mention.published).fromNow() mention.relativeTarget = mention.target.substring(mention.target.indexOf(domain) + domain.length, mention.target.length) }) - return result.body.json.sort((a, b) => dayjs(b.published).toDate() - dayjs(a.published).toDate()) + return json.sort((a, b) => dayjs(b.published).toDate() - dayjs(a.published).toDate()) } module.exports = { diff --git a/test/__mocks__/got.js b/test/__mocks__/got.js index 70be0e3..17153d3 100644 --- a/test/__mocks__/got.js +++ b/test/__mocks__/got.js @@ -5,7 +5,8 @@ async function got(url) { if(url.indexOf('/webmention') >= 0) { const result = await fs.readFile(`./test/__mocks__/get-sample.json`, 'utf8'); return { - body: JSON.parse(result) + // WHY not a JSON.parse here? The body is a STRING IRL! + body: result } } diff --git a/test/webmention/get-e2e.test.js b/test/webmention/get-e2e.test.js new file mode 100644 index 0000000..60c2e8a --- /dev/null +++ b/test/webmention/get-e2e.test.js @@ -0,0 +1,15 @@ + +jest.disableAutomock() +jest.unmock('got') + +const { getWebmentions } = require('./../../src/webmention/get') +const domain = "brainbaking.com" + +describe("webmention receive scenario test", () => { + + test("getWebmentions fetches anything at all", async () => { + const result = await getWebmentions(domain) + expect(result.length).toBeGreaterThan(-1) + }) + +})