Skip to content

Commit

Permalink
fix(init): better configuration handling in init
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoTron committed Oct 17, 2017
1 parent b308d28 commit bd81a06
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"nyc report --reporter=lcov | codecov -t c1cea4d6-9aec-445c-8179-bc5543358876",
"prebuild": "rimraf dist",
"build-with-files":
"npm run prebuild && babel --out-dir dist --ignore *.test.js src",
"build":
"npm run prebuild && babel --copy-files --out-dir dist --ignore *.test.js src",
"build": "npm run prebuild && babel --out-dir dist --ignore *.test.js src",
"test": "mocha --compilers js:babel-register src/index.test.js -w",
"test:single": "nyc mocha --compilers js:babel-register src/index.test.js",
"semantic-release":
Expand Down Expand Up @@ -57,6 +56,7 @@
},
"dependencies": {
"date-fns": "1.28.5",
"lodash": "4.17.4",
"mockdate": "2.0.2"
}
}
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hoursJson from "./hours.json";
import setMinutes from "date-fns/set_minutes";
import setHours from "date-fns/set_hours";
import getDay from "date-fns/get_day";
Expand All @@ -8,7 +7,7 @@ import isFuture from "date-fns/is_future";
import addDays from "date-fns/add_days";
import isEqual from "date-fns/is_equal";
import isBefore from "date-fns/is_before";

import _ from "lodash";
const weekdays = [
"Sunday",
"Monday",
Expand All @@ -35,9 +34,10 @@ class BusinessHours {
}

init(hours) {
if (hours === undefined) {
hours = hoursJson;
if (_.isEmpty(hours)) {
throw new Error("Hours are not set. Check your init() function.");
}

weekdays.forEach((day, index) => {
if (!hours.hasOwnProperty(index.toString())) {
throw new Error(day + " is missing from config");
Expand All @@ -49,11 +49,11 @@ class BusinessHours {
console.error(day + " is missing 'to' in config");
} else if (!this._isHourValid(hours[index.toString()][0].from)) {
console.error(
day + "'s 'from1' has not the right format. Should be ##:##"
day + "'s 'from' has not the right format. Should be ##:##"
);
} else if (!this._isHourValid(hours[index.toString()][0].to)) {
console.error(
day + "'s 'to1' has not the right format. Should be ##:##"
day + "'s 'to' has not the right format. Should be ##:##"
);
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var expect = require("chai").expect;
var bh = require("./index.js");
var hoursJson = require("./hours.json");
var hoursJson2 = require("./hours2.json");
var MockDate = require("mockdate");
import format from "date-fns/format";

bh.init();
bh.init(hoursJson);

describe("business-hours-js", function() {
after(function() {
Expand Down Expand Up @@ -165,5 +166,25 @@ describe("business-hours-js", function() {
"Sunday is missing from config"
);
});
it("missing config, empty object", function() {
expect(bh.init.bind(bh, {})).to.throw(
"Hours are not set. Check your init() function."
);
});
it("missing config, null ", function() {
expect(bh.init.bind(bh, null)).to.throw(
"Hours are not set. Check your init() function."
);
});
it("missing config, undefined ", function() {
expect(bh.init.bind(bh, undefined)).to.throw(
"Hours are not set. Check your init() function."
);
});
it("missing config, {'a':'test'} ", function() {
expect(bh.init.bind(bh, { a: "test" })).to.throw(
"Sunday is missing from config"
);
});
});
});

0 comments on commit bd81a06

Please sign in to comment.