goodreads widgetify and tests

This commit is contained in:
Wouter Groeneveld 2021-03-04 19:32:35 +01:00
parent 90f1a72fe3
commit c2020e20e3
5 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,28 @@
const https = require('https')
// WHY? Because including this thing comes with free cookies...
const replaceLowresWithHiresImages = (data) => {
return data.replace(/_SX[0-9]+_(SY[0-9]+_)*.jpg/g, "_S400_.jpg")
}
function widgetify(url) {
return new Promise(resolve => {
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(replaceLowresWithHiresImages(data))
});
})
})
}
module.exports = {
widgetify
}

View File

@ -1,5 +1,7 @@
const parseMastoFeed = require('./masto-feed-parser')
const { parseMastoFeed } = require('./masto-feed-parser')
const { widgetify } = require('./goodreads-widgetify.js')
module.exports = {
parseMastoFeed
parseMastoFeed,
goodreadsWidgetify: widgetify
};

16
test/__mocks__/https.js Normal file
View File

@ -0,0 +1,16 @@
const fs = require('fs')
const fakedata = fs.readFileSync('./test/expected-goodreads-content.js')
function getmock(url, callback) {
callback({
on: function(id, callback) {
if(id === "data") callback(fakedata)
if(id === "end") callback()
}
})
}
module.exports = {
"get": getmock
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,10 @@
jest.mock('https')
const { widgetify } = require('../src/goodreads-widgetify')
test("goodreads-widgetify changes from lowres to hires images", async () => {
const result = await widgetify("fake")
expect(result).toMatch(/_S400_.jpg/)
expect(result).not.toMatch(/_SX/)
})