Merge branch 'feature/matcher-toHaveClasses' of https://github.com/aYorky/jasmine
* Merges #2046 from @aYorky
This commit is contained in:
@@ -159,6 +159,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
|
|||||||
'toHaveBeenCalledTimes',
|
'toHaveBeenCalledTimes',
|
||||||
'toHaveBeenCalledWith',
|
'toHaveBeenCalledWith',
|
||||||
'toHaveClass',
|
'toHaveClass',
|
||||||
|
'toHaveClasses',
|
||||||
'toHaveSpyInteractions',
|
'toHaveSpyInteractions',
|
||||||
'toMatch',
|
'toMatch',
|
||||||
'toThrow',
|
'toThrow',
|
||||||
@@ -6605,6 +6606,41 @@ getJasmineRequireObj().toHaveClass = function(j$) {
|
|||||||
return toHaveClass;
|
return toHaveClass;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getJasmineRequireObj().toHaveClasses = function(j$) {
|
||||||
|
/**
|
||||||
|
* {@link expect} the actual value to be a DOM element that has the expected classes
|
||||||
|
* @function
|
||||||
|
* @name matchers#toHaveClasses
|
||||||
|
* @since 5.6.0
|
||||||
|
* @param {Object} expected - The class names to test for
|
||||||
|
* @example
|
||||||
|
* const el = document.createElement('div');
|
||||||
|
* el.className = 'foo bar baz';
|
||||||
|
* expect(el).toHaveClasses(['bar', 'baz']);
|
||||||
|
*/
|
||||||
|
function toHaveClasses(matchersUtil) {
|
||||||
|
return {
|
||||||
|
compare: function(actual, expected) {
|
||||||
|
if (!isElement(actual)) {
|
||||||
|
throw new Error(matchersUtil.pp(actual) + ' is not a DOM element');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
pass: expected.every(e => actual.classList.contains(e))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isElement(maybeEl) {
|
||||||
|
return (
|
||||||
|
maybeEl && maybeEl.classList && j$.isFunction_(maybeEl.classList.contains)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toHaveClasses;
|
||||||
|
};
|
||||||
|
|
||||||
getJasmineRequireObj().toHaveSize = function(j$) {
|
getJasmineRequireObj().toHaveSize = function(j$) {
|
||||||
/**
|
/**
|
||||||
* {@link expect} the actual size to be equal to the expected, using array-like length or object keys size.
|
* {@link expect} the actual size to be equal to the expected, using array-like length or object keys size.
|
||||||
|
|||||||
@@ -621,6 +621,20 @@ describe('Matchers (Integration)', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('toHaveClasses', function() {
|
||||||
|
verifyPasses(function(env) {
|
||||||
|
const el = specHelpers.domHelpers.createElementWithClassName(
|
||||||
|
'foo bar baz'
|
||||||
|
);
|
||||||
|
env.expect(el).toHaveClasses(['bar', 'baz']);
|
||||||
|
});
|
||||||
|
|
||||||
|
verifyFails(function(env) {
|
||||||
|
const el = specHelpers.domHelpers.createElementWithClassName('foo bar');
|
||||||
|
env.expect(el).toHaveClasses(['bar', 'baz']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('toHaveSpyInteractions', function() {
|
describe('toHaveSpyInteractions', function() {
|
||||||
let spyObj;
|
let spyObj;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
|||||||
48
spec/core/matchers/toHaveClassesSpec.js
Normal file
48
spec/core/matchers/toHaveClassesSpec.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
describe('toHaveClasses', function() {
|
||||||
|
it('fails for a DOM element that lacks all the expected classes', function() {
|
||||||
|
const matcher = jasmineUnderTest.matchers.toHaveClasses(),
|
||||||
|
result = matcher.compare(
|
||||||
|
specHelpers.domHelpers.createElementWithClassName(''),
|
||||||
|
['foo', 'bar']
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.pass).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passes for a DOM element that has all the expected classes', function() {
|
||||||
|
const matcher = jasmineUnderTest.matchers.toHaveClasses(),
|
||||||
|
el = specHelpers.domHelpers.createElementWithClassName('foo bar baz');
|
||||||
|
|
||||||
|
expect(matcher.compare(el, ['foo', 'bar']).pass).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails for a DOM element that only has some matching classes', function() {
|
||||||
|
const matcher = jasmineUnderTest.matchers.toHaveClasses(),
|
||||||
|
el = specHelpers.domHelpers.createElementWithClassName('foo bar');
|
||||||
|
|
||||||
|
expect(matcher.compare(el, ['foo', 'can']).pass).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an exception when actual is not a DOM element', function() {
|
||||||
|
const matcher = jasmineUnderTest.matchers.toHaveClasses({
|
||||||
|
pp: jasmineUnderTest.makePrettyPrinter()
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare('x', ['foo']);
|
||||||
|
}).toThrowError("'x' is not a DOM element");
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare(undefined, ['foo']);
|
||||||
|
}).toThrowError('undefined is not a DOM element');
|
||||||
|
|
||||||
|
const textNode = specHelpers.domHelpers.document.createTextNode('');
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare(textNode, ['foo']);
|
||||||
|
}).toThrowError('HTMLNode is not a DOM element');
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
matcher.compare({ classList: '' }, ['foo']);
|
||||||
|
}).toThrowError("Object({ classList: '' }) is not a DOM element");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -27,6 +27,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
|
|||||||
'toHaveBeenCalledTimes',
|
'toHaveBeenCalledTimes',
|
||||||
'toHaveBeenCalledWith',
|
'toHaveBeenCalledWith',
|
||||||
'toHaveClass',
|
'toHaveClass',
|
||||||
|
'toHaveClasses',
|
||||||
'toHaveSpyInteractions',
|
'toHaveSpyInteractions',
|
||||||
'toMatch',
|
'toMatch',
|
||||||
'toThrow',
|
'toThrow',
|
||||||
|
|||||||
34
src/core/matchers/toHaveClasses.js
Normal file
34
src/core/matchers/toHaveClasses.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
getJasmineRequireObj().toHaveClasses = function(j$) {
|
||||||
|
/**
|
||||||
|
* {@link expect} the actual value to be a DOM element that has the expected classes
|
||||||
|
* @function
|
||||||
|
* @name matchers#toHaveClasses
|
||||||
|
* @since 5.6.0
|
||||||
|
* @param {Object} expected - The class names to test for
|
||||||
|
* @example
|
||||||
|
* const el = document.createElement('div');
|
||||||
|
* el.className = 'foo bar baz';
|
||||||
|
* expect(el).toHaveClasses(['bar', 'baz']);
|
||||||
|
*/
|
||||||
|
function toHaveClasses(matchersUtil) {
|
||||||
|
return {
|
||||||
|
compare: function(actual, expected) {
|
||||||
|
if (!isElement(actual)) {
|
||||||
|
throw new Error(matchersUtil.pp(actual) + ' is not a DOM element');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
pass: expected.every(e => actual.classList.contains(e))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isElement(maybeEl) {
|
||||||
|
return (
|
||||||
|
maybeEl && maybeEl.classList && j$.isFunction_(maybeEl.classList.contains)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toHaveClasses;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user