1'use strict'; 2require('../common'); 3const assert = require('assert'); 4 5/** 6 * Checks the expected invocations against the invocations that actually 7 * occurred. 8 * @name checkInvocations 9 * @function 10 * @param {object} activity including timestamps for each life time event, 11 * i.e. init, before ... 12 * @param {object} hooks the expected life time event invocations with a count 13 * indicating how often they should have been invoked, 14 * i.e. `{ init: 1, before: 2, after: 2 }` 15 * @param {string} stage the name of the stage in the test at which we are 16 * checking the invocations 17 */ 18exports.checkInvocations = function checkInvocations(activity, hooks, stage) { 19 const stageInfo = `Checking invocations at stage "${stage}":\n `; 20 21 assert.ok(activity != null, 22 `${stageInfo} Trying to check invocation for an activity, ` + 23 'but it was empty/undefined.', 24 ); 25 26 // Check that actual invocations for all hooks match the expected invocations 27 [ 'init', 'before', 'after', 'destroy', 'promiseResolve' ].forEach(checkHook); 28 29 function checkHook(k) { 30 const val = hooks[k]; 31 // Not expected ... all good 32 if (val == null) return; 33 34 if (val === 0) { 35 // Didn't expect any invocations, but it was actually invoked 36 const invocations = activity[k].length; 37 const msg = `${stageInfo} Called "${k}" ${invocations} time(s), ` + 38 'but expected no invocations.'; 39 assert(activity[k] === null && activity[k] === undefined, msg); 40 } else { 41 // Expected some invocations, make sure that it was invoked at all 42 const msg1 = `${stageInfo} Never called "${k}", ` + 43 `but expected ${val} invocation(s).`; 44 assert(activity[k] !== null && activity[k] !== undefined, msg1); 45 46 // Now make sure that the expected count and 47 // the actual invocation count match 48 const msg2 = `${stageInfo} Called "${k}" ${activity[k].length} ` + 49 `time(s), but expected ${val} invocation(s).`; 50 assert.strictEqual(activity[k].length, val, msg2); 51 } 52 } 53}; 54