Skip to content

Commit

Permalink
More test + throw correct Error
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed Oct 3, 2023
1 parent 8b47993 commit 3d51680
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/file-handling/fileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class FileHandler {
*/
async handle(filePath, locale) {
const handler = this.#getHandler(filePath);
if (!handler) throw new Error(`Could not find handler for ${filePath}`);
if (!handler)
throw new LoadingException(`Could not find handler for ${filePath}. Supported file extensions are ${this.getSupportedFileExtensions()}`);
const textContent = await this.#readFileContent(filePath);
const dictionary = await handler.load(filePath, textContent, locale);

Expand Down
75 changes: 48 additions & 27 deletions test/compiler/file-handling/fileHandler.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
import { describe, it, expect } from "vitest";
import { FileHandler } from "../../../src/file-handling/fileHandler.js";
import { LoadingException } from "../../../src/file-handling/exception.js";

describe("FileHandler", () => {
it("correctly reports which file extensions it supports, if none are", () => {
const handler = new FileHandler([]);
expect(handler.getSupportedFileExtensions()).toEqual(new Set());
});

it("correctly reports which file extensions it supports, if one handler is registered", () => {
const handler = new FileHandler([
{
fileExtensions: ["json"],
load: async () => new Map(),
},
]);
expect(handler.getSupportedFileExtensions()).toEqual(new Set(["json"]));
});
describe("handle", () => {
it("throws LoadingException if no handler is found", async () => {
const handler = new FileHandler([]);
expect(async () => await handler.handle("foo.bar", "en")).toThrow(LoadingException);
});

it("throws LoadingException if the file cannot be read", async () => {
const handler = new FileHandler([
{
fileExtensions: ["json"],
load: async () => new Map(),
},
]);
expect(async () => await handler.handle("nonexistent.json", "en")).toThrow(LoadingException);
});
});

describe("getSupportedFileExtensions", () => {
it("correctly reports which file extensions it supports, if none are", () => {
const handler = new FileHandler([]);
expect(handler.getSupportedFileExtensions()).toEqual(new Set());
});

it("correctly reports which file extensions it supports, if one handler is registered", () => {
const handler = new FileHandler([
{
fileExtensions: ["json"],
load: async () => new Map(),
},
]);
expect(handler.getSupportedFileExtensions()).toEqual(new Set(["json"]));
});

it("correctly reports which file extensions it supports, if multiple handlers are registered", () => {
const handler = new FileHandler([
{
fileExtensions: ["json"],
load: async () => new Map(),
},
{
fileExtensions: ["yaml", "yml"],
load: async () => new Map(),
},
]);
expect(handler.getSupportedFileExtensions()).toEqual(
new Set(["json", "yaml", "yml"])
);
it("correctly reports which file extensions it supports, if multiple handlers are registered", () => {
const handler = new FileHandler([
{
fileExtensions: ["json"],
load: async () => new Map(),
},
{
fileExtensions: ["yaml", "yml"],
load: async () => new Map(),
},
]);
expect(handler.getSupportedFileExtensions()).toEqual(
new Set(["json", "yaml", "yml"])
);
});
});
});

0 comments on commit 3d51680

Please sign in to comment.