All files / universal/gpii/node_modules/lifecycleManager/test/js LifecycleManagerQueueTests.js

94.64% Statements 53/56
100% Branches 2/2
80.95% Functions 17/21
94.64% Lines 53/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                  2x   2x   2x   2x                         2x 18x 14x 14x 14x 14x   14x                 2x 14x             2x 2x 2x   2x   2x 2x 2x         2x 2x 2x 2x 6x     2x   2x 2x 2x   2x 2x 2x       2x 2x 2x   2x       2x   2x       2x 2x       2x 2x 2x   2x         2x 2x     2x     2x     2x   2x   2x     2x 2x          
/*!
GPII Lifecycle Manager Queue Tests
 
Copyright 2017 Raising The Floor - International
 
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
 
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
 
/* eslint-env browser */
/* eslint strict: ["error", "function"] */
 
/* global jqUnit, fluid */
 
(function () {
    "use strict";
    fluid.setLogging(true);
 
    var gpii = fluid.registerNamespace("gpii");
 
    fluid.registerNamespace("gpii.tests.lifecycleManager.queue");
 
    /**
     * Creates (and returns) an asynchronous function that returns a promise.
     * The function will wait the given `timeout` ms, the run the provided function and finally resolve (or reject) the promise.
     * The promise will be rejected if the `reject` parameter is a truthy. The value passed
     * in the rejection or resolving of the promise will be the same value that the created function
     * is called with
     *
     * @param timeout {int} the number of ms the function should wait before resolving (or rejecting) the promise
     * @param func {String (or function)} The function to call after `timeout` ms, immediately before resolving or rejecting the promise
     * @param reject {boolean} If true, this will cause the promise to be rejected instead of resolved
     */
    gpii.tests.lifecycleManager.queue.createAsyncPromiseFunc = function (timeout, func, reject) {
        return function (arg) {
            var myPromise = fluid.promise();
            setTimeout(function () {
                func(arg);
                reject ? myPromise.reject(arg) : myPromise.resolve(arg);
            }, timeout);
            return myPromise;
        };
    };
 
    /**
     * Creates an item suitable for the lifecycle managers queue.
     * The function provided to the queue-item is generated by gpii.tests.lifecycleManager.queue.createAsyncPromiseFunc
     * using the timeout and func parameters.
     */
    gpii.tests.lifecycleManager.queue.createCustomTask = function (timeout, func, arg) {
        return {
            func: gpii.tests.lifecycleManager.queue.createAsyncPromiseFunc(timeout, func),
            arg: arg
        };
    };
 
    // ensure that we're processing queue in the correct order:
    jqUnit.asyncTest("Tasks are being processed on addition", function () {
        var lifecycleManager = gpii.lifecycleManager();
        jqUnit.expect(1);
 
        var queuePromise = lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(1, fluid.identity, 1));
 
        queuePromise.then(function (val) {
            jqUnit.assertEquals("Task was processed on addition to queue", 1, val);
            jqUnit.start();
        });
    });
 
    // ensure that we're processing queue in the correct order:
    jqUnit.asyncTest("Queue is being processed in the correct order", function () {
        var lifecycleManager = gpii.lifecycleManager();
        var results = [];
        var pushResultFunc = function (arg) {
            results.push(arg);
        };
 
        jqUnit.expect(1);
 
        lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(300, pushResultFunc, 1));
        lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(50, pushResultFunc, 2));
        var queuePromise3 = lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(100, pushResultFunc, 3));
 
        queuePromise3.then(function () {
            jqUnit.assertDeepEq("Task was processed on addition to queue", [1, 2, 3], results);
            jqUnit.start();
        });
    });
 
    jqUnit.asyncTest("Rejected promises causes the queue promise to be rejected", function () {
        var lifecycleManager = gpii.lifecycleManager();
        jqUnit.expect(1);
 
        var task = {
            func: gpii.tests.lifecycleManager.queue.createAsyncPromiseFunc(200, fluid.identity, true),
            promise: fluid.promise()
        };
        var queuePromise = lifecycleManager.addToQueue(task);
 
        queuePromise.then(function () {
            jqUnit.fail("Rejected task should reject promise");
            jqUnit.start();
        }, function () {
            jqUnit.assertTrue("Task failed and promise was rejected", true);
            jqUnit.start();
        });
    });
 
    jqUnit.asyncTest("All promises in queue are rejected when a task fail", function () {
        var lifecycleManager = gpii.lifecycleManager();
        jqUnit.expect(4);
 
        var failTask = {
            func: gpii.tests.lifecycleManager.queue.createAsyncPromiseFunc(200, fluid.identity, true),
            promise: fluid.promise()
        };
        // first task that succeeds
        lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(300, fluid.identity)).then(function () {
            jqUnit.assertTrue("First task should succeed", true);
        });
        // next task fails
        lifecycleManager.addToQueue(failTask).then(function () {
            jqUnit.fail("Second task should fail");
        }, function () {
            jqUnit.assertTrue("Second task failed as expected", true);
        });
        // subsequent tasks fail:
        lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(50, fluid.identity)).then(
            function () {},
            function () { jqUnit.assertTrue("third task failed as expected", true); }
        );
        lifecycleManager.addToQueue(gpii.tests.lifecycleManager.queue.createCustomTask(50, fluid.identity)).then(
            function () {},
            function () {
                jqUnit.assertTrue("fourth task failed as expected", true);
                jqUnit.start();
            }
        );
    });
})();