You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mercury-parser/src/resource/index.test.js

76 lines
1.9 KiB
JavaScript

import assert from 'assert';
import cheerio from 'cheerio';
import { Errors } from 'utils';
import { record } from 'test-helpers';
import Resource from './index';
describe('Resource', () => {
const recorder = record('resource-test');
beforeAll(recorder.before);
afterAll(recorder.after);
describe('create(url)', () => {
feat: some small tweaks to toy's excellent parsers ☺️ Squashed commit of the following: commit 9638220124a325322d6cda7d16c645185d5fe827 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 11:02:29 2016 -0700 fix: removed eslint plugin that was adding unneded async parens commit ce2268c0f7c1b093c06f156730a0f1bc2aaba39c Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:47:36 2016 -0700 style: fix async in parens commit 9591856915eddaf93170da1ce9225b8a378bdf55 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:37:11 2016 -0700 fix: remove parens around async commit 6c56054717acc1f7e5499691780f8273f6d07bac Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:35:50 2016 -0700 fix msn fixture; adjusted yahoo test commit 4fc117ad5fdc5528f29b0873d60a6a1709642f15 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:14:38 2016 -0700 removed dek and date_publised tests; neither exist in littlethings commit 401094b4abc52901255fd2461f5839624f11d8a3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:08:44 2016 -0700 feat: updated buzzfeed for content extraction commit 19548a5485f70ff9b65e3e725d2364d07734ac9c Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 09:54:30 2016 -0700 fix: generator should make transforms an object, not array commit b92113f9f7c97aca9e6d3ce9243abac967d26b63 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:54:38 2016 -0700 feat: updated politico commit c026591040f7671cb2a6dd5177a995e21d015482 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:48:52 2016 -0700 fix: typos commit 14aa8fa4ce38ff1c2a212cd0225437ae3042c2c3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:36:12 2016 -0700 fix: incorrect command in readme commit fe260e6122877e2cb0130a1ecde0e503017057a3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:31:11 2016 -0700 fix: removed dek test because there is no dek on wikia
8 years ago
it('fetches the page and returns a cheerio object', async () => {
const url = 'http://theconcourse.deadspin.com/1786177057';
const $ = await Resource.create(url);
assert.equal(typeof $, 'function');
});
feat: some small tweaks to toy's excellent parsers ☺️ Squashed commit of the following: commit 9638220124a325322d6cda7d16c645185d5fe827 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 11:02:29 2016 -0700 fix: removed eslint plugin that was adding unneded async parens commit ce2268c0f7c1b093c06f156730a0f1bc2aaba39c Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:47:36 2016 -0700 style: fix async in parens commit 9591856915eddaf93170da1ce9225b8a378bdf55 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:37:11 2016 -0700 fix: remove parens around async commit 6c56054717acc1f7e5499691780f8273f6d07bac Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:35:50 2016 -0700 fix msn fixture; adjusted yahoo test commit 4fc117ad5fdc5528f29b0873d60a6a1709642f15 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:14:38 2016 -0700 removed dek and date_publised tests; neither exist in littlethings commit 401094b4abc52901255fd2461f5839624f11d8a3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 10:08:44 2016 -0700 feat: updated buzzfeed for content extraction commit 19548a5485f70ff9b65e3e725d2364d07734ac9c Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 09:54:30 2016 -0700 fix: generator should make transforms an object, not array commit b92113f9f7c97aca9e6d3ce9243abac967d26b63 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:54:38 2016 -0700 feat: updated politico commit c026591040f7671cb2a6dd5177a995e21d015482 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:48:52 2016 -0700 fix: typos commit 14aa8fa4ce38ff1c2a212cd0225437ae3042c2c3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:36:12 2016 -0700 fix: incorrect command in readme commit fe260e6122877e2cb0130a1ecde0e503017057a3 Author: Adam Pash <adam.pash@gmail.com> Date: Mon Oct 10 08:31:11 2016 -0700 fix: removed dek test because there is no dek on wikia
8 years ago
it('returns an error message if the url is malformed', async () => {
const url = 'http://nytimes.com/500';
const error = await Resource.create(url);
assert.equal(error, Errors.badUrl);
});
});
describe('generateDoc({ body, response })', () => {
it('returns a cheerio object if valid', () => {
const response = { headers: { 'content-type': 'text/html' } };
const body = '<div><p>Hi</p></div>';
const $ = Resource.generateDoc({ body, response });
assert.equal($.html(), body);
});
it('throws an error if the content is not text', () => {
const response = {
headers: {
'content-type': 'foo',
},
};
const body = '';
assert.throws(
() => {
Resource.generateDoc({ body, response });
},
/content does not appear to be text/i
);
});
it('throws an error if the content has no children', () => {
// jquery's parser won't work this way, and this is
// an outside case
if (!cheerio.browser) {
const response = {
headers: {
'content-type': 'html',
},
};
const body = '';
assert.throws(
() => {
Resource.generateDoc({ body, response });
},
/no children/i
);
}
});
});
});