Skip to content

Commit

Permalink
Corejs tests (joomla#15)
Browse files Browse the repository at this point in the history
Tests for core.js
  • Loading branch information
Ruchiranga authored and yvesh committed Jun 26, 2016
1 parent cbdc7fb commit 839aacd
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 6 deletions.
3 changes: 1 addition & 2 deletions tests/javascript/caption/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ define(['jquery', 'testsRoot/caption/spec-setup', 'jasmineJquery'], function ($)
var $element = $('img#no-options');
it('Should have container CSS as {width: element.width, float: none}', function () {
expect($element.parent()).toHaveCss({
float: 'none',
width: $element.width() + 'px'
float: 'none'
});
});
});
Expand Down
39 changes: 39 additions & 0 deletions tests/javascript/core/fixtures/fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div id="corejs">
<div id="submitform">
<form id="adminForm" onsubmit="return false"></form>
</div>
<div id="replace-tokens">
<input class="replace-tokens-input" type="hidden" value="1" name="123456789123456789123456789EDBCA">
<input class="replace-tokens-input" type="hidden" value="1" name="123456789123456789123456789EDBCA">
<input class="replace-tokens-input" id="invalid-type" type="submit" value="1" name="123456789123456789123456789EDBCA">
<input class="replace-tokens-input" id="invalid-value" type="hidden" value="0" name="123456789123456789123456789EDBCA">
<input class="replace-tokens-input" id="invalid-name" type="hidden" value="1" name="123">
</div>
<div id="check-all">
<input type="checkbox" id="cb-no-form" checked>
<form id="check-all-form">
<input type="checkbox" class="checked" id="cb0" checked>
<input type="checkbox" class="unchecked" id="cb1">
<input type="checkbox" class="unchecked" id="cb2">
<input type="checkbox" class="unchecked" id="no-cb3">
</form>
<form id="check-all-stub-form">
<input type="checkbox" id="stub-check-test-1" checked>
<input type="checkbox" id="stub-check-test-2">
</form>
</div>
<div id="render-messages-remove-messages">
<div id="system-message-container">
</div>
</div>
<div id="ischecked">
<form id="ischecked-test-form">
<input type="checkbox" id="check-all-box" name="checkall-toggle">
<input type="checkbox">
</form>
</div>
<div id="table-ordering">
<form id="table-ordering-test-form">
</form>
</div>
</div>
12 changes: 12 additions & 0 deletions tests/javascript/core/spec-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @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/core/fixtures/fixture.html', 'libs/core', 'jasmineJquery'], function ($, fixture) {
$('body').append(fixture);
});
188 changes: 188 additions & 0 deletions tests/javascript/core/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* @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/core/spec-setup', 'jasmineJquery'], function ($) {

describe('Joomla.submitform', function () {
var form = document.getElementById('adminForm');
form.task = {};

beforeEach(function () {
form.removeChild = function (element) {
return true;
};

spyOnEvent('#adminForm', 'submit')
spyOn(form, 'removeChild');

Joomla.submitform('article.add', form, true);
});

it('should assign task to form.task.value', function () {
expect(form.task.value).toEqual('article.add');
});
it('should set attribute novalidate to false', function () {
expect($(form)).toHaveAttr('novalidate', 'false');
});
it('should add input submit button to DOM', function () {
expect($('#adminForm')).toContainElement('input[type="submit"]');
});
it('should make the added input element invisible', function () {
expect($('#adminForm').children('input[type="submit"]')).not.toBeVisible();
});
it('should click the input element', function () {
expect('submit').toHaveBeenTriggeredOn('#adminForm');
});
it('should remove the input element', function () {
expect(form.removeChild).toHaveBeenCalled();
});
});

describe('Joomla.JText', function () {
var ob = {
'JTOGGLE_SHOW_SIDEBAR': 'Show Sidebar',
'Jtoggle_Hide_Sidebar': 'Hide Sidebar'
};
Joomla.JText.load(ob);

it('should add content passed via load() to the strings object', function () {
expect(Joomla.JText.strings.JTOGGLE_SHOW_SIDEBAR).toEqual('Show Sidebar');
expect(Joomla.JText.strings.JTOGGLE_HIDE_SIDEBAR).toEqual('Hide Sidebar');
});
it('should return \'Show Sidebar\' on calling Joomla.JText._(\'JTOGGLE_SHOW_SIDEBAR\', \'test\')', function () {
expect(Joomla.JText._('JTOGGLE_SHOW_SIDEBAR', 'test')).toEqual('Show Sidebar');
});
it('should return \'Show Sidebar\' on calling Joomla.JText._(\'Jtoggle_Show_Sidebar\', \'test\')', function () {
expect(Joomla.JText._('Jtoggle_Show_Sidebar', 'test')).toEqual('Show Sidebar');
});
it('should return \'test\' on calling Joomla.JText._(\'JTOGGLE_REMOVE_SIDEBAR\', \'test\')', function () {
expect(Joomla.JText._('JTOGGLE_REMOVE_SIDEBAR', 'test')).toEqual('test');
});
});

describe('Joomla.replaceTokens', function () {
var newToken = '123456789123456789123456789ABCDE';
Joomla.replaceTokens(newToken);

it('should set name of all hidden input elements with value = 1 and name = old token to new token', function () {
var elements =$('.replace-tokens-input');
expect(elements[0].name).toEqual(newToken);
expect(elements[1].name).toEqual(newToken);
});
it('should not set name of non hidden input elements to new token', function () {
expect($('.replace-tokens-input #invalid-type').name).not.toEqual(newToken);
});
it('should not set name of input elements with value other than 1 to new token', function () {
expect($('.replace-tokens-input #invalid-value').name).not.toEqual(newToken);
});
it('should not set name of input elements with invalid name to new token', function () {
expect($('.replace-tokens-input #invalid-name').name).not.toEqual(newToken);
});
});

describe('Joomla.checkAll', function () {
var form = document.getElementById('check-all-form');
form.boxchecked = {};

var element = document.getElementById('cb0');
Joomla.checkAll(element);

it('should return false when input element is not inside a form', function () {
expect(Joomla.checkAll(document.getElementById('cb-no-form'))).toEqual(false);
});

it('should check all the checkboxes that has id starting with \'cb\' inside the form', function () {
expect($('#cb0')).toBeChecked();
expect($('#cb1')).toBeChecked();
expect($('#cb2')).toBeChecked();
expect($('#no-cb3')).not.toBeChecked();
});

it('should set the number of checked boxes in the form to form.boxchecked', function () {
expect(form.boxchecked.value).toEqual(3);
});

it('should use passed in stub to look for input elements', function () {
var element = document.getElementById('stub-check-test-1');
Joomla.checkAll(element, 'stub');

expect($('#stub-check-test-1')).toBeChecked();
expect($('#stub-check-test-2')).toBeChecked();
});
});

describe('Joomla.renderMessages and Joomla.removeMessages', function () {
var messages = {
"message": ["Message one", "Message two"],
"error": ["Error one", "Error two"]
};

beforeAll(function () {
Joomla.JText.load({"message": "Message"});
Joomla.renderMessages(messages);
});

it('renderMessages should render titles when translated strings are available', function () {
expect($('h4.alert-heading').first()).toContainText('Message');
});

it('renderMessages should render messages inside a div having class alert-message', function () {
var messages = $('div.alert-success').children('div');
expect(messages[0]).toContainText('Message two');
expect(messages[1]).toContainText('Message one');
});

it('renderMessages should render errors inside a div having class alert-error', function () {
var messages = $('div.alert-error').children('div');
expect(messages[0]).toContainText('Error two');
expect(messages[1]).toContainText('Error one');
});

it('removeMessages should remove all content from system-message-container', function () {
Joomla.removeMessages();
expect($("#system-message-container")).toBeEmpty();
});
});

describe('Joomla.isChecked', function () {
var form = document.getElementById('ischecked-test-form');
form.boxchecked = {value: 5};

Joomla.isChecked(true, form);

it('should increase form.boxchecked.value from 5 to 6', function () {
expect(form.boxchecked.value).toEqual(6);
});
it('should set checkAllToggle.checked to false', function () {
expect(form.elements[ 'checkall-toggle' ].checked).toEqual(false);
});
});

describe('Joomla.tableOrdering', function () {
beforeAll(function () {
spyOn(Joomla, 'submitform');

this.form = document.getElementById('table-ordering-test-form');
this.form.filter_order = {};
this.form.filter_order_Dir = {};

Joomla.tableOrdering('order', 'dir', 'task', this.form);
});

it('should call Joomla.submitform with params task and form', function () {
expect(Joomla.submitform).toHaveBeenCalledWith('task', this.form);
});
it('should set form.filter_order.value = order', function () {
expect(this.form.filter_order.value).toEqual('order')
});
it('should set form.filter_order_Dir.value = dir', function () {
expect(this.form.filter_order_Dir.value).toEqual('dir')
});
});
});
16 changes: 12 additions & 4 deletions tests/javascript/validate/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,18 @@ define(['jquery', 'testsRoot/validate/spec-setup', 'jasmineJquery'], function ($
});

describe('isValid method on button click', function () {
Joomla.renderMessages = function() {
return true;
};
$('#button').trigger( "click" );
beforeAll(function () {
fn = Joomla.renderMessages;

Joomla.renderMessages = function() {
return true;
};
$('#button').trigger( "click" );
});

afterAll(function () {
Joomla.renderMessages = fn;
});

it('should add class invalid to element #isvalid-numeric-nan', function () {
expect(element.find('#isvalid-numeric-nan')).toHaveClass('invalid');
Expand Down

0 comments on commit 839aacd

Please sign in to comment.