From 6d053d65e795e10ae1a9cbad760fe4bc984b36d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 21 Dec 2017 20:09:12 -0500 Subject: [PATCH 1/2] Add tests for the Handlebars helper `equal` --- client/js/libs/handlebars/equal.js | 5 ++++ test/client/js/libs/handlebars/equalTest.js | 26 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/client/js/libs/handlebars/equalTest.js diff --git a/client/js/libs/handlebars/equal.js b/client/js/libs/handlebars/equal.js index 6a8033ca..2d25ea9d 100644 --- a/client/js/libs/handlebars/equal.js +++ b/client/js/libs/handlebars/equal.js @@ -1,8 +1,13 @@ "use strict"; module.exports = function(a, b, opt) { + if (arguments.length !== 3) { + throw new Error("Handlebars helper `equal` expects 3 arguments"); + } + a = a.toString(); b = b.toString(); + if (a === b) { return opt.fn(this); } diff --git a/test/client/js/libs/handlebars/equalTest.js b/test/client/js/libs/handlebars/equalTest.js new file mode 100644 index 00000000..c386fbd5 --- /dev/null +++ b/test/client/js/libs/handlebars/equalTest.js @@ -0,0 +1,26 @@ +"use strict"; + +const expect = require("chai").expect; +const equal = require("../../../../../client/js/libs/handlebars/equal"); + +describe("equal Handlebars helper", function() { + const block = { + fn: () => "fn", + inverse: () => "inverse", + }; + + it("should render the first block if both values are equal", function() { + expect(equal("foo", "foo", block)).to.equal("fn"); + }); + + it("should render the inverse block if values are not equal", function() { + expect(equal("foo", "bar", block)).to.equal("inverse"); + }); + + it("should throw if too few or too many arguments are given", function() { + expect(() => equal("foo", block)).to.throw(Error, /expects 3 arguments/); + + expect(() => equal("foo", "bar", "baz", block)) + .to.throw(Error, /expects 3 arguments/); + }); +}); From 371c5bcac2c52a225da57ae69ee619868e0c843b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 21 Dec 2017 20:09:50 -0500 Subject: [PATCH 2/2] Add a `notEqual` block helper for Handlebars --- client/js/libs/handlebars/notEqual.js | 18 +++++++++++ client/views/actions/whois.tpl | 2 +- .../client/js/libs/handlebars/notEqualTest.js | 30 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 client/js/libs/handlebars/notEqual.js create mode 100644 test/client/js/libs/handlebars/notEqualTest.js diff --git a/client/js/libs/handlebars/notEqual.js b/client/js/libs/handlebars/notEqual.js new file mode 100644 index 00000000..dfd06d44 --- /dev/null +++ b/client/js/libs/handlebars/notEqual.js @@ -0,0 +1,18 @@ +"use strict"; + +module.exports = function(a, b, opt) { + if (arguments.length !== 3) { + throw new Error("Handlebars helper `notEqual` expects 3 arguments"); + } + + a = a.toString(); + b = b.toString(); + + if (a !== b) { + return opt.fn(this); + } + + if (opt.inverse(this) !== "") { + throw new Error("Handlebars helper `notEqual` does not take an `else` block"); + } +}; diff --git a/client/views/actions/whois.tpl b/client/views/actions/whois.tpl index 40c66977..f1e15881 100644 --- a/client/views/actions/whois.tpl +++ b/client/views/actions/whois.tpl @@ -10,7 +10,7 @@ {{#if whois.actualhost}}
Actual host:
-
{{whois.actualip}}{{#equal whois.actualhost whois.actualip}}{{else}} ({{whois.actualhost}}){{/equal}}
+
{{whois.actualip}}{{#notEqual whois.actualhost whois.actualip}} ({{whois.actualhost}}){{/notEqual}}
{{/if}} {{#if whois.real_name}} diff --git a/test/client/js/libs/handlebars/notEqualTest.js b/test/client/js/libs/handlebars/notEqualTest.js new file mode 100644 index 00000000..40a37230 --- /dev/null +++ b/test/client/js/libs/handlebars/notEqualTest.js @@ -0,0 +1,30 @@ +"use strict"; + +const expect = require("chai").expect; +const notEqual = require("../../../../../client/js/libs/handlebars/notEqual"); + +describe("notEqual Handlebars helper", function() { + const block = { + fn: () => "fn", + }; + + it("should render the block if both values are equal", function() { + expect(notEqual("foo", "bar", block)).to.equal("fn"); + }); + + it("should throw if too few or too many arguments are given", function() { + expect(() => notEqual("foo", block)).to.throw(Error, /expects 3 arguments/); + + expect(() => notEqual("foo", "bar", "baz", block)) + .to.throw(Error, /expects 3 arguments/); + }); + + it("should throw if too few or too many arguments are given", function() { + const blockWithElse = { + fn: () => "fn", + inverse: () => "inverse", + }; + + expect(() => notEqual("foo", "foo", blockWithElse)).to.throw(Error, /else/); + }); +});