hamjest
Version:
A library of composable matchers for defining meaningful and readable assertions in JavaScript.
52 lines (47 loc) • 1.35 kB
JavaScript
;
const _create = require('lodash/create');
const IsPromise = require('./IsPromise');
const asMatcher = require('../utils/asMatcher');
const anything = require('./IsAnything').anything;
function IsFulfilled(valueOrMatcher) {
const anyValue = (arguments.length === 0);
const valueMatcher = (anyValue ? anything() : asMatcher(valueOrMatcher));
return _create(new IsPromise(), {
matchesSafely: function (actual) {
return actual.then((value) => {
return valueMatcher.matches(value);
}, () => false);
},
describeTo: function (description) {
if (anyValue) {
description.append('a fulfilled promise');
} else {
description.append('a promise fulfilled with ');
valueMatcher.describeTo(description);
}
},
describeMismatchSafely: function (actual, description) {
return actual.then(
(actualValue) => {
description
.append('fulfillment value: ');
return valueMatcher.describeMismatch(actualValue, description);
},
(error) => {
description
.append('was rejected with ')
.appendValue(error);
}
);
}
});
}
IsFulfilled.fulfilled = function (operand) {
if (arguments.length === 0) {
return new IsFulfilled();
} else {
return new IsFulfilled(operand);
}
};
IsFulfilled.isFulfilledWith = IsFulfilled.fulfilled;
module.exports = IsFulfilled;