"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var chai_1 = require("chai");
var shelljs_1 = require("shelljs");
var memFs = require("mem-fs");
var fsEditor = require("mem-fs-editor");
var migrate_common_js_1 = require("../../steps/migrate_common_js");
var immutable_1 = require("immutable");
describe("migrate common js", function () {
    var editor;
    var options;
    var cwd = process.cwd();
    var src = cwd + "/src/test/fixtures/common_js_sample_app";
    var dest = cwd + "/tmp/test/common_js_sample_app";
    beforeEach(function () {
        shelljs_1.mkdir("-p", dest + "/src/javascripts");
        editor = fsEditor.create(memFs.create());
        options = immutable_1.Map({ src: src, dest: dest, editor: editor });
    });
    afterEach(function () { return shelljs_1.rm("-rf", dest); });
    describe("with no v1 `lib` folder", function () {
        before(function () {
            shelljs_1.cp("-R", src + "/lib/", src + "/_lib");
            shelljs_1.rm("-rf", src + "/lib/");
        });
        after(function () {
            shelljs_1.cp("-R", src + "/_lib/", src + "/lib");
            shelljs_1.rm("-rf", src + "/_lib/");
        });
        it("does nothing", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, migrate_common_js_1.default(options)];
                    case 1:
                        options = _a.sent();
                        chai_1.expect(options).not.to.exist;
                        chai_1.expect(editor.exists(dest + "/src/javascripts/lib/utilities.js")).to.be
                            .false;
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe("with a v1 `lib` folder", function () {
        it("copies the lib folder to the destination", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, migrate_common_js_1.default(options)];
                    case 1:
                        options = _a.sent();
                        chai_1.expect(options.has("hasCommonJS")).to.be.true;
                        return [2 /*return*/];
                }
            });
        }); });
        it("rewrites require statements within to be relative", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            var code;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, migrate_common_js_1.default(options)];
                    case 1:
                        _a.sent();
                        code = editor.read(dest + "/src/javascripts/lib/utilities.js");
                        chai_1.expect(code).to.match(/require\("\.\/utilities\/string\.js"\)/);
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
