omnivore/packages/thumbnail-handler/test/index.test.ts

29 lines
912 B
TypeScript
Raw Permalink Normal View History

2023-06-07 02:45:46 +00:00
import { expect } from 'chai'
import fs from 'fs'
import 'mocha'
import nock from 'nock'
import path from 'path'
2023-08-07 10:51:17 +00:00
import { fetchAllImageSizes, findThumbnail } from '../src'
2023-06-07 02:45:46 +00:00
describe('findThumbnail', () => {
it('finds the largest and squarest image', async () => {
const images = ['large_and_square', 'small', 'sprite', 'wide']
// mock getting image by url
images.forEach((image) => {
2025-09-24 10:37:39 +00:00
nock('https://omnivore.work')
2023-06-07 02:45:46 +00:00
.get(`/${image}.png`)
.replyWithFile(200, path.join(__dirname, 'fixtures', `${image}.png`))
})
// get html content from file
const content = fs.readFileSync(
path.join(__dirname, 'fixtures', 'findThumbnail.html'),
'utf8'
)
// find thumbnail
2023-08-07 10:51:17 +00:00
const imageSizes = await fetchAllImageSizes(content)
const thumbnail = findThumbnail(imageSizes)
2023-06-07 02:45:46 +00:00
2025-09-24 10:37:39 +00:00
expect(thumbnail).to.eql('https://omnivore.work/large_and_square.png')
2023-06-07 02:45:46 +00:00
})
})