Skip to content

Commit

Permalink
Tests for permissions.js (joomla#13)
Browse files Browse the repository at this point in the history
* Test suite for permissions.js
* Added karma-jasmine-ajax plugin
  • Loading branch information
Ruchiranga authored and yvesh committed Jun 26, 2016
1 parent 839aacd commit 4ad9c2a
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
3 changes: 2 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -69,6 +69,7 @@ module.exports = function (config) {
// list of plugins
plugins: [
'karma-jasmine',
'karma-jasmine-ajax',
'karma-firefox-launcher',
'karma-coverage',
'karma-requirejs',
Expand Down
1 change: 1 addition & 0 deletions tests/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions tests/javascript/permissions/fixtures/fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id="permissionsjs">
<span id="icon_0" class="icon"></span>
<div id="ajax-test">
<table>
<tr>
<div>
<td>
<input id="sendBtn" type="button">
</td>
</div>
<td>
<span></span>
</td>
</tr>
</table>
</div>
</div>
32 changes: 32 additions & 0 deletions tests/javascript/permissions/spec-setup.js
Original file line number Diff line number Diff line change
@@ -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: ''
}
};
});
110 changes: 110 additions & 0 deletions tests/javascript/permissions/spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});

0 comments on commit 4ad9c2a

Please sign in to comment.