diff --git a/src/components/dashboard/ClientsGraph.tsx b/src/components/dashboard/ClientsGraph.tsx index 17837ae..3ad42f2 100644 --- a/src/components/dashboard/ClientsGraph.tsx +++ b/src/components/dashboard/ClientsGraph.tsx @@ -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"; diff --git a/src/components/dashboard/QueriesGraph.tsx b/src/components/dashboard/QueriesGraph.tsx index e321bb2..759bfb9 100644 --- a/src/components/dashboard/QueriesGraph.tsx +++ b/src/components/dashboard/QueriesGraph.tsx @@ -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"; diff --git a/src/components/log/QueryLog.tsx b/src/components/log/QueryLog.tsx index b746ca1..16aa3fb 100644 --- a/src/components/log/QueryLog.tsx +++ b/src/components/log/QueryLog.tsx @@ -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"; diff --git a/src/util/__tests__/graphUtils.test.tsx b/src/util/__tests__/graphUtils.test.tsx new file mode 100644 index 0000000..3258910 --- /dev/null +++ b/src/util/__tests__/graphUtils.test.tsx @@ -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); + }); +}); diff --git a/src/util/index.tsx b/src/util/graphUtils.tsx similarity index 97% rename from src/util/index.tsx rename to src/util/graphUtils.tsx index d339154..b49c4e8 100644 --- a/src/util/index.tsx +++ b/src/util/graphUtils.tsx @@ -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. */