2016-11-22 16:13:44 +00:00
|
|
|
'use strict';
|
|
|
|
require('../common');
|
2019-05-12 07:11:13 +00:00
|
|
|
const assert = require('assert');
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the expected invocations against the invocations that actually
|
|
|
|
* occurred.
|
|
|
|
*
|
|
|
|
* @name checkInvocations
|
|
|
|
* @function
|
|
|
|
* @param {Object} activity including timestamps for each life time event,
|
|
|
|
* i.e. init, before ...
|
|
|
|
* @param {Object} hooks the expected life time event invocations with a count
|
2018-01-06 18:34:27 +00:00
|
|
|
* indicating how often they should have been invoked,
|
2016-11-22 16:13:44 +00:00
|
|
|
* i.e. `{ init: 1, before: 2, after: 2 }`
|
|
|
|
* @param {String} stage the name of the stage in the test at which we are
|
|
|
|
* checking the invocations
|
|
|
|
*/
|
|
|
|
exports.checkInvocations = function checkInvocations(activity, hooks, stage) {
|
|
|
|
const stageInfo = `Checking invocations at stage "${stage}":\n `;
|
|
|
|
|
|
|
|
assert.ok(activity != null,
|
|
|
|
`${stageInfo} Trying to check invocation for an activity, ` +
|
|
|
|
'but it was empty/undefined.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that actual invocations for all hooks match the expected invocations
|
2018-05-09 13:05:51 +00:00
|
|
|
[ 'init', 'before', 'after', 'destroy', 'promiseResolve' ].forEach(checkHook);
|
2016-11-22 16:13:44 +00:00
|
|
|
|
|
|
|
function checkHook(k) {
|
|
|
|
const val = hooks[k];
|
|
|
|
// Not expected ... all good
|
|
|
|
if (val == null) return;
|
|
|
|
|
|
|
|
if (val === 0) {
|
|
|
|
// Didn't expect any invocations, but it was actually invoked
|
|
|
|
const invocations = activity[k].length;
|
|
|
|
const msg = `${stageInfo} Called "${k}" ${invocations} time(s), ` +
|
|
|
|
'but expected no invocations.';
|
|
|
|
assert(activity[k] === null && activity[k] === undefined, msg);
|
|
|
|
} else {
|
|
|
|
// Expected some invocations, make sure that it was invoked at all
|
|
|
|
const msg1 = `${stageInfo} Never called "${k}", ` +
|
|
|
|
`but expected ${val} invocation(s).`;
|
|
|
|
assert(activity[k] !== null && activity[k] !== undefined, msg1);
|
|
|
|
|
|
|
|
// Now make sure that the expected count and
|
|
|
|
// the actual invocation count match
|
|
|
|
const msg2 = `${stageInfo} Called "${k}" ${activity[k].length} ` +
|
|
|
|
`time(s), but expected ${val} invocation(s).`;
|
|
|
|
assert.strictEqual(activity[k].length, val, msg2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|