"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var BarChart_1 = require("./BarChart");
var react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
describe('BarChart', function () {
    var defaultProps = {
        data: [
            { label: 'first', value: 10, group: '1' },
            { label: 'second', value: 20, group: '2' },
            { label: 'third', value: 30, group: '3' }
        ],
        width: 100,
        height: 200
    };
    var setUp = function (props) {
        if (props === void 0) { props = defaultProps; }
        var user = user_event_1.default.setup();
        return __assign({ user: user }, (0, react_2.render)(react_1.default.createElement(BarChart_1.BarChart, __assign({}, props))));
    };
    it('should render BarChartWidget', function () { return __awaiter(void 0, void 0, void 0, function () {
        var container, chart, cartesianGrid, cartesianGridLines, xAxis, xAxisLine, xAxisTicks, yAxis, yAxisLine, yAxisTicks, barRectangles;
        return __generator(this, function (_a) {
            container = setUp().container;
            chart = container.querySelector('.recharts-surface');
            expect(chart).toHaveAttribute('width', "".concat(defaultProps.width));
            expect(chart).toHaveAttribute('height', "".concat(defaultProps.height));
            expect(container.querySelector('.recharts-cartesian-grid-vertical')).toBeInTheDocument();
            expect(container.querySelector('.recharts-cartesian-grid-horizontal')).toBeInTheDocument();
            expect(container.querySelector('.recharts-tooltip-wrapper')).toBeInTheDocument();
            cartesianGrid = container.querySelector('.recharts-cartesian-grid');
            cartesianGridLines = cartesianGrid.querySelectorAll('line');
            expect(cartesianGrid).toBeInTheDocument();
            expect(cartesianGridLines).toHaveLength(13);
            cartesianGridLines.forEach(function (line) {
                expect(line).toHaveAttribute('stroke', '#666');
                expect(line).toHaveAttribute('stroke-dasharray', '3 3');
                expect(line).toHaveAttribute('stroke-opacity', '0.4');
            });
            xAxis = container.querySelector('.recharts-xAxis');
            xAxisLine = xAxis.querySelector('.recharts-cartesian-axis-line');
            xAxisTicks = xAxis.querySelectorAll('.recharts-cartesian-axis-tick');
            expect(xAxis).toBeInTheDocument();
            expect(xAxisLine).toBeInTheDocument();
            expect(xAxisLine).toHaveAttribute('text-anchor', 'middle');
            expect(xAxisLine).toHaveAttribute('angle', '0');
            expect(xAxisTicks).toHaveLength(3);
            xAxisTicks.forEach(function (xAxisTick, index) {
                (0, react_2.within)(xAxisTick).getByText(defaultProps.data[index].label);
            });
            yAxis = container.querySelector('.recharts-yAxis');
            yAxisLine = yAxis.querySelector('.recharts-cartesian-axis-line');
            yAxisTicks = yAxis.querySelectorAll('.recharts-cartesian-axis-tick');
            expect(yAxis).toBeInTheDocument();
            expect(yAxisLine).toBeInTheDocument();
            expect(yAxisTicks).toHaveLength(8);
            (0, react_2.within)(yAxisTicks[0]).getByText('0');
            (0, react_2.within)(yAxisTicks[1]).getByText('5');
            (0, react_2.within)(yAxisTicks[2]).getByText('10');
            (0, react_2.within)(yAxisTicks[3]).getByText('15');
            (0, react_2.within)(yAxisTicks[4]).getByText('20');
            (0, react_2.within)(yAxisTicks[5]).getByText('25');
            (0, react_2.within)(yAxisTicks[6]).getByText('30');
            (0, react_2.within)(yAxisTicks[7]).getByText('35');
            barRectangles = container.querySelectorAll('.recharts-bar-rectangle');
            expect(barRectangles).toHaveLength(defaultProps.data.length);
            return [2 /*return*/];
        });
    }); });
    it('should render horizontal BarChartWidget', function () {
        var container = setUp(__assign(__assign({}, defaultProps), { options: {
                isHorizontal: true
            } })).container;
        expect(container.querySelector('.recharts-cartesian-grid-vertical')).not.toBeInTheDocument();
        expect(container.querySelector('.recharts-cartesian-grid-horizontal')).toBeInTheDocument();
    });
    it('should render BarChartWidget with rotated XAxis labels', function () {
        var container = setUp(__assign(__assign({}, defaultProps), { options: {
                isLabelRotated: true
            } })).container;
        var xAxis = container.querySelector('.recharts-xAxis');
        var xAxisLine = xAxis.querySelector('.recharts-cartesian-axis-line');
        expect(xAxisLine).toHaveAttribute('angle', '-45');
        expect(xAxisLine).toHaveAttribute('text-anchor', 'end');
    });
    it('should render YAxis for big numbers correctly', function () {
        var container = setUp(__assign(__assign({}, defaultProps), { data: __spreadArray(__spreadArray([], defaultProps.data, true), [{ label: 'fouth', value: 100100, group: '3' }], false) })).container;
        var yAxis = container.querySelector('.recharts-yAxis');
        var yAxisTicks = yAxis.querySelectorAll('.recharts-cartesian-axis-tick');
        expect(yAxisTicks).toHaveLength(8);
        (0, react_2.within)(yAxisTicks[0]).getByText('0');
        (0, react_2.within)(yAxisTicks[1]).getByText('15.0K');
        (0, react_2.within)(yAxisTicks[2]).getByText('30.0K');
        (0, react_2.within)(yAxisTicks[3]).getByText('45.0K');
        (0, react_2.within)(yAxisTicks[4]).getByText('60.0K');
        (0, react_2.within)(yAxisTicks[5]).getByText('75.0K');
        (0, react_2.within)(yAxisTicks[6]).getByText('90.0K');
        (0, react_2.within)(yAxisTicks[7]).getByText('105.0K');
    });
    it('should call props.onItemClick on rect click', function () { return __awaiter(void 0, void 0, void 0, function () {
        var onItemClickFn, _a, user, container, barRectangles;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    onItemClickFn = jest.fn();
                    _a = setUp(__assign(__assign({}, defaultProps), { onItemClick: onItemClickFn })), user = _a.user, container = _a.container;
                    barRectangles = container.querySelectorAll('.recharts-bar-rectangle');
                    expect(barRectangles).toHaveLength(3);
                    return [4 /*yield*/, user.click(barRectangles[0])];
                case 1:
                    _b.sent();
                    expect(onItemClickFn).toHaveBeenLastCalledWith(defaultProps.data[0].group);
                    return [4 /*yield*/, user.click(barRectangles[1])];
                case 2:
                    _b.sent();
                    expect(onItemClickFn).toHaveBeenLastCalledWith(defaultProps.data[1].group);
                    return [4 /*yield*/, user.click(barRectangles[2])];
                case 3:
                    _b.sent();
                    expect(onItemClickFn).toHaveBeenLastCalledWith(defaultProps.data[2].group);
                    return [2 /*return*/];
            }
        });
    }); });
});
