"use strict";
/**
 * Test case for Client Quote Hooks
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var ClientQuoteHooks_1 = require("../../../src/client/quote/ClientQuoteHooks");
var chai_1 = require("chai");
describe('createQuoteStagingHook', function () {
    it('Do not hook quote when autosave is missing', function () {
        var _a = createStubs({ foo: [1] }), client = _a.client, quote = _a.quote, program = _a.program, transport = _a.transport;
        var quote_hooked = false;
        program.autosave = false;
        quote.on = function (_) {
            quote_hooked = true;
            return quote;
        };
        ClientQuoteHooks_1.createQuoteStagingHook(client, program, transport)(quote);
        chai_1.expect(quote_hooked).to.be.false;
    });
    it('Do not hook quote when quote is locked', function () {
        var _a = createStubs({ foo: [1] }), client = _a.client, quote = _a.quote, program = _a.program, transport = _a.transport;
        var quote_hooked = false;
        quote.isLocked = function () { return true; };
        quote.on = function (_) {
            quote_hooked = true;
            return quote;
        };
        ClientQuoteHooks_1.createQuoteStagingHook(client, program, transport)(quote);
        chai_1.expect(quote_hooked).to.be.false;
    });
    [
        {
            label: 'hook calls autosave',
            diff: { bar: [1] },
            save_pending: false,
            navigation_pending: false,
            expected_autosave_called: true,
        },
        {
            label: 'does not autosave an empty diff',
            diff: {},
            save_pending: false,
            navigation_pending: false,
            expected_autosave_called: false,
        },
        {
            label: 'does not autosave a diff with only empty arrays',
            diff: { foo: [] },
            save_pending: false,
            navigation_pending: false,
            expected_autosave_called: false,
        },
        {
            label: 'does not autosave when step save is in progress',
            diff: { foo: [1] },
            save_pending: true,
            navigation_pending: false,
            expected_autosave_called: false,
        },
        {
            label: 'does not autosave when navigation is in progress',
            diff: { foo: [1] },
            save_pending: false,
            navigation_pending: true,
            expected_autosave_called: false,
        },
    ].forEach(function (_a) {
        var label = _a.label, diff = _a.diff, save_pending = _a.save_pending, navigation_pending = _a.navigation_pending, expected_autosave_called = _a.expected_autosave_called;
        it(label, function () {
            var _a = createStubs(diff, save_pending, navigation_pending), client = _a.client, quote = _a.quote, program = _a.program, transport = _a.transport;
            var autosave_called = false;
            quote.autosave = function (_) {
                autosave_called = true;
                return quote;
            };
            ClientQuoteHooks_1.createQuoteStagingHook(client, program, transport)(quote);
            chai_1.expect(autosave_called).to.equal(expected_autosave_called);
        });
    });
});
function createStubClientQuote(diff) {
    if (diff === void 0) { diff = {}; }
    return {
        on: function (_, cb) { return cb(diff); },
        autosave: function (_) { },
        isLocked: function () { return false; },
        invalidateAutosave: function () { return false; },
    };
}
function createStubClient(ui, navigation_pending) {
    return {
        isNavigating: function () { return navigation_pending; },
        getUi: function () { return ui; },
    };
}
function createStubUi(save_pending) {
    return {
        isSaving: function () { return save_pending; },
    };
}
function createStubProgram() {
    return {
        autosave: true,
    };
}
function createStubTransport() {
    return {};
}
function createStubs(diff, save_pending, navigation_pending) {
    if (diff === void 0) { diff = {}; }
    if (save_pending === void 0) { save_pending = false; }
    if (navigation_pending === void 0) { navigation_pending = false; }
    var ui = createStubUi(save_pending);
    var client = createStubClient(ui, navigation_pending);
    var quote = createStubClientQuote(diff);
    var program = createStubProgram();
    var transport = createStubTransport();
    return {
        client: client,
        ui: ui,
        quote: quote,
        program: program,
        transport: transport,
    };
}
//# sourceMappingURL=data:application/json;base64, 