Skip to content

Commit

Permalink
Merge 5a2523a into d3271b6
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Sep 22, 2018
2 parents d3271b6 + 5a2523a commit da0047d
Showing 1 changed file with 74 additions and 57 deletions.
131 changes: 74 additions & 57 deletions lib/index.js
Expand Up @@ -260,7 +260,14 @@ module.exports = {
installInto: function(expect) {
expect = expect.child();
expect.use(require('magicpen-prism'));
var topLevelExpect = expect;

function bubbleError(body) {
return expect.withError(body, function(err) {
err.errorMode = 'bubble';
throw err;
});
}

expect.exportType({
name: 'DOMNode',
base: 'object',
Expand Down Expand Up @@ -700,11 +707,9 @@ module.exports = {
if (typeof value === 'string') {
value = getClassNamesFromAttributeValue(value);
}
return topLevelExpect(
actualClasses.sort(),
'to equal',
value.sort()
);
return bubbleError(function() {
return expect(actualClasses.sort(), 'to equal', value.sort());
});
}
});
}
Expand Down Expand Up @@ -966,11 +971,13 @@ module.exports = {
var promiseByKey = {
name: expect.promise(function() {
if (value && typeof value.name !== 'undefined') {
return topLevelExpect(
isHtml ? subject.nodeName.toLowerCase() : subject.nodeName,
'to satisfy',
value.name
);
return bubbleError(function() {
return expect(
isHtml ? subject.nodeName.toLowerCase() : subject.nodeName,
'to satisfy',
value.name
);
});
}
}),
children: expect.promise(function() {
Expand All @@ -980,20 +987,24 @@ module.exports = {
'The children and textContent properties are not supported together'
);
}
return topLevelExpect(
makeAttachedDOMNodeList(
subject.childNodes,
subject.ownerDocument.contentType
),
'to satisfy',
value.children
);
return bubbleError(function() {
return expect(
makeAttachedDOMNodeList(
subject.childNodes,
subject.ownerDocument.contentType
),
'to satisfy',
value.children
);
});
} else if (typeof value.textContent !== 'undefined') {
return topLevelExpect(
subject.textContent,
'to satisfy',
value.textContent
);
return bubbleError(function() {
return expect(
subject.textContent,
'to satisfy',
value.textContent
);
});
}
}),
attributes: {}
Expand Down Expand Up @@ -1032,15 +1043,13 @@ module.exports = {
expectedValueByAttributeName[attributeName];
promiseByKey.attributes[attributeName] = expect.promise(function() {
if (expectedAttributeValue === true) {
topLevelExpect(
subject.hasAttribute(attributeName),
'to be true'
);
return bubbleError(function() {
expect(subject.hasAttribute(attributeName), 'to be true');
});
} else if (typeof expectedAttributeValue === 'undefined') {
topLevelExpect(
subject.hasAttribute(attributeName),
'to be false'
);
return bubbleError(function() {
expect(subject.hasAttribute(attributeName), 'to be false');
});
} else if (
attributeName === 'class' &&
(typeof expectedAttributeValue === 'string' ||
Expand All @@ -1056,19 +1065,25 @@ module.exports = {
);
}
if (onlyAttributes) {
return topLevelExpect(
actualClasses.sort(),
'to equal',
expectedClasses.sort()
);
return bubbleError(function() {
return expect(
actualClasses.sort(),
'to equal',
expectedClasses.sort()
);
});
} else {
if (expectedClasses.length === 0) {
return topLevelExpect(expectedClasses, 'to be empty');
return bubbleError(function() {
return expect(expectedClasses, 'to be empty');
});
}
return topLevelExpect.apply(
topLevelExpect,
[actualClasses, 'to contain'].concat(expectedClasses)
);
return bubbleError(function() {
return expect.apply(
expect,
[actualClasses, 'to contain'].concat(expectedClasses)
);
});
}
} else if (attributeName === 'style') {
var expectedStyleObj;
Expand All @@ -1081,24 +1096,26 @@ module.exports = {
}

if (onlyAttributes) {
return topLevelExpect(
attrs.style,
'to exhaustively satisfy',
expectedStyleObj
);
return bubbleError(function() {
return expect(
attrs.style,
'to exhaustively satisfy',
expectedStyleObj
);
});
} else {
return topLevelExpect(
attrs.style,
'to satisfy',
expectedStyleObj
);
return bubbleError(function() {
return expect(attrs.style, 'to satisfy', expectedStyleObj);
});
}
} else {
return topLevelExpect(
attributeValue,
'to satisfy',
expectedAttributeValue
);
return bubbleError(function() {
return expect(
attributeValue,
'to satisfy',
expectedAttributeValue
);
});
}
});
});
Expand Down

0 comments on commit da0047d

Please sign in to comment.