From bd249e4c0288b2f9ceea20d86fcf26c2401122ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 30 Jul 2017 17:25:10 -0700 Subject: [PATCH] Proper behaviour tests for ui.js Checks whenever code does what it suppose to, not only if it runs --- assets/static/alerts.js | 4 +- assets/static/templates.js | 17 +++++--- assets/static/ui.js | 13 +----- assets/static/ui.test.js | 85 +++++++++++++++++++++++++++++++++++--- assets/static/unsee.js | 2 +- bindata_assetfs.go | 44 ++++++++++---------- 6 files changed, 118 insertions(+), 47 deletions(-) diff --git a/assets/static/alerts.js b/assets/static/alerts.js index d5824eb3c..3c990a144 100644 --- a/assets/static/alerts.js +++ b/assets/static/alerts.js @@ -30,7 +30,9 @@ AlertGroup.prototype.Render = function() { // called after group was rendered for the first time AlertGroup.prototype.Added = function() { - ui.setupAlertGroupUI($("#" + this.id)); + var elem = $("#" + this.id); + ui.setupGroupTooltips(elem); + ui.setupGroupLinkHover(elem); }; AlertGroup.prototype.Update = function() { diff --git a/assets/static/templates.js b/assets/static/templates.js index 975654cac..c2e8f605a 100644 --- a/assets/static/templates.js +++ b/assets/static/templates.js @@ -52,14 +52,18 @@ function getConfig() { return config; } +function loadTemplate(name, selector) { + try { + templates[name] = _.template($(selector).html()); + } catch (err) { + console.error("Failed to parse template " + name + " " + selector); + console.error(err); + } +} + function init() { $.each(config, function(name, selector) { - try { - templates[name] = _.template($(selector).html()); - } catch (err) { - console.error("Failed to parse template " + name + " " + selector); - console.error(err); - } + loadTemplate(name, selector); }); } @@ -82,4 +86,5 @@ function renderTemplate(name, context) { exports.init = init; exports.getConfig = getConfig; +exports.loadTemplate = loadTemplate; exports.renderTemplate = renderTemplate; diff --git a/assets/static/ui.js b/assets/static/ui.js index 0e7f16eb1..2849a75a3 100644 --- a/assets/static/ui.js +++ b/assets/static/ui.js @@ -75,15 +75,6 @@ function setupGroupTooltips(groupElem) { }); } -function setupAlertGroupUI(elem) { - setupGroupLinkHover(elem); - setupGroupTooltips(elem); -} - -function init() { - setupModal(); -} - -exports.init = init; -exports.setupAlertGroupUI = setupAlertGroupUI; +exports.setupModal = setupModal; exports.setupGroupTooltips = setupGroupTooltips; +exports.setupGroupLinkHover = setupGroupLinkHover; diff --git a/assets/static/ui.test.js b/assets/static/ui.test.js index 1090074ad..ceb61270a 100644 --- a/assets/static/ui.test.js +++ b/assets/static/ui.test.js @@ -1,28 +1,101 @@ const $ = window.$ = window.jQuery = require("jquery"); -test("ui init()", () => { +jest.useFakeTimers(); + +test("ui setupModal()", () => { + document.body.innerHTML = + "" + + "" + + "