Skip to content

Commit

Permalink
Add functions to layouts module for a cleaner API
Browse files Browse the repository at this point in the history
  • Loading branch information
suvash committed Mar 1, 2021
1 parent 416c5b5 commit 5448a96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
25 changes: 24 additions & 1 deletion src/layouts/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import romanized from "./romanized";
import traditional from "./traditional";

const layouts = {
const layoutStore = {
romanized: romanized,
traditional: traditional,
};

function availableLayouts() {
return Object.keys(layoutStore);
}

function isValidLayout(layout) {
return availableLayouts().includes(layout);
}

function fetchLayout(layout) {
if (isValidLayout(layout)) {
return layoutStore[layout];
} else {
const msg = "Invalid Layout : " + layout;
throw new Error(msg);
}
}

const layouts = {
fetchLayout: fetchLayout,
isValidLayout: isValidLayout,
availableLayouts: availableLayouts,
};

export default layouts;
35 changes: 31 additions & 4 deletions test/layouts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@ import { expect } from "chai";
import layouts from "../src/layouts";

describe("layouts", () => {
it("should return an object of available layouts", () => {
expect(layouts).to.be.an("object");
expect(layouts).to.have.property("romanized");
expect(layouts).to.have.property("traditional");
describe("availableLayouts()", () => {
it("should return a list of available layouts", () => {
const expected = ["romanized", "traditional"];
expect(layouts.availableLayouts()).to.deep.equal(expected);
});
});

describe("isValidLayout(layout_name)", () => {
it("should return a boolean given a layout name", () => {
expect(layouts.isValidLayout("bogus")).to.equal(false);
expect(layouts.isValidLayout("random")).to.equal(false);

expect(layouts.isValidLayout("romanized")).to.equal(true);
expect(layouts.isValidLayout("traditional")).to.equal(true);
});
});

describe("fetchLayout(layout_name)", () => {
it("when valid should return a layout object", () => {
expect(layouts.fetchLayout("romanized")).to.be.an("object");
expect(() => {
layouts.fetchLayout("romanized");
}).to.not.throw("Invalid Layout");
expect(layouts.fetchLayout("traditional").name).to.equal("traditional");
});

it("when invalid should throw an exception", () => {
expect(() => {
layouts.fetchLayout("bogus");
}).to.throw("Invalid Layout");
});
});
});

0 comments on commit 5448a96

Please # to comment.