Move graph utils to new file and add tests

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12
2019-04-09 21:08:15 -07:00
parent fde34be4a3
commit e31ed2f1af
5 changed files with 55 additions and 4 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ import React, { Component, RefObject } from "react";
import ReactDOM from "react-dom";
import { Line } from "react-chartjs-2";
import { WithNamespaces, withNamespaces } from "react-i18next";
import { getIntervalForRange, padNumber } from "../../util";
import { getIntervalForRange, padNumber } from "../../util/graphUtils";
import api from "../../util/api";
import ChartTooltip from "./ChartTooltip";
import { WithAPIData } from "../common/WithAPIData";
+1 -1
View File
@@ -10,7 +10,7 @@
import React, { Component } from "react";
import { WithNamespaces, withNamespaces } from "react-i18next";
import { getIntervalForRange, padNumber } from "../../util";
import { getIntervalForRange, padNumber } from "../../util/graphUtils";
import api from "../../util/api";
import { WithAPIData } from "../common/WithAPIData";
import { ChartData, ChartOptions, TimeUnit } from "chart.js";
+1 -1
View File
@@ -20,7 +20,7 @@ import i18next from "i18next";
import { WithNamespaces, withNamespaces } from "react-i18next";
import debounce from "lodash.debounce";
import moment from "moment";
import { padNumber } from "../../util";
import { padNumber } from "../../util/graphUtils";
import api from "../../util/api";
import { dateRanges } from "../../util/dateRanges";
import { TranslatedTimeRangeSelector } from "../dashboard/TimeRangeSelector";
+51
View File
@@ -0,0 +1,51 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2019 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* Web Interface
* Graph utility tests
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
import { padNumber, getIntervalForRange } from "../graphUtils";
import { TimeRange } from "../../components/common/context/TimeRangeContext";
import moment from "moment";
describe("padNumber", () => {
it("pads 0 to 00", () => {
expect(padNumber(0)).toEqual("00");
});
it("pads 1 to 01", () => {
expect(padNumber(1)).toEqual("01");
});
it("pads 12 to 12", () => {
expect(padNumber(12)).toEqual("12");
});
});
describe("getIntervalForRange", () => {
it("returns 10 minutes for 24 hours", () => {
const range: TimeRange = {
name: "24 Hours",
from: moment().subtract(1, "day"),
until: moment()
};
expect(getIntervalForRange(range)).toEqual(10 * 60);
});
it("returns 1 day for 144 days", () => {
const range: TimeRange = {
name: "144 days",
// Use seconds instead of days to ensure the difference in epoch time is
// equal to 144 days
from: moment().subtract(144 * 24 * 60 * 60, "seconds"),
until: moment()
};
expect(getIntervalForRange(range)).toEqual(24 * 60 * 60);
});
});
@@ -3,7 +3,7 @@
* Network-wide ad blocking via your own hardware.
*
* Web Interface
* Various utilities
* Graph utility functions
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */