var assert = require("assert"); var splitHtml = require("../index.js"); describe('splitHtml', function(){ it('Leaves img-free HTML alone when splitting on img', function() { var html = '
This is fun.
Loose text.'; var result = splitHtml(html, 'img'); assert(result.length === 1); assert(result[0] === html); }); it('Splits into the correct three components in the presence of img', function() { var html = '
Second component.
'; var result = splitHtml(html, 'img'); assert(result.length === 3); assert(result[0] === '
');
assert(result[2] === 'Second component.
'); }); it('Correctly closes open tags in the first component and reopens them at the start of the second component', function() { var html = '
Second component.
'; var result = splitHtml(html, 'img'); assert(result.length === 3); assert(result[0] === '
');
assert(result[2] === 'Second component.
'); }); it('Works with free text', function() { var html = 'Text one
Text two';
var result = splitHtml(html, 'img');
assert(result.length === 3);
assert(result[0] === 'Text one');
assert(result[1] === '
');
assert(result[2] === 'Text two');
});
it('Works with multiple instances', function() {
var html = '
Second component.

Third component.
'; var result = splitHtml(html, 'img'); assert(result.length === 5); assert(result[0] === '
');
assert(result[2] === 'Second component.
'); assert(result[3] === '
');
assert(result[4] === 'Third component.
'); }); it('Respects test function', function() { var html = '
Second component.
Link text.More text in second component.
'; var result = splitHtml(html, 'a', function($el) { if ($el.find('img').length) { return true; } else { return false; } }); assert(result.length === 3); assert(result[0] === '
');
assert(result[2] === 'Second component.
Link text.More text in second component.
'); }); it('Respects a split at the end of a word', function() { var html = 'One
Two
Three
Four
'; var result = splitHtml(html, 'span[data-split-marker]'); assert(result.length === 3); assert(result[0] === 'One
Two
'); assert(result[1] === ''); assert(result[2] === 'Three
Four
'); }); it('Splits properly when parents are multiple levels deep', function() { var html = 'Hello!Goodbye.
Hello!
Goodbye.