UNPKG

simple-ascii-chart

Version:
280 lines (279 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var draw_1 = require("../draw"); var constants_1 = require("../../constants"); describe('Drawing functions', function () { describe('drawPosition', function () { it('should correctly draw a symbol at the specified position in the graph', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; (0, draw_1.drawPosition)({ graph: graph, scaledX: 1, scaledY: 1, symbol: 'X' }); expect(graph[1][1]).toEqual('X'); }); it('should handle out-of-bounds Y position in debug mode', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var consoleSpy = jest.spyOn(console, 'log').mockImplementation(); (0, draw_1.drawPosition)({ graph: graph, scaledX: 1, scaledY: 3, symbol: 'X', debugMode: true }); expect(consoleSpy).toHaveBeenCalledWith('Drawing at [1, 3]', 'Error: out of bounds Y', expect.objectContaining({ graph: graph, scaledX: 1, scaledY: 3, })); consoleSpy.mockRestore(); }); it('should handle out-of-bounds X position in debug mode', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var consoleSpy = jest.spyOn(console, 'log').mockImplementation(); (0, draw_1.drawPosition)({ graph: graph, scaledX: 4, scaledY: 1, symbol: 'X', debugMode: true }); expect(consoleSpy).toHaveBeenCalledWith('Drawing at [4, 1]', 'Error: out of bounds X', expect.objectContaining({ graph: graph, scaledX: 4, scaledY: 1, })); consoleSpy.mockRestore(); }); it('should not log any errors if debugMode is off and out-of-bounds error occurs', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var consoleSpy = jest.spyOn(console, 'log').mockImplementation(); (0, draw_1.drawPosition)({ graph: graph, scaledX: 4, scaledY: 1, symbol: 'X' }); expect(consoleSpy).not.toHaveBeenCalled(); consoleSpy.mockRestore(); }); }); describe('drawXAxisEnd', function () { it('should draw the X-axis end correctly', function () { var graph = [ [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], ]; var args = { hasPlaceToRender: true, yPos: 1, graph: graph, yShift: 1, i: 0, plotHeight: 3, scaledX: 1, shift: 0, signShift: 0, axisSymbols: constants_1.AXIS, pointXShift: ['1'], }; (0, draw_1.drawXAxisEnd)(args); expect(graph[0][4]).toEqual('1'); expect(graph[1][4]).toEqual(constants_1.AXIS.x); }); it('should draw Y axis end', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var params = { graph: graph, scaledY: 1, yShift: 0, axis: { x: 0, y: 0 }, pointY: 1, plotHeight: 3, transformLabel: function (value) { return value.toString(); }, axisSymbols: { y: 'Y' }, expansionX: [0], expansionY: [0, 1, 2], coordsGetter: function () { return [0, 0]; }, plotGetter: function () { return [0, 0]; }, }; (0, draw_1.drawYAxisEnd)(params); expect(graph[2][1]).toBe('Y'); }); }); describe('drawYAxisEnd', function () { it('should draw tick labels for each step when showTickLabel is true', function () { var graph = [ [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], ]; var args = { graph: graph, scaledY: 1, yShift: 1, axis: { x: 1, y: 1 }, pointY: 2, plotHeight: 4, transformLabel: function (value) { return value.toString(); }, axisSymbols: { y: 'Y' }, expansionX: [0], expansionY: [0, 1, 2, 3], showTickLabel: true, }; (0, draw_1.drawYAxisEnd)(args); // Expect Y-axis labels to be drawn starting from [1][2], not [0][2] expect(graph[1][2]).toEqual('3'); // Top of the axis (Y value 3) expect(graph[2][2]).toEqual('2'); // Mid Y value (Y value 2) expect(graph[3][2]).toEqual('1'); // Near bottom (Y value 1) // The bottom Y value '0' might not be drawn, depending on the graph size }); it('should draw the Y-axis end correctly', function () { var graph = [ [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], ]; var args = { graph: graph, scaledY: 1, yShift: 1, axis: { x: 1, y: 1 }, pointY: 2, plotHeight: 2, transformLabel: function (value) { return value.toString(); }, axisSymbols: constants_1.AXIS, expansionX: [], expansionY: [], coordsGetter: function () { return [0, 0]; }, plotGetter: function () { return [0, 0]; }, }; (0, draw_1.drawYAxisEnd)(args); expect(graph[2][3]).toEqual(constants_1.AXIS.y); expect(graph[2][2]).toEqual('2'); }); }); describe('drawAxis', function () { it('should draw the main axis', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var args = { graph: graph, axis: { x: 1, y: 1 }, axisSymbols: constants_1.AXIS, }; (0, draw_1.drawAxis)(args); expect(graph[0][1]).toEqual(constants_1.AXIS.n); expect(graph[1][1]).toEqual(constants_1.AXIS.ns); expect(graph[2][1]).toEqual(constants_1.AXIS.nse); }); }); describe('drawGraph', function () { it('should draw an empty graph correctly', function () { var result = (0, draw_1.drawGraph)({ plotWidth: 3, plotHeight: 2, emptySymbol: ' ', }); expect(result).toEqual([ [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], ]); }); }); describe('drawChart', function () { it('should return the graph as a string', function () { var graph = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ]; var result = (0, draw_1.drawChart)({ graph: graph }); expect(result).toEqual('\nabc\ndef\nghi\n'); }); }); describe('drawCustomLine', function () { it('should draw a custom line', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '], ]; var args = { sortedCoords: [[1, 1]], scaledX: 1, scaledY: 1, input: [[1, 1]], index: 0, minY: 0, minX: 0, expansionX: [0], expansionY: [0], lineFormatter: function () { return ({ x: 1, y: 1, symbol: 'X' }); }, toPlotCoordinates: function () { return [1, 1]; }, graph: graph, }; (0, draw_1.drawCustomLine)(args); expect(graph[1][1]).toEqual('X'); }); }); describe('drawLine', function () { it('should draw a line', function () { var graph = [ [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], ]; var args = { index: 1, arr: [ [0, 0], [1, 1], ], graph: graph, horizontalBarChart: false, barChart: false, scaledX: 1, axis: { x: 0, y: 5 }, axisCenter: undefined, scaledY: 1, plotHeight: 3, emptySymbol: ' ', chartSymbols: constants_1.CHART, }; (0, draw_1.drawLine)(args); expect(graph[3][1]).toEqual('┛'); expect(graph[2][2]).toEqual('━'); expect(graph[3][2]).toEqual(' '); }); }); describe('drawShift', function () { it('should shift the graph', function () { var graph = [ [' ', ' ', ' '], [' ', ' ', ' '], ]; var result = (0, draw_1.drawShift)({ graph: graph, plotWidth: 2, emptySymbol: ' ', scaledCoords: [ [0, 0], [1, 1], ], xShift: 1, yShift: 1, }); expect(result.hasToBeMoved).toBe(false); }); }); });