diff --git a/src/index.test.ts b/src/index.test.ts index 367f8b7..3282907 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -63,6 +63,16 @@ describe("getMetricValue", () => { expect(getMetricValue("test", true)).toEqual(500); expect(getMetricValue("test", true, [0, 10], 2)).toEqual(5); }); + + it("returns noDataValue when no value is found", () => { + expect(getMetricValue("nonExistentName")).toBe(null); + expect( + getMetricValue("nonExistentName", false, [0, 100], 2, "something") + ).toBe("something"); + expect(getMetricValue("minimal", false, [0, 100], 2, "something")).toBe( + "something" + ); + }); }); describe("getShowcaseMetricValue", () => { diff --git a/src/index.ts b/src/index.ts index 6ecf0e2..d24e858 100644 --- a/src/index.ts +++ b/src/index.ts @@ -71,19 +71,20 @@ const getMetricValueByName = ( * @param showcase - Decides if values are randomly generated. * @param range - Range of values to return. * @param decimals - Amount of decimals returned. - * @param noDataValue - Returns null if no data is found. + * @param noDataValue - Return value when no data is found. */ function getMetricValue( metric: string, showcase = false, range?: Array, - decimals?: number + decimals?: number, + noDataValue: unknown = null ): unknown { if (showcase) { return getShowcaseMetricValue({ range, decimals }); } - return getMetricValueByName(metric); + return getMetricValueByName(metric, { noDataValue }); } export { getMetricValue, getMetricValueByName, getShowcaseMetricValue };