"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 __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 react_2 = require("@testing-library/react");
var GaugeChart_1 = require("./GaugeChart");
var defaultProps = {
    width: 400,
    height: 300,
    data: [
        { color: 'rgba(241, 35, 35, 1)', value: 30 },
        { color: 'rgba(255, 109, 26, 1)', value: 40 },
        { color: 'rgba(255, 238, 54, 1)', value: 20 },
        { color: 'rgba(147, 244, 87, 1)', value: 10 }
    ],
    value: 50,
    label: 'Label',
    isAnimationActive: false
};
var removeExtraSpaces = function (str) { return str.replace(/\s{2,}/g, ' '); };
var setUp = function (props) {
    if (props === void 0) { props = defaultProps; }
    var result = (0, react_2.render)(react_1.default.createElement(GaugeChart_1.GaugeChart, __assign({}, props)));
    return __assign(__assign({}, result), { rerender: function (newProps) { return result.rerender(react_1.default.createElement(GaugeChart_1.GaugeChart, __assign({}, props, newProps))); } });
};
describe('Gauge chart behaviour', function () {
    it('should render chart correctly in basic scenario', function () { return __awaiter(void 0, void 0, void 0, function () {
        var rerender, text, gaugeChart, pieChart, pie, sectors, indicator, firstSector, secondSector, thirdSector, fourthSector;
        return __generator(this, function (_a) {
            rerender = setUp(__assign(__assign({}, defaultProps), { width: 0, height: 0 })).rerender;
            text = react_2.screen.getByTestId('gauge-chart-text');
            Object.defineProperty((0, react_2.within)(text).getByText('Label'), 'clientHeight', { configurable: true, value: 16 });
            rerender(defaultProps);
            gaugeChart = react_2.screen.getByTestId('gauge-chart');
            pieChart = gaugeChart.querySelector('.recharts-wrapper');
            expect(gaugeChart).toHaveStyle({ width: '400px', height: '300px' });
            expect(pieChart).toHaveStyle({ width: '400px', height: '300px' });
            pie = gaugeChart.querySelector('.recharts-surface');
            expect(pie).toHaveStyle({ width: '100%', height: '100%' });
            expect(pie).toHaveAttribute('width', '400');
            expect(pie).toHaveAttribute('height', '300');
            expect(pie).toHaveAttribute('cx', '50%');
            expect(pie).toHaveAttribute('cy', '50%');
            sectors = gaugeChart.querySelectorAll('.recharts-pie-sector');
            indicator = react_2.screen.getByTestId('gauge-chart-indicator');
            expect(sectors).toHaveLength(4);
            firstSector = sectors[0].querySelector('.recharts-sector');
            expect(firstSector).toHaveAttribute('fill', 'rgba(241, 35, 35, 1)');
            expect(firstSector).toHaveAttribute('cx', '200');
            expect(firstSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(firstSector.getAttribute('d'))).toBe('M 0,249.99999999999997 A 200,200,0, 0,1, 82.4429495415054,88.19660112501052 L 105.95435963320432,120.55728090000841 A 160,160,0, 0,0, 40,249.99999999999997 Z');
            secondSector = sectors[1].querySelector('.recharts-sector');
            expect(secondSector).toHaveAttribute('fill', 'rgba(255, 109, 26, 1)');
            expect(secondSector).toHaveAttribute('cx', '200');
            expect(secondSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(secondSector.getAttribute('d'))).toBe('M 82.4429495415054,88.19660112501052 A 200,200,0, 0,1, 317.55705045849464,88.19660112501052 L 294.04564036679574,120.55728090000841 A 160,160,0, 0,0, 105.95435963320432,120.55728090000841 Z');
            thirdSector = sectors[2].querySelector('.recharts-sector');
            expect(thirdSector).toHaveAttribute('fill', 'rgba(255, 238, 54, 1)');
            expect(thirdSector).toHaveAttribute('cx', '200');
            expect(thirdSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(thirdSector.getAttribute('d'))).toBe('M 317.55705045849464,88.19660112501052 A 200,200,0, 0,1, 390.21130325903073,188.19660112501052 L 352.1690426072246,200.5572809000084 A 160,160,0, 0,0, 294.04564036679574,120.55728090000841 Z');
            fourthSector = sectors[3].querySelector('.recharts-sector');
            expect(fourthSector).toHaveAttribute('fill', 'rgba(147, 244, 87, 1)');
            expect(fourthSector).toHaveAttribute('cx', '200');
            expect(fourthSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(fourthSector.getAttribute('d'))).toBe('M 390.21130325903073,188.19660112501052 A 200,200,0, 0,1, 400,250 L 360,250 A 160,160,0, 0,0, 352.1690426072246,200.5572809000084 Z');
            expect(indicator).toHaveStyle({ opacity: 1 });
            expect(indicator).toHaveAttribute('d', 'M198 98L202 98 L200 36 L198 98');
            expect(text).toHaveStyle({ top: '30%', bottom: 'calc(16.666666666666664% - 0px)', left: '10%', right: '10%' });
            (0, react_2.within)(text).getByText('50');
            (0, react_2.within)(text).getByText('Label');
            return [2 /*return*/];
        });
    }); });
    it('should render chart correctly if the label height is more than 16 pixels', function () { return __awaiter(void 0, void 0, void 0, function () {
        var rerender, text, gaugeChart, indicator, sectors, firstSector, secondSector, thirdSector, fourthSector;
        return __generator(this, function (_a) {
            rerender = setUp(__assign(__assign({}, defaultProps), { width: 0, height: 0 })).rerender;
            text = react_2.screen.getByTestId('gauge-chart-text');
            Object.defineProperty((0, react_2.within)(text).getByText('Label'), 'clientHeight', { configurable: true, value: 48 });
            rerender(defaultProps);
            gaugeChart = react_2.screen.getByTestId('gauge-chart');
            indicator = react_2.screen.getByTestId('gauge-chart-indicator');
            sectors = gaugeChart.querySelectorAll('.recharts-pie-sector');
            firstSector = sectors[0].querySelector('.recharts-sector');
            expect(firstSector).toHaveAttribute('cx', '200');
            expect(firstSector).toHaveAttribute('cy', '234');
            expect(removeExtraSpaces(firstSector.getAttribute('d'))).toBe('M 0,233.99999999999997 A 200,200,0, 0,1, 82.4429495415054,72.19660112501052 L 105.95435963320432,104.55728090000841 A 160,160,0, 0,0, 40,233.99999999999997 Z');
            secondSector = sectors[1].querySelector('.recharts-sector');
            expect(secondSector).toHaveAttribute('cx', '200');
            expect(secondSector).toHaveAttribute('cy', '234');
            expect(removeExtraSpaces(secondSector.getAttribute('d'))).toBe('M 82.4429495415054,72.19660112501052 A 200,200,0, 0,1, 317.55705045849464,72.19660112501052 L 294.04564036679574,104.55728090000841 A 160,160,0, 0,0, 105.95435963320432,104.55728090000841 Z');
            thirdSector = sectors[2].querySelector('.recharts-sector');
            expect(thirdSector).toHaveAttribute('cx', '200');
            expect(thirdSector).toHaveAttribute('cy', '234');
            expect(removeExtraSpaces(thirdSector.getAttribute('d'))).toBe('M 317.55705045849464,72.19660112501052 A 200,200,0, 0,1, 390.21130325903073,172.19660112501052 L 352.1690426072246,184.5572809000084 A 160,160,0, 0,0, 294.04564036679574,104.55728090000841 Z');
            fourthSector = sectors[3].querySelector('.recharts-sector');
            expect(fourthSector).toHaveAttribute('cx', '200');
            expect(fourthSector).toHaveAttribute('cy', '234');
            expect(removeExtraSpaces(fourthSector.getAttribute('d'))).toBe('M 390.21130325903073,172.19660112501052 A 200,200,0, 0,1, 400,234 L 360,234 A 160,160,0, 0,0, 352.1690426072246,184.5572809000084 Z');
            expect(indicator).toHaveAttribute('d', 'M198 82L202 82 L200 20 L198 82');
            expect(text).toHaveStyle({ top: '24.666666666666668%', bottom: 'calc(22% - 32px)', left: '10%', right: '10%' });
            return [2 /*return*/];
        });
    }); });
    it('should render chart correctly if the label height is less than 16 pixels', function () { return __awaiter(void 0, void 0, void 0, function () {
        var rerender, text, gaugeChart, indicator, sectors, firstSector, secondSector, thirdSector, fourthSector;
        return __generator(this, function (_a) {
            rerender = setUp(__assign(__assign({}, defaultProps), { width: 0, height: 0 })).rerender;
            text = react_2.screen.getByTestId('gauge-chart-text');
            Object.defineProperty((0, react_2.within)(text).getByText('Label'), 'clientHeight', { configurable: true, value: 6 });
            rerender(defaultProps);
            gaugeChart = react_2.screen.getByTestId('gauge-chart');
            indicator = react_2.screen.getByTestId('gauge-chart-indicator');
            sectors = gaugeChart.querySelectorAll('.recharts-pie-sector');
            firstSector = sectors[0].querySelector('.recharts-sector');
            expect(firstSector).toHaveAttribute('cx', '200');
            expect(firstSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(firstSector.getAttribute('d'))).toBe('M 0,249.99999999999997 A 200,200,0, 0,1, 82.4429495415054,88.19660112501052 L 105.95435963320432,120.55728090000841 A 160,160,0, 0,0, 40,249.99999999999997 Z');
            secondSector = sectors[1].querySelector('.recharts-sector');
            expect(secondSector).toHaveAttribute('cx', '200');
            expect(secondSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(secondSector.getAttribute('d'))).toBe('M 82.4429495415054,88.19660112501052 A 200,200,0, 0,1, 317.55705045849464,88.19660112501052 L 294.04564036679574,120.55728090000841 A 160,160,0, 0,0, 105.95435963320432,120.55728090000841 Z');
            thirdSector = sectors[2].querySelector('.recharts-sector');
            expect(thirdSector).toHaveAttribute('cx', '200');
            expect(thirdSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(thirdSector.getAttribute('d'))).toBe('M 317.55705045849464,88.19660112501052 A 200,200,0, 0,1, 390.21130325903073,188.19660112501052 L 352.1690426072246,200.5572809000084 A 160,160,0, 0,0, 294.04564036679574,120.55728090000841 Z');
            fourthSector = sectors[3].querySelector('.recharts-sector');
            expect(fourthSector).toHaveAttribute('cx', '200');
            expect(fourthSector).toHaveAttribute('cy', '250');
            expect(removeExtraSpaces(fourthSector.getAttribute('d'))).toBe('M 390.21130325903073,188.19660112501052 A 200,200,0, 0,1, 400,250 L 360,250 A 160,160,0, 0,0, 352.1690426072246,200.5572809000084 Z');
            expect(indicator).toHaveAttribute('d', 'M198 98L202 98 L200 36 L198 98');
            expect(text).toHaveStyle({ top: '30%', bottom: 'calc(16.666666666666664% - -10px)', left: '10%', right: '10%' });
            return [2 /*return*/];
        });
    }); });
    it('should render custom label correctly', function () {
        var props = __assign(__assign({}, defaultProps), { value: 78.4, renderValue: function (value) { return react_1.default.createElement("span", { "data-reltio-id": "custom-label" },
                Math.round(value),
                "%"); } });
        setUp(props);
        expect(react_2.screen.getByTestId('custom-label')).toHaveTextContent('78%');
    });
});
