From 4ad9c2aee73ea9d3e2257c597f86d88abf55748a Mon Sep 17 00:00:00 2001 From: Ruchiranga Date: Sun, 26 Jun 2016 17:07:01 +0530 Subject: [PATCH] Tests for permissions.js (#13) * Test suite for permissions.js * Added karma-jasmine-ajax plugin --- karma.conf.js | 3 +- tests/javascript/package.json | 1 + .../permissions/fixtures/fixture.html | 17 +++ tests/javascript/permissions/spec-setup.js | 32 +++++ tests/javascript/permissions/spec.js | 110 ++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 tests/javascript/permissions/fixtures/fixture.html create mode 100644 tests/javascript/permissions/spec-setup.js create mode 100644 tests/javascript/permissions/spec.js diff --git a/karma.conf.js b/karma.conf.js index 2bc1c5031f01f..2ad232326efd6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -8,7 +8,7 @@ module.exports = function (config) { // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['jasmine', 'requirejs'], + frameworks: ['jasmine-ajax', 'jasmine', 'requirejs'], // list of files / patterns to load in the browser files: [ @@ -69,6 +69,7 @@ module.exports = function (config) { // list of plugins plugins: [ 'karma-jasmine', + 'karma-jasmine-ajax', 'karma-firefox-launcher', 'karma-coverage', 'karma-requirejs', diff --git a/tests/javascript/package.json b/tests/javascript/package.json index e75ee185963f0..2d7855c927ca3 100644 --- a/tests/javascript/package.json +++ b/tests/javascript/package.json @@ -20,6 +20,7 @@ "karma-coverage": "^1.0.0", "karma-firefox-launcher": "^0.1.7", "karma-jasmine": "^0.3.8", + "karma-jasmine-ajax": "^0.1.13", "karma-requirejs": "^1.0.0", "karma-verbose-reporter": "0.0.3", "requirejs": "^2.2.0", diff --git a/tests/javascript/permissions/fixtures/fixture.html b/tests/javascript/permissions/fixtures/fixture.html new file mode 100644 index 0000000000000..e1fab7bba0d3b --- /dev/null +++ b/tests/javascript/permissions/fixtures/fixture.html @@ -0,0 +1,17 @@ +
+ +
+ + +
+
+ + + +
+ + + +
+
+
diff --git a/tests/javascript/permissions/spec-setup.js b/tests/javascript/permissions/spec-setup.js new file mode 100644 index 0000000000000..b2ffd94737600 --- /dev/null +++ b/tests/javascript/permissions/spec-setup.js @@ -0,0 +1,32 @@ +/** + * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + * @package Joomla + * @subpackage JavaScript Tests + * @since 3.6 + * @version 1.0.0 + */ + +define(['jquery', 'text!testsRoot/permissions/fixtures/fixture.html', 'libs/permissions', 'libs/core', 'jasmineJquery'], function ($, fixture) { + $('body').append(fixture); + + window.id = '0'; + window.value = '1'; + + event = {target: '#sendBtn'}; + + responses = { + success: { + status: 200, + statusText: 'HTTP/1.1 200 OK', + contentType: 'text/plain', + responseText: '{"data": "true", "message": "0"}' + }, + fail: { + status: 404, + statusText: 'HTTP/1.1 404 Not Found', + contentType: 'text/plain', + responseText: '' + } + }; +}); diff --git a/tests/javascript/permissions/spec.js b/tests/javascript/permissions/spec.js new file mode 100644 index 0000000000000..ce2e18dc4ea72 --- /dev/null +++ b/tests/javascript/permissions/spec.js @@ -0,0 +1,110 @@ +/** + * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + * @package Joomla + * @subpackage JavaScript Tests + * @since 3.6 + * @version 1.0.0 + */ + +define(['jquery', 'testsRoot/permissions/spec-setup', 'jasmineJquery'], function ($) { + describe('sendPermissions', function () { + beforeAll(function() { + jasmine.Ajax.install(); + Joomla.JText._ = jasmine.createSpy('_'); + Joomla.renderMessages = jasmine.createSpy('renderMessages'); + sendPermissions(event); + }); + + afterAll(function () { + jasmine.Ajax.uninstall(); + }); + + it("should remove attribute class from icon", function() { + expect($('#icon_0')).not.toHaveAttr('class'); + }); + + it("should set style attribute to display the spinner in icon", function() { + expect($('#icon_0')).toHaveAttr('style', 'background: url(../media/system/images/modal/spinner.gif); display: inline-block; width: 16px; height: 16px'); + }); + + describe("on success with resp.data == 'true' & resp.message == 0", function() { + var spanContainer = $('#ajax-test'); + + beforeAll(function() { + sendPermissions(event); + request = jasmine.Ajax.requests.mostRecent(); + request.respondWith(responses.success); + }); + + it("should make a AJAX request of type GET", function() { + expect(request.method).toBe('GET'); + }); + + it("should set attribute class in icon to icon-cancel", function() { + expect($('#icon_0')).toHaveAttr('class', 'icon-cancel'); + }); + + it("should remove classes label label-important from span elements", function() { + expect(spanContainer.find('span')).not.toHaveClass('label label-important'); + }); + + it("should add classes label label-success to span elements", function() { + expect(spanContainer.find('span')).toHaveClass('label label-success'); + }); + + it("should remove attribute style from icon", function() { + expect($('#icon_0')).not.toHaveAttr('style'); + }); + + it("should call Joomla.JText._('JLIB_RULES_ALLOWED')", function() { + expect(Joomla.JText._).toHaveBeenCalledWith('JLIB_RULES_ALLOWED'); + }); + + it("should call Joomla.renderMessages({ error: [undefined] })", function() { + expect(Joomla.renderMessages).toHaveBeenCalledWith({ error: [undefined] }); + }); + }); + + describe("on success with resp.data !== 'true' & resp.message !== 0", function() { + beforeAll(function() { + sendPermissions(event); + request = jasmine.Ajax.requests.mostRecent(); + responses.success.responseText = '{"data": "false", "message": "1"}'; + request.respondWith(responses.success); + }); + + it("should call Joomla.renderMessages({ error: [undefined] })", function() { + expect(Joomla.renderMessages).toHaveBeenCalledWith({ error: [undefined] }); + }); + + it("should remove attribute style from icon", function() { + expect($('#icon_0')).not.toHaveAttr('style'); + }); + + it("should set attribute class in icon to icon-cancel", function() { + expect($('#icon_0')).toHaveAttr('class', 'icon-cancel'); + }); + }); + + describe("on failure", function() { + beforeAll(function() { + sendPermissions(event); + request = jasmine.Ajax.requests.mostRecent(); + request.respondWith(responses.fail); + }); + + it("should call Joomla.renderMessages({ error: [undefined] })", function() { + expect(Joomla.renderMessages).toHaveBeenCalledWith({ error: [undefined] }); + }); + + it("should remove attribute style from icon", function() { + expect($('#icon_0')).not.toHaveAttr('style'); + }); + + it("should set attribute class in icon to icon-cancel", function() { + expect($('#icon_0')).toHaveAttr('class', 'icon-cancel'); + }); + }); + }); +});