var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
define(["require", "exports", "chai", "sinon-chai", "wed/domutil", "wed/key-constants", "wed/validator", "../base-config", "../wed-test-util"], function (require, exports, chai_1, sinon_chai_1, domutil_1, keyConstants, validator_1, globalConfig, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    sinon_chai_1 = __importDefault(sinon_chai_1);
    keyConstants = __importStar(keyConstants);
    globalConfig = __importStar(globalConfig);
    chai_1.use(sinon_chai_1.default);
    const expect = chai.expect;
    describe("wed validation:", () => {
        let setup;
        let editor;
        let caretManager;
        let validator;
        let controller;
        let processRunner;
        // let refreshRunner: TaskRunner;
        let guiRoot;
        let ps;
        let startSpy;
        before(() => __awaiter(this, void 0, void 0, function* () {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/wed_test_data/source_converted.xml`, globalConfig.config, document);
            ({ editor } = setup);
            yield setup.init();
            validator = editor.validator;
            // tslint:disable-next-line:no-any
            processRunner = editor.validationController.processErrorsRunner;
            // tslint:disable-next-line:no-any
            // refreshRunner = (editor as any).validationController.refreshErrorsRunner;
            caretManager = editor.caretManager;
            // tslint:disable-next-line:no-any
            controller = editor.validationController;
            guiRoot = editor.guiRoot;
            startSpy = setup.sandbox.spy(validator, "start");
        }));
        beforeEach(() => __awaiter(this, void 0, void 0, function* () {
            // tslint:disable-next-line:no-any
            validator._validateUpTo(editor.dataRoot, -1);
            ps = guiRoot.querySelectorAll(".body .p");
            // Force the processing of errors
            controller.processErrors();
            yield processRunner.onCompleted();
            // We want to start with a fresh state for the spies, etc.
            setup.sandbox.reset();
        }));
        afterEach(() => {
            setup.reset();
            setup.sandbox.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
            // tslint:disable-next-line:no-any
            caretManager = undefined;
            // tslint:disable-next-line:no-any
            controller = undefined;
        });
        it("restarts validation when deleting elements", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            const p = ps[0];
            const dataP = editor.toDataNode(p);
            const parent = dataP.parentElement;
            caretManager.setCaret(parent, domutil_1.indexOf(parent.childNodes, dataP));
            const trs = editor.modeTree.getMode(dataP).getContextualActions(["delete-element"], "p", dataP);
            trs[0].execute({ node: p });
            expect(startSpy).to.have.been.calledOnce;
        });
        it("restarts validation when adding elements", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            const initial = editor.toDataNode(ps[4]);
            // Make sure we are looking at the right thing.
            expect(initial.childNodes).to.have.lengthOf(1);
            expect(initial).to.have.property("textContent").equal("abcdefghij");
            const trs = editor.modeTree.getMode(initial)
                .getContextualActions(["wrap"], "hi", initial, 0);
            caretManager.setCaret(initial.firstChild, 3);
            const caret = caretManager.getNormalizedCaret();
            caretManager.setRange(caret, caret.makeWithOffset(caret.offset + 2));
            trs[0].execute({ node: undefined, name: "hi" });
            expect(startSpy).to.have.been.calledOnce;
        });
        it("restarts validation when inserting text", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            const initial = editor.toDataNode(ps[4]);
            // Make sure we are looking at the right thing.
            expect(initial.childNodes).to.have.lengthOf(1);
            expect(initial).to.have.property("textContent").equal("abcdefghij");
            caretManager.setCaret(initial.firstChild, 3);
            editor.type("f");
            expect(startSpy).to.have.been.calledOnce;
        });
        it("restarts validation when deleting text", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            const initial = editor.toDataNode(ps[4]);
            // Make sure we are looking at the right thing.
            expect(initial.childNodes).to.have.lengthOf(1);
            expect(initial).to.have.property("textContent").equal("abcdefghij");
            caretManager.setCaret(initial.firstChild, 3);
            editor.type(keyConstants.DELETE);
            expect(startSpy).to.have.been.calledOnce;
        });
        it("restarts validation when cutting text", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            const initial = editor.toDataNode(ps[4]);
            // Make sure we are looking at the right thing.
            expect(initial.childNodes).to.have.lengthOf(1);
            expect(initial).to.have.property("textContent").equal("abcdefghij");
            caretManager.setCaret(initial.firstChild, 3);
            const caret = caretManager.getNormalizedCaret();
            caretManager.setRange(caret, caret.makeWithOffset(caret.offset + 2));
            // With the selection in effect, this has for effect to delete the
            // selection.
            editor.type(keyConstants.DELETE);
            expect(initial).to.have.property("textContent").equal("abcfghij");
            expect(startSpy).to.have.been.calledOnce;
        });
        it("restarts validation when editing attributes", () => {
            expect(validator.getWorkingState()).to.have.property("state")
                .equal(validator_1.INVALID);
            let initial = wed_test_util_1.getAttributeValuesFor(ps[7])[0].firstChild;
            caretManager.setCaret(initial, 0);
            expect(initial).to.have.property("data").equal("rend_value");
            editor.type("x");
            // We have to refetch because the decorations have been redone.
            initial = wed_test_util_1.getAttributeValuesFor(ps[7])[0].firstChild;
            expect(initial).to.have.property("data").equal("xrend_value");
            expect(startSpy).to.have.been.calledOnce;
        });
    });
});
//# sourceMappingURL=wed-validation-test.js.map