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