Skip to content

Commit

Permalink
Moved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Jan 9, 2019
1 parent 9c88746 commit eb7e7f6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/build/prepare/index.js
Expand Up @@ -83,7 +83,7 @@ function Prepare(entry) {
debug(entry.path, "Generated title", entry.title);

debug(entry.path, "Generating summary");
entry.summary = Summary($, entry.title);
entry.summary = Summary($, metadata.title || entry.title);
debug(entry.path, "Generated summary");

debug(entry.path, "Generating teasers");
Expand Down
39 changes: 0 additions & 39 deletions app/build/prepare/summary.js
Expand Up @@ -58,43 +58,4 @@ function summary ($, title) {
return summary;
}

var cheerio = require('cheerio');
var assert = require('assert');

function is (html, expected, title) {

var $ = cheerio.load(html, {decodeEntities: false});
var output = summary($, title || '');

try {
assert(output === expected);
} catch (e) {
console.log('INPUT', html);
console.log('OUTPUT', output);
console.log('EXPECTED', expected);
}

}

var longWord = 'helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo';


is('<p>H</p>', 'H');

// Handle inline elements nicely
is('<span>H</span><a href="">ello</a>', 'Hello');

// Check spaces are inserted between paragraphs
is('<p>H</p><p>E</p>', 'H E');

// Check blot ignores the title text
is('<p>Hello</p><p>there</p>', 'there', 'Hello');

// Check long results are trimmed to words
is('<p>Hello there ' + longWord + '.</p>', 'Hello there');

// Check the result is decoded
is('<p>Hello & foo</p><p>there</p>', 'Hello & foo there');
is('<p>Hello &amp; & foo</p><p>there</p>', 'Hello & & foo there');

module.exports = summary;
76 changes: 76 additions & 0 deletions app/build/prepare/tests/summary.js
@@ -0,0 +1,76 @@
describe("summary", function() {
var cheerio = require("cheerio");
var summary = require("../summary");

beforeEach(function() {
this.summary = function(input) {
var $ = cheerio.load(input.html, {
decodeEntities: false,
withDomLvl1: false // this may cause issues?
});

return summary($, input.title || "");
};
});

it("is empty when HTML is empty", function() {
expect(
this.summary({
title: "",
html: ""
})
).toEqual("");
});

it("is the text of the first paragraph", function() {
expect(
this.summary({
html: "<p>H</p>"
})
).toEqual("H");
});

it("contains text inside inline elements", function() {
expect(
this.summary({
html: "<p>H<b>ell</b>o</p>"
})
).toEqual("Hello");
});

it("does not contain the text of the title", function() {
expect(
this.summary({
html: "<p>Hello</p><p>World</p>",
title: "Hello"
})
).toEqual("World");
});

// Not sure if I like this behaviour
it("has spaces between seperate paragraphs", function() {
expect(
this.summary({
html: "<p>Hello</p><p>World</p>",
title: "Hello"
})
).toEqual("World");
});

it("will not contain or truncate long words", function() {
expect(
this.summary({
html: "<p>Hello there helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.</p>"
})
).toEqual("Hello there");
});

it("decodes HTML entities", function() {
expect(
this.summary({
html: "<p>Hello & &amp; foo</p><p>there</p>"
})
).toEqual("Hello & & foo there");
});

});

0 comments on commit eb7e7f6

Please sign in to comment.