const assert = require('assert/strict'); const ObjectID = require('bson-objectid'); const Mention = require('../lib/Mention'); const cheerio = require('cheerio'); const sinon = require('sinon'); const validInput = { source: 'https://source.com', target: 'https://target.com', sourceTitle: 'Title!', sourceExcerpt: 'Excerpt!' }; describe('Mention', function () { describe('toJSON', function () { it('Returns a object with the expected properties', async function () { const mention = await Mention.create(validInput); const actual = Object.keys(mention.toJSON()); const expected = [ 'id', 'source', 'target', 'timestamp', 'payload', 'resourceId', 'resourceType', 'sourceTitle', 'sourceSiteTitle', 'sourceAuthor', 'sourceExcerpt', 'sourceFavicon', 'sourceFeaturedImage', 'verified' ]; assert.deepEqual(actual, expected); }); }); describe('verify', function () { afterEach(function () { sinon.restore(); }); it('can handle invalid HTML', async function () { const mention = await Mention.create(validInput); assert(!mention.verified); sinon.stub(cheerio, 'load').throws(new Error('Invalid HTML')); mention.verify('irrelevant', 'text/html'); assert(!mention.verified); assert(!mention.isDeleted()); }); it('Does basic check for the target URL and updates verified property', async function () { const mention = await Mention.create(validInput); assert(!mention.verified); mention.verify('', 'text/html'); assert(mention.verified); assert(!mention.isDeleted()); mention.verify('something else', 'text/html'); assert(mention.verified); assert(mention.isDeleted()); }); it('detects differences', async function () { const mention = await Mention.create(validInput); assert(!mention.verified); mention.verify('', 'text/html'); assert(!mention.verified); assert(!mention.isDeleted()); }); it('Does check for Image targets', async function () { const mention = await Mention.create({ ...validInput, target: 'https://target.com/image.jpg' }); assert(!mention.verified); mention.verify('', 'text/html'); assert(mention.verified); assert(!mention.isDeleted()); mention.verify('something else', 'text/html'); assert(mention.verified); assert(mention.isDeleted()); }); it('Does check for Video targets', async function () { const mention = await Mention.create({ ...validInput, target: 'https://target.com/video.mp4' }); assert(!mention.verified); mention.verify('