"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var mock = require("mock-fs");
var config_1 = require("./config");
describe('Config class', function () {
    var name = 'dev';
    var server = 'localhost';
    var port = 1433;
    var database = 'awesome-db';
    var user = 'example';
    var password = 'qwerty';
    var connection = { name: name, server: server, port: port, database: database, user: user, password: password };
    var files = ['dbo.*'];
    var data = ['dbo.LookupTable'];
    var output = { root: './my-database' };
    var idempotency = { triggers: false };
    describe('write method', function () {
        it('should write to default file', function () {
            // todo (jbl): error thrown with nyc
            // mock();
            // Config.write({
            //   connections: [connection]
            // });
            // const config: Config = new Config();
            // const conn: Connection = config.connections[0] as Connection;
            // expect(conn.name).toEqual(name);
            // expect(conn.server).toEqual(server);
            // expect(conn.port).toEqual(port);
            // expect(conn.database).toEqual(database);
            // expect(conn.user).toEqual(user);
            // expect(conn.password).toEqual(password);
            // mock.restore();
        });
    });
    describe('doesDefaultExist method', function () {
        it('should return true if file exists', function () {
            var _a;
            var file = config_1.default.defaultConfigFile;
            mock((_a = {},
                _a[file] = '',
                _a));
            var value = config_1.default.doesDefaultExist();
            expect(value).toEqual(true);
            mock.restore();
        });
    });
    describe('doesDefaultExist method', function () {
        it('should return false if file not exists', function () {
            mock();
            var value = config_1.default.doesDefaultExist();
            expect(value).toEqual(false);
            mock.restore();
        });
    });
    describe('getConnectionsFromWebConfig method', function () {
        it('should return connections if default web.config exists', function () {
            var _a;
            var file = config_1.default.defaultWebConfigFile;
            mock((_a = {},
                _a[file] = "\n          <?xml version=\"1.0\" encoding=\"utf-8\"?>\n          <configuration>\n            <connectionStrings>\n            <add\n              name=\"" + name + "\"\n              connectionString=\"server=" + server + ";database=" + database + ";uid=" + user + ";password=" + password + ";\" />\n            </connectionStrings>\n          </configuration>\n        ",
                _a));
            var conns = config_1.default.getConnectionsFromWebConfig();
            var conn = conns[0];
            expect(conn.name).toEqual(name);
            expect(conn.server).toEqual(server);
            expect(conn.port).toBeUndefined();
            expect(conn.database).toEqual(database);
            expect(conn.user).toEqual(user);
            expect(conn.password).toEqual(password);
            mock.restore();
        });
        it('should return undefined if web.config not exists', function () {
            mock();
            var conns = config_1.default.getConnectionsFromWebConfig();
            expect(conns).toBeUndefined();
            mock.restore();
        });
    });
    describe('constructor', function () {
        it('should load from default file', function () {
            var _a;
            var file = config_1.default.defaultConfigFile;
            mock((_a = {},
                _a[file] = JSON.stringify({
                    connections: [connection],
                    files: files,
                    data: data,
                    output: output,
                    idempotency: idempotency
                }),
                _a));
            var config = new config_1.default();
            var conn = config.connections[0];
            expect(conn.name).toEqual(name);
            expect(conn.server).toEqual(server);
            expect(conn.port).toEqual(port);
            expect(conn.database).toEqual(database);
            expect(conn.user).toEqual(user);
            expect(conn.password).toEqual(password);
            expect(config.files).toEqual(files);
            expect(config.data).toEqual(data);
            expect(config.output.root).toEqual(output.root);
            expect(config.idempotency.triggers).toEqual(idempotency.triggers);
            mock.restore();
        });
        it('should load from specified file', function () {
            var _a;
            var file = 'override-example.json';
            mock((_a = {},
                _a[file] = JSON.stringify({
                    connections: [connection],
                    files: files,
                    data: data,
                    output: output,
                    idempotency: idempotency
                }),
                _a));
            var config = new config_1.default(file);
            var conn = config.connections[0];
            expect(conn.name).toEqual(name);
            expect(conn.server).toEqual(server);
            expect(conn.port).toEqual(port);
            expect(conn.database).toEqual(database);
            expect(conn.user).toEqual(user);
            expect(conn.password).toEqual(password);
            expect(config.files).toEqual(files);
            expect(config.data).toEqual(data);
            expect(config.output.root).toEqual(output.root);
            expect(config.idempotency.triggers).toEqual(idempotency.triggers);
            mock.restore();
        });
    });
    describe('getConnection method', function () {
        it('should return first connection', function () {
            var _a;
            var file = config_1.default.defaultConfigFile;
            mock((_a = {},
                _a[file] = JSON.stringify({
                    connections: [connection]
                }),
                _a));
            var config = new config_1.default();
            var conn = config.getConnection();
            expect(conn.name).toEqual(name);
            expect(conn.server).toEqual(server);
            expect(conn.port).toEqual(port);
            expect(conn.database).toEqual(database);
            expect(conn.user).toEqual(user);
            expect(conn.password).toEqual(password);
            mock.restore();
        });
        it('should return connection by name', function () {
            var _a;
            var file = config_1.default.defaultConfigFile;
            mock((_a = {},
                _a[file] = JSON.stringify({
                    connections: [connection]
                }),
                _a));
            var config = new config_1.default();
            var conn = config.getConnection(name);
            expect(conn.name).toEqual(name);
            expect(conn.server).toEqual(server);
            expect(conn.port).toEqual(port);
            expect(conn.database).toEqual(database);
            expect(conn.user).toEqual(user);
            expect(conn.password).toEqual(password);
            mock.restore();
        });
    });
    describe('getConnections method', function () {
        it('should return all conneections', function () {
            var _a;
            var file = config_1.default.defaultConfigFile;
            mock((_a = {},
                _a[file] = JSON.stringify({
                    connections: [connection]
                }),
                _a));
            var config = new config_1.default();
            var conns = config.getConnections();
            expect(conns.length).toEqual(1);
            mock.restore();
        });
    });
});
//# sourceMappingURL=config.spec.js.map