/**
 * DevExtreme (esm/__internal/scheduler/options_validator/core/options_validator_error_handler.test.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    OptionsValidatorErrorHandler
} from "../../../scheduler/options_validator/core/options_validator_error_handler";
class TestErrorHandler extends OptionsValidatorErrorHandler {
    constructor(errorsMap, globalErrorHandler) {
        super(errorsMap, globalErrorHandler)
    }
}
var errorMap = {
    A: "E0",
    B: "E1",
    C: "E2",
    D: "E3"
};
var createGlobalErrorHandlerMock = () => ({
    logError: jest.fn(),
    throwError: jest.fn()
});
describe("OptionsValidatorErrorHandler", () => {
    it('shouldn\'t call global error handler if options validator result is "true"', () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler(errorMap, globalErrorHandler);
        handler.handleValidationResult(true);
        expect(globalErrorHandler.logError).not.toHaveBeenCalled();
        expect(globalErrorHandler.throwError).not.toHaveBeenCalled()
    });
    it("shouldn't call global error handler if there is no error codes for validator's errors", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler({
            B: "E1"
        }, globalErrorHandler);
        handler.handleValidationResult({
            A: {
                fist: "error",
                second: "error"
            },
            C: {
                first: "error"
            },
            D: {
                some: "error"
            }
        });
        expect(globalErrorHandler.logError).not.toHaveBeenCalled();
        expect(globalErrorHandler.throwError).not.toHaveBeenCalled()
    });
    it("should log validator's errors and throw the last one", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler(errorMap, globalErrorHandler);
        handler.handleValidationResult({
            A: {
                fist: "error",
                second: "error"
            },
            B: {
                some: "error"
            },
            C: {
                first: "error"
            },
            D: {
                error: "error"
            }
        });
        expect(globalErrorHandler.logError).toHaveBeenCalledWith("E0");
        expect(globalErrorHandler.logError).toHaveBeenCalledWith("E1");
        expect(globalErrorHandler.logError).toHaveBeenCalledWith("E2");
        expect(globalErrorHandler.throwError).toHaveBeenCalledWith("E3");
        expect(globalErrorHandler.logError).toHaveBeenCalledTimes(3);
        expect(globalErrorHandler.throwError).toHaveBeenCalledTimes(1)
    });
    it("should log and throw only exising error codes", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler({
            A: "E0",
            C: "E2"
        }, globalErrorHandler);
        handler.handleValidationResult({
            A: {
                fist: "error",
                second: "error"
            },
            B: {
                some: "error"
            },
            C: {
                first: "error"
            },
            D: {
                error: "error"
            }
        });
        expect(globalErrorHandler.logError).toHaveBeenCalledWith("E0");
        expect(globalErrorHandler.throwError).toHaveBeenCalledWith("E2");
        expect(globalErrorHandler.logError).toHaveBeenCalledTimes(1);
        expect(globalErrorHandler.throwError).toHaveBeenCalledTimes(1)
    });
    it("should throw single validator's error", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler(errorMap, globalErrorHandler);
        handler.handleValidationResult({
            B: {
                some: "error"
            }
        });
        expect(globalErrorHandler.logError).not.toHaveBeenCalled();
        expect(globalErrorHandler.throwError).toHaveBeenCalledWith("E1");
        expect(globalErrorHandler.throwError).toHaveBeenCalledTimes(1)
    });
    it("should throw single validator's error if only one error code matches with it", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler({
            B: "E1"
        }, globalErrorHandler);
        handler.handleValidationResult({
            A: {
                fist: "error",
                second: "error"
            },
            B: {
                some: "error"
            },
            C: {
                first: "error"
            },
            D: {
                error: "error"
            }
        });
        expect(globalErrorHandler.logError).not.toHaveBeenCalled();
        expect(globalErrorHandler.throwError).toHaveBeenCalledWith("E1");
        expect(globalErrorHandler.throwError).toHaveBeenCalledTimes(1)
    });
    it("shouldn't log the same error code more that one time", () => {
        var globalErrorHandler = createGlobalErrorHandlerMock();
        var handler = new TestErrorHandler({
            A: "E0",
            B: "E0",
            C: "E1",
            D: "E1"
        }, globalErrorHandler);
        handler.handleValidationResult({
            A: {
                fist: "error",
                second: "error"
            },
            B: {
                some: "error"
            },
            C: {
                first: "error"
            },
            D: {
                error: "error"
            }
        });
        expect(globalErrorHandler.logError).toHaveBeenCalledWith("E0");
        expect(globalErrorHandler.throwError).toHaveBeenCalledWith("E1");
        expect(globalErrorHandler.logError).toHaveBeenCalledTimes(1);
        expect(globalErrorHandler.throwError).toHaveBeenCalledTimes(1)
    })
});
