From 63f7b8016e97f0c0f3f0923e9223405dbd9d1f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Thu, 13 Jun 2013 17:37:02 +0200 Subject: [PATCH] Add tests for javascript module implementation This commit adds tests for the module loader and registry of icinga2-web. It mainly registers event handlers and calls custom enable/disable functions refs #3753 --- test/js/test/icinga/moduleTest.js | 190 ++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 test/js/test/icinga/moduleTest.js diff --git a/test/js/test/icinga/moduleTest.js b/test/js/test/icinga/moduleTest.js new file mode 100644 index 000000000..da89f4558 --- /dev/null +++ b/test/js/test/icinga/moduleTest.js @@ -0,0 +1,190 @@ +/** +* Test cases for the module loading implementation +* +* +**/ + + +// {{LICENSE_HEADER}} +// {{LICENSE_HEADER}} +var should = require("should"); +var rjsmock = require("requiremock.js"); + +var BASE = "../../../../public/js/"; +require(BASE+"icinga/module.js"); + +var module = rjsmock.getDefine(); + +/** +* Test module that only uses eventhandlers and +* no custom logic +**/ +var eventOnlyModule = function() { + var thiz = this; + this.moduleLinkClick = false; + this.formFocus = false; + + var onModuleLinkClick = function() { + thiz.moduleLinkClick = true; + }; + var onFormFocus = function() { + thiz.formFocus = true; + }; + this.eventHandler = { + '.myModule a' : { + 'click': onModuleLinkClick + }, + '.myModule div.test input' : { + 'focus' : onFormFocus + } + }; +}; +/** +* Module that defines an own enable and disable function +* that is called additionally to the eventhandler setup +* +**/ +var customLogicModule = function() { + var thiz = this; + this.clicked = false; + this.customEnable = false; + this.customDisable = false; + + var onClick = function() { + thiz.clicked = true; + }; + + this.enable = function() { + thiz.customEnable = true; + }; + + this.disable = function() { + thiz.customDisable = true; + }; + + this.eventHandler = { + '.myModule a' : { + 'click' : onClick + } + }; +}; + +var setupTestDOM = function() { + tearDownTestDOM(); + $('body').append($('
') + .append($('funkey')) + .append($('
') + .append(''))); +}; + +var tearDownTestDOM = function() { + $('body').off(); + $('body').empty(); +}; + +describe('Module loader', function() { + var err = null; + var errorCallback = function(error) { + err = error; + }; + + + it('Should call the errorCallback when module isn\'t found', function() { + err = null; + rjsmock.purgeDependencies(); + module.resetHard(); + module.enableModule("testModule", errorCallback); + should.exist(err); + }); + + + it('Should register model event handlers when an \'eventHandler\' attribute exists', function() { + rjsmock.purgeDependencies(); + var testModule = new eventOnlyModule(); + rjsmock.registerDependencies({ + testModule: testModule + }); + err = null; + var toTest = null; + + // Test event handler + setupTestDOM(); + + // Enable the module and asser it is recognized and enabled + module.enableModule("testModule", errorCallback, function(enabled) { + toTest = enabled; + }); + should.not.exist(err, "An error occured during loading: "+err); + should.exist(toTest, "The injected test module didn't work!"); + should.exist(toTest.enable, "Implicit enable method wasn't created"); + $('.myModule a').click(); + should.equal(toTest.moduleLinkClick, true, "Click on link should trigger handler"); + + $('.myModule div.test input').focus(); + should.equal(toTest.formFocus, true, "Form focus should trigger handler"); + + tearDownTestDOM(); + }); + + it('Should be able to deregister events handlers when disable() is called', function() { + rjsmock.purgeDependencies(); + var testModule = new eventOnlyModule(); + rjsmock.registerDependencies({ + testModule: testModule + }); + err = null; + var toTest = null; + + setupTestDOM(); + + module.enableModule("testModule", errorCallback, function(enabled) { + toTest = enabled; + }); + should.not.exist(err, "An error occured during loading: "+err); + should.exist(toTest, "The injected test module didn't work!"); + should.exist(toTest.enable, "Implicit enable method wasn't created"); + + $('.myModule a').click(); + should.equal(toTest.moduleLinkClick, true, "Click on link should trigger handler"); + toTest.moduleLinkClick = false; + + module.disableModule("testModule"); + $('.myModule a').click(); + should.equal(toTest.moduleLinkClick, false, "Click on link shouldn't trigger handler when module is disabled"); + tearDownTestDOM(); + $('body').unbind(); + }); + + it('Should additionally call custom enable and disable functions', function() { + + rjsmock.purgeDependencies(); + var testModule = new customLogicModule(); + rjsmock.registerDependencies({ + testModule: testModule + }); + err = null; + var toTest = null; + + // Test event handler + setupTestDOM(); + + module.enableModule("testModule", errorCallback, function(enabled) { + toTest = enabled; + }); + should.not.exist(err, "An error occured during loading: "+err); + should.exist(toTest, "The injected test module didn't work!"); + should.exist(toTest.enable, "Implicit enable method wasn't created"); + should.equal(toTest.customEnable, true, "Custom enable method wasn't called"); + $('.myModule a').click(); + should.equal(toTest.clicked, true, "Click on link should trigger handler"); + toTest.clicked = false; + + module.disableModule("testModule"); + should.equal(toTest.customDisable, true, "Custom disable method wasn't called"); + $('.myModule a').click(); + should.equal(toTest.clicked, false, "Click on link shouldn't trigger handler when module is disabled"); + tearDownTestDOM(); + $('body').unbind(); + }); + +});