@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
77 lines • 3.58 kB
JavaScript
/// <reference path="./types/chai.d.ts"/>
import { Mark } from '../';
function isNodeOrFragment(thing) {
// Using a simple `instanceof` check is intentionally avoided here to make
// this code agnostic to a specific instance of a Schema.
return thing && typeof thing.eq === 'function';
}
function isSlice(thing) {
return typeof thing.openStart === 'number' &&
typeof thing.openEnd === 'number' &&
isNodeOrFragment(thing.content);
}
export default function (chai) {
var Assertion = chai.Assertion, util = chai.util;
// Node and Fragment
Assertion.overwriteMethod('equal', function (equalSuper) {
return function (right) {
var left = this._obj;
var deep = util.flag(this, 'deep');
if (deep && isNodeOrFragment(left) && isNodeOrFragment(right)) {
this.assert(left.eq(right), "expected " + left.toString() + " to equal " + right.toString(), "expected " + left.toString() + " to not equal " + right.toString(), left.toJSON(), right.toJSON(),
/*showDiff*/ true);
}
else {
equalSuper.apply(this, arguments);
}
};
});
// Slice
Assertion.overwriteMethod('equal', function (equalSuper) {
return function (right) {
var left = this._obj;
var deep = util.flag(this, 'deep');
if (deep && isSlice(left) && isSlice(right)) {
this.assert(left.content.eq(right.content), 'expected left\'s fragment #{exp} to equal right\'s fragment #{act}', 'expected left\'s fragment #{exp} to not equal right\'s fragment #{act}', left.content.toString(), right.content.toString());
this.assert(left.openStart === right.openStart, 'expected left\'s openStart #{exp} to equal right\'s openStart #{act}', 'expected left\'s openStart #{exp} to not equal right\'s openStart #{act}', left.openStart, right.openStart);
this.assert(left.openEnd === right.openEnd, 'expected left\'s openEnd #{exp} to equal right\'s openEnd #{act}', 'expected left\'s openEnd #{exp} to not equal right\'s openEnd #{act}', left.openEnd, right.openEnd);
}
else {
equalSuper.apply(this, arguments);
}
};
});
Assertion.addMethod('nodeType', function (nodeType) {
var obj = util.flag(this, 'object');
var negate = util.flag(this, 'negate');
if (negate) {
return new Assertion(obj.type).not.to.be.an.instanceof(nodeType);
}
return new Assertion(obj.type).to.be.an.instanceof(nodeType);
});
Assertion.addMethod('textWithMarks', function (text, marks) {
var obj = util.flag(this, 'object');
var negate = util.flag(this, 'negate');
var matched = false;
obj.descendants(function (node, pos) {
if (node.isText && node.text === text) {
if (Mark.sameSet(node.marks, marks)) {
matched = true;
}
}
});
if (negate) {
return new Assertion(matched).not.to.be.true;
}
return new Assertion(matched).to.be.true;
});
Assertion.addMethod('nodeSpec', function (nodeSpec) {
var obj = util.flag(this, 'object');
var negate = util.flag(this, 'negate');
if (negate) {
return new Assertion(obj.type.spec).not.to.deep.equal(nodeSpec);
}
return new Assertion(obj.type.spec).to.deep.equal(nodeSpec);
});
};
//# sourceMappingURL=chai.js.map