Ability to specify hex colors (#72)

This commit is contained in:
Simon J
2021-11-04 14:33:37 +11:00
committed by GitHub
parent 9d76c92e69
commit 602cb6b820
2 changed files with 24 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ import logger from "./logger";
import url from "./url_builder"; import url from "./url_builder";
export const WORD = /^\w+$/; export const WORD = /^\w+$/;
export const COLOR = /^#?\w+$/;
type EnvVarOpts = { type EnvVarOpts = {
default: string | undefined; default: string | undefined;
@@ -64,10 +65,10 @@ export default function () {
secret: bnbEnvVar("SECRET", { default: "bonob" })!, secret: bnbEnvVar("SECRET", { default: "bonob" })!,
icons: { icons: {
foregroundColor: bnbEnvVar("ICON_FOREGROUND_COLOR", { foregroundColor: bnbEnvVar("ICON_FOREGROUND_COLOR", {
validationPattern: WORD, validationPattern: COLOR,
}), }),
backgroundColor: bnbEnvVar("ICON_BACKGROUND_COLOR", { backgroundColor: bnbEnvVar("ICON_BACKGROUND_COLOR", {
validationPattern: WORD, validationPattern: COLOR,
}), }),
}, },
sonos: { sonos: {

View File

@@ -1,5 +1,5 @@
import { hostname } from "os"; import { hostname } from "os";
import config, { envVar, WORD } from "../src/config"; import config, { COLOR, envVar } from "../src/config";
describe("envVar", () => { describe("envVar", () => {
const OLD_ENV = process.env; const OLD_ENV = process.env;
@@ -180,18 +180,25 @@ describe("config", () => {
}); });
}); });
describe(`when ${k} is specified`, () => { describe(`when ${k} is specified as a color`, () => {
it(`should use it`, () => { it(`should use it`, () => {
process.env[k] = "pink"; process.env[k] = "pink";
expect(config().icons.foregroundColor).toEqual("pink"); expect(config().icons.foregroundColor).toEqual("pink");
}); });
}); });
describe(`when ${k} is specified as hex`, () => {
it(`should use it`, () => {
process.env[k] = "#1db954";
expect(config().icons.foregroundColor).toEqual("#1db954");
});
});
describe(`when ${k} is an invalid string`, () => { describe(`when ${k} is an invalid string`, () => {
it(`should blow up`, () => { it(`should blow up`, () => {
process.env[k] = "#dfasd"; process.env[k] = "!dfasd";
expect(() => config()).toThrow( expect(() => config()).toThrow(
`Invalid value specified for 'BNB_ICON_FOREGROUND_COLOR', must match ${WORD}` `Invalid value specified for 'BNB_ICON_FOREGROUND_COLOR', must match ${COLOR}`
); );
}); });
}); });
@@ -215,18 +222,25 @@ describe("config", () => {
}); });
}); });
describe(`when ${k} is specified`, () => { describe(`when ${k} is specified as a color`, () => {
it(`should use it`, () => { it(`should use it`, () => {
process.env[k] = "blue"; process.env[k] = "blue";
expect(config().icons.backgroundColor).toEqual("blue"); expect(config().icons.backgroundColor).toEqual("blue");
}); });
}); });
describe(`when ${k} is specified as hex`, () => {
it(`should use it`, () => {
process.env[k] = "#1db954";
expect(config().icons.backgroundColor).toEqual("#1db954");
});
});
describe(`when ${k} is an invalid string`, () => { describe(`when ${k} is an invalid string`, () => {
it(`should blow up`, () => { it(`should blow up`, () => {
process.env[k] = "#red"; process.env[k] = "!red";
expect(() => config()).toThrow( expect(() => config()).toThrow(
`Invalid value specified for 'BNB_ICON_BACKGROUND_COLOR', must match ${WORD}` `Invalid value specified for 'BNB_ICON_BACKGROUND_COLOR', must match ${COLOR}`
); );
}); });
}); });