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)', () => { 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'); }); 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 = '

Hi

'; 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 ); } }); }); });