diff --git a/src/sdl/SDL/SDL.spec.ts b/src/sdl/SDL/SDL.spec.ts index bd4244a..15f9b10 100644 --- a/src/sdl/SDL/SDL.spec.ts +++ b/src/sdl/SDL/SDL.spec.ts @@ -1,6 +1,7 @@ import { faker } from "@faker-js/faker"; import template from "lodash/template"; import omit from "lodash/omit"; +import { dump } from "js-yaml"; import { readYml } from "../../../test/read-yml"; import { SdlValidationError } from "../../error"; @@ -18,7 +19,7 @@ describe("SDL", () => { password: faker.internet.password() }; const yml = createYML({ - credentials: createCreds(credentials) + credentials: toYmlFragment({ credentials }, { nestingLevel: 2 }) }); const sdl = SDL.fromString(yml, "beta3", "sandbox"); @@ -53,7 +54,7 @@ describe("SDL", () => { it.each(fields)('should throw an error when credentials are missing "%s"', field => { const yml = createYML({ - credentials: createCreds(omit(credentials, field)) + credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 }) }); expect(() => { @@ -64,7 +65,7 @@ describe("SDL", () => { it.each(fields)('should throw an error when credentials "%s" is empty', field => { credentials[field] = ""; const yml = createYML({ - credentials: createCreds(omit(credentials, field)) + credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 }) }); expect(() => { @@ -75,7 +76,7 @@ describe("SDL", () => { it.each(fields)('should throw an error when credentials "%s" contains spaces only', field => { credentials[field] = " "; const yml = createYML({ - credentials: createCreds(omit(credentials, field)) + credentials: toYmlFragment({ credentials: omit(credentials, field) }, { nestingLevel: 2 }) }); expect(() => { @@ -86,24 +87,20 @@ describe("SDL", () => { }); }); -function createCreds({ host, username, password }: { host?: string; username?: string; password?: string }) { - let creds = ""; +type YmlInputObject = { + [key: string]: string | number | boolean | null | YmlInputObject; +}; - if (host) { - creds += ` host: "${host}"\n`; - } - - if (username) { - creds += ` username: "${username}"\n`; - } - - if (password) { - creds += ` password: "${password}"\n`; - } +function toYmlFragment(object: YmlInputObject, options?: { nestingLevel: number }): string { + const yamlString = dump(object); - if (creds) { - creds = ` credentials:\n${creds}`; + if (!options) { + return yamlString; } - return creds; + const indentation = " ".repeat(options.nestingLevel); + return yamlString + .split("\n") + .map(line => indentation + line) + .join("\n"); }