|
| 1 | +import {getLabelPosition, fontSize, getLabelTransform} from "./axis-labels"; |
| 2 | + |
| 3 | +import type {GraphDimensions} from "../types"; |
| 4 | + |
| 5 | +describe("getLabelPosition", () => { |
| 6 | + it("should return the correct position for the default graph", () => { |
| 7 | + const graphInfo: GraphDimensions = { |
| 8 | + range: [ |
| 9 | + [-10, 10], |
| 10 | + [-10, 10], |
| 11 | + ], |
| 12 | + width: 400, |
| 13 | + height: 400, |
| 14 | + }; |
| 15 | + const labelLocation = "onAxis"; |
| 16 | + const expected = [ |
| 17 | + [400, 200], // X Label at [Right edge of the graph, vertical center of the graph] |
| 18 | + [200, -2 * fontSize], // Y Label at [Horizontal center of the graph, 2x fontSize above the top edge] |
| 19 | + ]; |
| 20 | + |
| 21 | + expect(getLabelPosition(graphInfo, labelLocation)).toEqual(expected); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should return the correct position for the default graph without a labelLocation", () => { |
| 25 | + const graphInfo: GraphDimensions = { |
| 26 | + range: [ |
| 27 | + [-10, 10], |
| 28 | + [-10, 10], |
| 29 | + ], |
| 30 | + width: 400, |
| 31 | + height: 400, |
| 32 | + }; |
| 33 | + |
| 34 | + const expected = [ |
| 35 | + [400, 200], // X Label at [Right edge of the graph, vertical center of the graph] |
| 36 | + [200, -2 * fontSize], // Y Label at [Horizontal center of the graph, 2x fontSize above the top edge] |
| 37 | + ]; |
| 38 | + |
| 39 | + expect(getLabelPosition(graphInfo, undefined)).toEqual(expected); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should return the correct position for labels set to alongEdge", () => { |
| 43 | + const graphInfo: GraphDimensions = { |
| 44 | + range: [ |
| 45 | + [-10, 10], |
| 46 | + [-10, 10], |
| 47 | + ], |
| 48 | + width: 400, |
| 49 | + height: 400, |
| 50 | + }; |
| 51 | + const labelLocation = "alongEdge"; |
| 52 | + const expected = [ |
| 53 | + [200, 400 + fontSize], // X Label at [Horizontal center of the graph, 1x fontSize below the bottom edge] |
| 54 | + [-fontSize, 200 - fontSize], // Y label at [1x fontSize to the left of the left edge, vertical center of the graph] |
| 55 | + ]; |
| 56 | + |
| 57 | + expect(getLabelPosition(graphInfo, labelLocation)).toEqual(expected); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should return the correct position for labels set to alongEdge with wholly negative ranges", () => { |
| 61 | + const graphInfo: GraphDimensions = { |
| 62 | + range: [ |
| 63 | + [-10, -5], |
| 64 | + [-10, -5], |
| 65 | + ], |
| 66 | + width: 400, |
| 67 | + height: 400, |
| 68 | + }; |
| 69 | + const labelLocation = "alongEdge"; |
| 70 | + const expected = [ |
| 71 | + [200, 400 + fontSize], // X Label at [Horizontal center of the graph, 1x fontSize below the bottom edge] |
| 72 | + [-fontSize, 200 - fontSize], // Y label at [1x fontSize to the left of the left edge, vertical center of the graph] |
| 73 | + ]; |
| 74 | + |
| 75 | + expect(getLabelPosition(graphInfo, labelLocation)).toEqual(expected); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return the correct position for labels set to alongEdge with wholly positive ranges", () => { |
| 79 | + const graphInfo: GraphDimensions = { |
| 80 | + range: [ |
| 81 | + [5, 10], |
| 82 | + [5, 10], |
| 83 | + ], |
| 84 | + width: 400, |
| 85 | + height: 400, |
| 86 | + }; |
| 87 | + const labelLocation = "alongEdge"; |
| 88 | + const expected = [ |
| 89 | + [200, 400 + 3 * fontSize], // X Label at [Horizontal center of the graph, 3x fontSize below the bottom edge] |
| 90 | + [-3 * fontSize, 200 - fontSize], // Y label at [3x fontSize to the left of the left edge, vertical center of the graph] |
| 91 | + ]; |
| 92 | + |
| 93 | + expect(getLabelPosition(graphInfo, labelLocation)).toEqual(expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should return the correct position for labels set to alongEdge with min ranges at 0", () => { |
| 97 | + // Should result in same position as the wholly positive range test |
| 98 | + const graphInfo: GraphDimensions = { |
| 99 | + range: [ |
| 100 | + [0, 10], |
| 101 | + [0, 10], |
| 102 | + ], |
| 103 | + width: 400, |
| 104 | + height: 400, |
| 105 | + }; |
| 106 | + const labelLocation = "alongEdge"; |
| 107 | + const expected = [ |
| 108 | + [200, 400 + 3 * fontSize], // X Label at [Horizontal center of the graph, 3x fontSize below the bottom edge] |
| 109 | + [-3 * fontSize, 200 - fontSize], // Y label at [3x fontSize to the left of the left edge, vertical center of the graph] |
| 110 | + ]; |
| 111 | + |
| 112 | + expect(getLabelPosition(graphInfo, labelLocation)).toEqual(expected); |
| 113 | + }); |
| 114 | +}); |
| 115 | +describe("getLabelTransform", () => { |
| 116 | + it("should return the correct transform for the default graph", () => { |
| 117 | + const expected = { |
| 118 | + xLabelTransform: "translate(7px, -50%)", |
| 119 | + yLabelTransform: "translate(-50%, 0px)", |
| 120 | + }; |
| 121 | + |
| 122 | + expect(getLabelTransform(undefined)).toEqual(expected); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should return the correct transform for an onAxis graph", () => { |
| 126 | + const labelLocation = "onAxis"; |
| 127 | + const expected = { |
| 128 | + xLabelTransform: "translate(7px, -50%)", |
| 129 | + yLabelTransform: "translate(-50%, 0px)", |
| 130 | + }; |
| 131 | + |
| 132 | + expect(getLabelTransform(labelLocation)).toEqual(expected); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should return the correct transform for labels set to alongEdge", () => { |
| 136 | + const labelLocation = "alongEdge"; |
| 137 | + const expected = { |
| 138 | + xLabelTransform: "translate(-50%, -50%)", |
| 139 | + yLabelTransform: "translate(-50%, 0px) rotate(-90deg)", |
| 140 | + }; |
| 141 | + |
| 142 | + expect(getLabelTransform(labelLocation)).toEqual(expected); |
| 143 | + }); |
| 144 | +}); |
0 commit comments