hamjest
Version:
A library of composable matchers for defining meaningful and readable assertions in JavaScript.
48 lines (41 loc) • 1.29 kB
JavaScript
;
const _ = require('lodash');
const IsArray = require('./IsArray');
const acceptingMatcher = require('../utils/acceptingMatcher');
const promiseAgnostic = require('./promiseAgnostic');
const IsArrayWithItem = acceptingMatcher((matcher) => {
return _.create(new IsArray(), {
matchesSafely: function (actual) {
const results = _.map(actual, (value) => {
return matcher.matches(value);
});
return promiseAgnostic.matchesAggregate(results, _.some);
},
describeTo: function (description) {
description
.append('an array containing ')
.appendDescriptionOf(matcher);
},
describeMismatchSafely: function (actual, description) {
if (actual.length === 0) {
description.append('was empty');
return;
}
const results = _.map(actual, (value) => {
return matcher.matches(value);
});
return promiseAgnostic.describeMismatchAggregate(results, (__result, index) => {
description.append('\n');
description
.append('item ')
.append(index)
.append(': ');
return description.indented(() => matcher.describeMismatch(actual[index], description));
});
}
});
});
IsArrayWithItem.hasItem = function (valueOrMatcher) {
return new IsArrayWithItem(valueOrMatcher);
};
module.exports = IsArrayWithItem;