"use strict";
/**
 * Test case for SplitioFeatureFlag
 *
 *  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 chai_1 = require("chai");
var SplitFeatureFlag_1 = require("../../../src/system/flags/SplitFeatureFlag");
describe('SplitioFeatureFlag', function () {
    it("isEnabled retrieves 'on' value and returns true", function () {
        var client = createClient('on');
        var default_flag = createDefault();
        var sut = new SplitFeatureFlag_1.SplitFeatureFlag(default_flag, {}, client);
        return chai_1.expect(sut.isEnabled('liza_autosave')).to.eventually.be.true;
    });
    it("isEnabled retrieves 'off' value and returns false", function () {
        var client = createClient('off');
        var default_flag = createDefault();
        var sut = new SplitFeatureFlag_1.SplitFeatureFlag(default_flag, {}, client);
        return chai_1.expect(sut.isEnabled('liza_autosave')).to.eventually.be.false;
    });
    it('isEnabled uses defaults when the response is not on or off', function () {
        var default_called = false;
        var client = createClient('foo');
        var default_flag = createDefault();
        default_flag.isEnabled = function (_) {
            default_called = true;
            return Promise.resolve(false);
        };
        var sut = new SplitFeatureFlag_1.SplitFeatureFlag(default_flag, {}, client);
        return chai_1.expect(sut.isEnabled('liza_autosave')).to.eventually.be.false.then(function () { return chai_1.expect(default_called).to.be.true; });
    });
    describe('base conditions are joined with isEnabled conditions', function () {
        [
            {
                label: 'Newly supplied variables are added to base',
                base: { foo: 'bar1' },
                supplied: { bar: 'baz' },
                expected: { foo: 'bar1', bar: 'baz' },
            },
            {
                label: 'Newly supplied variables override base',
                base: { foo: 'bar1' },
                supplied: { foo: 'baz' },
                expected: { foo: 'baz' },
            },
        ].forEach(function (_a) {
            var label = _a.label, base = _a.base, supplied = _a.supplied, expected = _a.expected;
            it(label, function () {
                var given_key = 'liza_autosave';
                var treatment_called = false;
                var client = createClient('on');
                var default_flag = createDefault();
                client.getTreatment = function (_, key, atts) {
                    chai_1.expect(key).to.equal(given_key);
                    chai_1.expect(atts).to.deep.equal(expected);
                    treatment_called = true;
                    return Promise.resolve('foo');
                };
                var sut = new SplitFeatureFlag_1.SplitFeatureFlag(default_flag, base, client);
                return chai_1.expect(sut.isEnabled(given_key, supplied))
                    .to.eventually.equal(false)
                    .then(function () { return chai_1.expect(treatment_called).to.be.true; });
            });
        });
    });
});
function createDefault(defaults) {
    if (defaults === void 0) { defaults = {}; }
    return {
        close: function () { },
        isEnabled: function (key) {
            return Promise.resolve(defaults[key] || false);
        },
    };
}
function createClient(treatment_value) {
    if (treatment_value === void 0) { treatment_value = ''; }
    return {
        destroy: function () { },
        getTreatment: function (_, __, ___) {
            return Promise.resolve(treatment_value);
        },
    };
}
//# sourceMappingURL=data:application/json;base64, 