Skip to content

Commit

Permalink
add setEditor helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Oct 29, 2015
1 parent 6405932 commit c573235
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 198 deletions.
3 changes: 0 additions & 3 deletions config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module.exports = function(config) {
'node_modules/jquery/dist/jquery.js',
'node_modules/lodash/index.js',

'test/helpers/unit.js',
'test/helpers/matchers.js',

'http://' + config.host + '/quill.base.css',
'http://' + config.host + '/test/quill.js'
],
Expand Down
8 changes: 0 additions & 8 deletions test/helpers/coverage.js

This file was deleted.

80 changes: 0 additions & 80 deletions test/helpers/matchers.js

This file was deleted.

86 changes: 86 additions & 0 deletions test/helpers/unit.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,96 @@
import Editor from '../../src/editor';

let $div = $('<div>').attr('id', 'test-container');
$(document.body).prepend($div);

beforeEach(function() {
jasmine.addMatchers({
toEqualHTML: function() {
return {
compare: function(actual, expected, ignoreClassId) {
let [div1, div2] = _.map([actual, expected], function(html) {
if (html instanceof HTMLElement) {
html = html.innerHTML;
}
let div = document.createElement('div');
div.innerHTML = html.replace(/\n\s*/g, '');
return div;
});
let ignoredAttributes = ['width', 'height'];
if (ignoreClassId) {
ignoredAttributes = ignoredAttributes.concat(['class', 'id']);
}
let message = compareNodes(div1, div2, ignoredAttributes)
if (message != null) {
console.error(div1.innerHTML);
console.error(div2.innerHTML);
return { pass: false, message: message };
} else {
return { pass: true, message: 'HTMLs equal' };
}
}
};
},

toBeApproximately: function() {
return {
compare: function(actual, expected, tolerance) {
let pass = Math.abs(actual - expected) <= tolerance;
return {
pass: pass,
message: `${actual} is ${(pass ? '' : 'not')} approximately ${expected}`
};
}
};
}
});

this.container = $div.html('<div></div>').get(0).firstChild;
// Defining in a beforeAll does not work, seems this is cloned or something
this.setContainer = (html, container = this.container) => {
return container.innerHTML = html.replace(/\n\s*/g, '');
};

this.setEditor = (html, container = this.container) => {
this.setContainer(html, container);
return new Editor(container);
}
});


function compareNodes(node1, node2, ignoredAttributes = []) {
let attr1, attr2, message, ref;
if (node1.nodeType !== node2.nodeType) {
return `Expected nodeType '${node1.nodeType}' to equal '${node2.nodeType}'`;
}
if (node1.nodeType === node1.ELEMENT_NODE) {
if (node1.tagName !== node2.tagName) {
return `Expected tagName '${node1.tagName}' to equal '${node2.tagName}'`;
}
let [attr1, attr2] = _.map([node1, node2], function(node) {
return _.reduce(node.attributes, function(attr, elem) {
if (ignoredAttributes.indexOf(elem.name) < 0) {
attr[elem.name] = elem.name === 'style' ? elem.value.trim() : elem.value;
}
return attr;
}, {});
});
if (!_.isEqual(attr1, attr2)) {
return `Expected attributes ${jasmine.pp(attr1)} to equal ${jasmine.pp(attr2)}`;
}
if (node1.childNodes.length !== node2.childNodes.length) {
return `Expected node childNodes length '#{node1.childNodes.length}' to equal '#{node2.childNodes.length}'`;
}
if (node1.childNodes.length === 0) return null;
let message = '';
if (_.any($(node1).contents(), function(child1, i) {
message = compareNodes(child1, node2.childNodes[i], ignoredAttributes);
return message;
})) {
return message;
}
} else if ($(node1).text() !== $(node2).text()) {
return `Expected node text '#{$(node1).text()}' to equal '#{$(node2).text()}'`;
}
return null;
}
2 changes: 2 additions & 0 deletions test/quill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Quill from '../src/index';

import {} from './helpers/unit.js';

import EditorTests from './unit/editor';
import SelectionTests from './unit/selection';
import EventTests from './unit/events';
Expand Down
Loading

0 comments on commit c573235

Please sign in to comment.