atsid/circuits-js

View on GitHub
test/TestRetryOnErrorPlugin.js

Summary

Maintainability
A
0 mins
Test Coverage
define([
    "circuits/plugins/RetryOnErrorPlugin"
], function (
    RetryOnErrorPlugin
) {

    var b;

    /**
     * Test the RetryOnErrorPlugin in isolation.
     */
    describe("TestRetryOnErrorPlugin", function() {

        beforeEach(function () {
        });

        /**
         * Test that the request is re-executed the correct number of times before
         * calling the configured error function.
         */
        it("testSimpleRetry",  function () {
            var numberOfTries = 0,
                fnCalled = false,
                mscope = {mockScope: true},
                plugin = new RetryOnErrorPlugin({
                    retry:  10,
                    scope: mscope,
                    callback: function (data, params) {
                        if (this.mockScope) {
                            fnCalled = true;
                        }
                    }
                }),
                mockRequest = {
                    execute: function () {
                        numberOfTries += 1;
                        plugin.fn.call(this, {}, {request: mockRequest});
                    }
                };
            mockRequest.execute();
            assert.isTrue(fnCalled);
            // the initial execute plus the ten retries.
            assert.equal(11, numberOfTries);

            // test the plugin resets properly.
            numberOfTries = 0;
            fnCalled = false;
            mockRequest.execute();
            assert.isTrue(fnCalled);
            // the initial execute plus the ten retries.
            assert.equal(11, numberOfTries);
        });

    });
});