2022-01-13 13:00:31 +00:00
|
|
|
function moduleTests(ui) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let
|
|
|
|
module = ui.module,
|
|
|
|
element = ui.element,
|
|
|
|
singleton = ui.singleton,
|
|
|
|
name = $.fn[module].settings.name,
|
|
|
|
|
|
|
|
testValue = 'Test',
|
|
|
|
fixtures = jasmine.getFixtures(), // eslint-disable-line jest/no-jasmine-globals
|
|
|
|
|
|
|
|
originalSettings,
|
|
|
|
$modules,
|
|
|
|
$oneModule,
|
|
|
|
$module,
|
|
|
|
$clone
|
|
|
|
;
|
|
|
|
|
|
|
|
// set fixture path
|
|
|
|
fixtures.fixturesPath = 'base/test/fixtures/';
|
|
|
|
|
|
|
|
// disable debug
|
|
|
|
$.fn[module].settings.debug = false;
|
|
|
|
$.fn[module].settings.performance = false;
|
|
|
|
$.fn[module].settings.verbose = false;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// load fixtures
|
|
|
|
fixtures.load(module + '.html');
|
|
|
|
// save settings
|
|
|
|
originalSettings = $.fn[module].settings;
|
|
|
|
|
|
|
|
// module available in scope
|
|
|
|
$module = $(element);
|
|
|
|
|
|
|
|
if ($module.size() === 1) { // one module available in fixture
|
|
|
|
$oneModule = $module;
|
|
|
|
$clone = $module.clone().appendTo($(sandbox()));
|
|
|
|
$modules = $clone.add($module);
|
|
|
|
} else { // multiple modules available in fixture
|
|
|
|
$modules = $(element);
|
|
|
|
$clone = $module.eq(1);
|
|
|
|
$oneModule = $modules.first();
|
|
|
|
}
|
2022-01-13 13:00:31 +00:00
|
|
|
});
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
afterEach(function () {
|
|
|
|
// restore settings
|
|
|
|
$.fn[module].settings = originalSettings;
|
|
|
|
// remove element
|
|
|
|
$(element).remove();
|
2022-01-13 13:00:31 +00:00
|
|
|
});
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/*******************************
|
|
|
|
Module
|
|
|
|
*******************************/
|
|
|
|
|
|
|
|
/* -------------------
|
|
|
|
Instantiation
|
|
|
|
-------------------- */
|
|
|
|
describe('Module', function () {
|
|
|
|
it('allows chaining when no settings returned', function () {
|
|
|
|
let $chain = $modules[module]();
|
|
|
|
expect($chain).toExist();
|
|
|
|
expect($chain.size()).toBe($modules.size());
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('returns a string when one setting returned', function () {
|
|
|
|
let result = $oneModule[module]('setting', 'name');
|
|
|
|
expect(typeof result).toBe('string');
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('returns an array when multiple settings returned', function () {
|
|
|
|
let result = $modules[module]('setting', 'name');
|
|
|
|
expect($.isArray(result)).toBeTruthy();
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('has an instance in metadata after init', function () {
|
|
|
|
$oneModule[module]();
|
|
|
|
expect($module).toHaveData('module-' + module);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
});
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/* -------------------
|
|
|
|
Settings
|
|
|
|
-------------------- */
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
describe('Settings', function () {
|
|
|
|
it('clears settings on re-init', function () {
|
|
|
|
$oneModule[module]({
|
|
|
|
name: testValue,
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
expect(retrievedValue).toBe(testValue);
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// reinit
|
|
|
|
$oneModule[module]();
|
|
|
|
retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
expect(retrievedValue).toBe(name);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('allows default settings to be changed', function () {
|
|
|
|
$.fn[module].settings.name = testValue;
|
|
|
|
$oneModule[module]();
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
$.fn[module].settings.name = name;
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('allows settings to be changed during init', function () {
|
|
|
|
$oneModule[module]({
|
|
|
|
name: testValue,
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('allows settings to be changed during runtime', function () {
|
|
|
|
$oneModule[module]();
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(name);
|
2022-01-13 13:00:31 +00:00
|
|
|
});
|
2023-08-17 09:47:40 +00:00
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/* -------------------
|
|
|
|
Groups
|
|
|
|
-------------------- */
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!singleton) {
|
|
|
|
describe('Group Contamination', function () {
|
|
|
|
it('creates settings for all instances', function () {
|
|
|
|
$modules[module]('setting', 'name', testValue);
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
let clonedSetting = $clone[module]('setting', 'name');
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
expect(clonedSetting).toBe(testValue);
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
$oneModule[module]({
|
|
|
|
name: testValue,
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
expect(clonedSetting).toBe(testValue);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('does not change other elements settings when changing one element', function () {
|
|
|
|
$modules[module]();
|
|
|
|
$oneModule[module]('setting', 'name', testValue);
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
let clonedSetting = $clone[module]('setting', 'name');
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
expect(clonedSetting).toBe(name);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('does not change other elements when re-initialized', function () {
|
|
|
|
$modules[module]();
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
$oneModule[module]({
|
|
|
|
name: testValue,
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let retrievedValue = $oneModule[module]('setting', 'name');
|
|
|
|
let clonedSetting = $clone[module]('setting', 'name');
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
expect(retrievedValue).toBe(testValue);
|
|
|
|
expect(clonedSetting).toBe(name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/* -------------------
|
|
|
|
Destroy
|
|
|
|
-------------------- */
|
|
|
|
describe('Destroy', function () {
|
|
|
|
it('removes all events from page', function () {
|
|
|
|
$module[module]('destroy');
|
|
|
|
if ($.events().length > 0) {
|
|
|
|
dump($.events());
|
|
|
|
}
|
|
|
|
expect($.events().length).toBe(0);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
it('removes instance metadata', function () {
|
|
|
|
$module[module]('destroy');
|
|
|
|
expect($module.data('module-' + module)).toBe(undefined);
|
|
|
|
});
|
2022-01-13 13:00:31 +00:00
|
|
|
});
|
2022-06-08 10:36:39 +00:00
|
|
|
}
|