"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var chai_1 = require("chai");
var memFs = require("mem-fs");
var fsEditor = require("mem-fs-editor");
var prettier_1 = require("prettier");
var migrate_app_js_1 = require("../../steps/migrate_app_js");
var immutable_1 = require("immutable");
describe("migrate app js", function () {
    var editor;
    var options;
    var cwd = process.cwd();
    var src = cwd + "/src/test/fixtures/migrate_js_app";
    var dest = cwd + "/tmp/test/migrate_js_app";
    beforeEach(function () {
        editor = fsEditor.create(memFs.create());
        options = immutable_1.Map({ src: src, dest: dest, editor: editor });
    });
    describe("with an empty v1 app.js file", function () {
        beforeEach(function () {
            editor.write(src + "/app.js", "");
        });
        it("should insert no-op app code into legacy_app.js", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            var code, expected;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, migrate_app_js_1.default(options)];
                    case 1:
                        _a.sent();
                        code = prettier_1.format(editor.read(dest + "/src/javascripts/legacy_app.js"));
                        expected = prettier_1.format("\n      const App = (function() {\n        return {\n          /* no-op */\n        };\n      }());\n      ");
                        chai_1.expect(code).to.have.string(expected);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe("with a v1 app.js file", function () {
        it("should extract the v1 return statement into src/javascripts/legacy_app.js", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            var code, expected;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, migrate_app_js_1.default(options)];
                    case 1:
                        _a.sent();
                        code = prettier_1.format(editor.read(dest + "/src/javascripts/legacy_app.js"));
                        expected = prettier_1.format("\n      const App = (function() {\n        return {\n          events: { \"app.activated\": \"init\" },\n          foo: require(\"foo\"),\n          init() {}\n        };\n      })()\n    ");
                        chai_1.expect(code).to.have.string(expected);
                        return [2 /*return*/];
                }
            });
        }); });
        it("should ignore any code outside of the v1 IIFE", 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_app_js_1.default(options)];
                    case 1:
                        _a.sent();
                        code = prettier_1.format(editor.read(dest + "/src/javascripts/legacy_app.js"));
                        chai_1.expect(code).not.to.match(/function someNaughtyFunction\(\)/);
                        return [2 /*return*/];
                }
            });
        }); });
        describe("when there are common js modules", function () {
            beforeEach(function () {
                options = options.set("hasCommonJS", true);
            });
            it("should update require statements in app.js to be relative", 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_app_js_1.default(options)];
                        case 1:
                            _a.sent();
                            chai_1.expect(editor.read(dest + "/src/javascripts/legacy_app.js")).to.match(/require\("\.\/lib\/foo"\)/);
                            return [2 /*return*/];
                    }
                });
            }); });
        });
    });
});
