Skip to content

Commit

Permalink
Add unit tests for validateLanguageRequested
Browse files Browse the repository at this point in the history
function
  • Loading branch information
heliomar-pena committed Nov 17, 2023
1 parent f234aa7 commit 148d815
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions utils/validateLanguageRequested.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const supportedLanguages = require('../SUPPORTED-LANGUAGES.json');
const { getSupportedLanguagesWithNames } = require("./formatSupportedLanguages");
const {
validateLanguageRequested,
} = require("./validateLanguageRequested");

describe("validateLanguageRequested", () => {
it("should return true if the requested language is supported", () => {
const requestedLanguage = "en";

const result = validateLanguageRequested(requestedLanguage);

expect(result).toBe(true);
});

it("should throw an error if no language is provided", () => {
const requestedLanguage = "";
const error = new Error("No language provided");

expect(() => {
validateLanguageRequested(requestedLanguage);
}).toThrowError(`${error.message}.\n\nPlease use one of these:\n\n${getSupportedLanguagesWithNames(supportedLanguages).join('\n')}`);
});

it("should throw an error if the requested language is not supported", () => {
const requestedLanguage = "invalid-language";
const error = new Error(`Language ${requestedLanguage} is not supported`);

expect(() => {
validateLanguageRequested(requestedLanguage);
}).toThrowError(`${error.message}.\n\nPlease use one of these:\n\n${getSupportedLanguagesWithNames(supportedLanguages).join('\n')}`);
});

it("should throw an error if the requested language is not supported by any engine", () => {
const requestedLanguage = "ab";
const error = new Error(`Language ${requestedLanguage} is not supported by any engine`);

expect(() => {
validateLanguageRequested(requestedLanguage);
}).toThrowError(`${error.message}.\n\nPlease use one of these:\n\n${getSupportedLanguagesWithNames(supportedLanguages).join('\n')}`);
});
});

0 comments on commit 148d815

Please sign in to comment.