Create toHaveSpyInteractions matcher
This matcher checks all the properties of a given spy object and checks whether at least one of the spies has been called. It returns true if one or more of the spies of the spy object has been called and false otherwise.
This commit is contained in:
1
src/core/matchers/requireMatchers.js
Normal file → Executable file
1
src/core/matchers/requireMatchers.js
Normal file → Executable file
@@ -27,6 +27,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
|
||||
'toHaveBeenCalledTimes',
|
||||
'toHaveBeenCalledWith',
|
||||
'toHaveClass',
|
||||
'toHaveSpyInteractions',
|
||||
'toMatch',
|
||||
'toThrow',
|
||||
'toThrowError',
|
||||
|
||||
73
src/core/matchers/toHaveSpyInteractions.js
Executable file
73
src/core/matchers/toHaveSpyInteractions.js
Executable file
@@ -0,0 +1,73 @@
|
||||
getJasmineRequireObj().toHaveSpyInteractions = function(j$) {
|
||||
var getErrorMsg = j$.formatErrorMsg(
|
||||
'<toHaveSpyInteractions>',
|
||||
'expect(<spyObj>).toHaveSpyInteractions()'
|
||||
);
|
||||
|
||||
/**
|
||||
* {@link expect} the actual (a {@link SpyObj}) spies to have been called.
|
||||
* @function
|
||||
* @name matchers#toHaveSpyInteractions
|
||||
* @since 4.0.0
|
||||
* @example
|
||||
* expect(mySpyObj).toHaveSpyInteractions();
|
||||
* expect(mySpyObj).not.toHaveSpyInteractions();
|
||||
*/
|
||||
function toHaveSpyInteractions(matchersUtil) {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
var result = {};
|
||||
|
||||
if (!j$.isObject_(actual)) {
|
||||
throw new Error(
|
||||
getErrorMsg(
|
||||
'Expected a spy object, but got ' + matchersUtil.pp(actual) + '.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (arguments.length > 1) {
|
||||
throw new Error(getErrorMsg('Does not take arguments'));
|
||||
}
|
||||
|
||||
result.pass = false;
|
||||
let hasSpy = false;
|
||||
const calledSpies = [];
|
||||
for (const spy of Object.values(actual)) {
|
||||
if (!j$.isSpy(spy)) continue;
|
||||
hasSpy = true;
|
||||
|
||||
if (spy.calls.any()) {
|
||||
result.pass = true;
|
||||
calledSpies.push([spy.and.identity, spy.calls.count()]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasSpy) {
|
||||
throw new Error(
|
||||
getErrorMsg(
|
||||
'Expected a spy object with spies, but object has no spies.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let resultMessage = 'Expected spy object spies to have been called';
|
||||
if (result.pass) {
|
||||
resultMessage += ', the following spies were called: ';
|
||||
resultMessage += calledSpies
|
||||
.map(([spyName, spyCount]) => {
|
||||
return `${spyName} called ${spyCount} time(s)`;
|
||||
})
|
||||
.join(', ');
|
||||
} else {
|
||||
resultMessage += ', but no spies were called.';
|
||||
}
|
||||
result.message = resultMessage;
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toHaveSpyInteractions;
|
||||
};
|
||||
Reference in New Issue
Block a user