Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/js_tests/SelectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@

});

it("should handle string entries", function () {
var element = new StyledElements.Select();

element.addEntries(["a", "b", "c"]);
expect(element.value).toBe("a");
expect(element.getLabel()).toBe("a");
});

it("should handle number entries", function () {
var element = new StyledElements.Select();

element.addEntries([1, 2, 3]);
expect(element.value).toBe(1);
expect(element.getLabel()).toBe("1");
});

it("should support changing the value using the setValue method", function () {

var element = new StyledElements.Select({
Expand Down
126 changes: 126 additions & 0 deletions src/js_tests/UtilsSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2016 CoNWeT Lab., Universidad Politécnica de Madrid
*
* This file is part of Wirecloud Platform.
*
* Wirecloud Platform is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Wirecloud is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Wirecloud Platform. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

/* jshint jasmine:true */
/* globals StyledElements */

(function () {

"use strict";

describe("Styled Element Utils - Object helpers", function () {

describe("isEmpty(value)", function () {
var isEmpty;

beforeAll(function () {
isEmpty = StyledElements.Utils.isEmpty;
});

it("returns true if value is null or undefined", function () {
expect(isEmpty(null)).toBeTruthy();
expect(isEmpty()).toBeTruthy();
});

it("returns true if value is boolean", function () {
expect(isEmpty(new Boolean(true))).toBeTruthy();
expect(isEmpty(new Boolean(false))).toBeTruthy();
});

it("returns true if value is number", function () {
expect(isEmpty(new Number(-1))).toBeTruthy();
expect(isEmpty(new Number(0))).toBeTruthy();
expect(isEmpty(new Number(1))).toBeTruthy();
});

it("returns true if value is string and string.length is 0", function () {
expect(isEmpty(new String(""))).toBeTruthy();
});

it("returns false if value is string and string.length is greater than 0", function () {
expect(isEmpty(new String("hello world"))).toBeFalsy();
});

it("returns true if value is array and array.length is 0", function () {
expect(isEmpty([])).toBeTruthy();
});

it("returns false if value is array and array.length is greater than 0", function () {
expect(isEmpty([1, 2, 3])).toBeFalsy();
});

it("returns true if value is object and object.keys.length is 0", function () {
expect(isEmpty({})).toBeTruthy();
});

it("returns false if value is object and object.keys.length is greater than 0", function () {
expect(isEmpty({a: 1, b: 2})).toBeFalsy();
});
});

describe("merge(object, ...sources)", function () {
var merge;

beforeAll(function () {
merge = StyledElements.Utils.merge;
});

it("throws exception if object is null or undefined", function () {
expect(function () {
return merge();
}).toThrowError(TypeError, "The argument `object` must be an `Object`.");
expect(function () {
return merge(null);
}).toThrowError(TypeError, "The argument `object` must be an `Object`.");
});

it("should merge sources into object", function () {
var src = {};
var defaults = {
depth: 0,
state: "default",
events: ["click", "focus"]
};
var options = {
state: "primary",
events: ["mouseover"]
};
expect(merge(src, defaults, options)).toBe(src);
expect(src.depth).toBe(0);
expect(src.state).toBe("primary");
expect(src.events).toEqual(["mouseover"]);
});
});

describe("values(object)", function () {
var values;

beforeAll(function () {
values = StyledElements.Utils.values;
});

it("returns array of object property values", function () {
expect(values({one: 1, two: 2, tree: 3})).toEqual([1, 2, 3]);
});
});
});

})();
9 changes: 5 additions & 4 deletions src/wirecloud/commons/static/js/StyledElements/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@
for (var i = 0; i < newEntries.length; i++) {
newEntry = newEntries[i];
var option = document.createElement("option");
if ('value' in newEntry) {
optionValue = newEntry.value;
optionLabel = newEntry.label;
} else if (Array.isArray(newEntry)) {

if (Array.isArray(newEntry)) {
optionValue = newEntry[0];
optionLabel = newEntry[1];
} else if (newEntry.value != null) {
optionValue = newEntry.value;
optionLabel = newEntry.label;
} else {
optionValue = newEntry;
optionLabel = newEntry;
Expand Down
155 changes: 112 additions & 43 deletions src/wirecloud/commons/static/js/StyledElements/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,6 @@ if (window.StyledElements == null) {
};
}

Utils.values = function values(object) {
var result = [];

for (var key in object) {
result.push(object[key]);
}

return result;
};

/**
* @experimental
*/
Expand Down Expand Up @@ -611,15 +601,6 @@ if (window.StyledElements == null) {
return sourceValue;
};

Utils.isEmptyObject = function isEmptyObject(obj) {
var name;

for (name in obj) {
return false;
}
return true;
};

// ==================================================================================
// DOCUMENT - HELPERS
// ==================================================================================
Expand All @@ -636,30 +617,6 @@ if (window.StyledElements == null) {
return document.activeElement === element;
};

/**
* Permite obtener un objeto a partir de la mezcla de los atributos de dos
* objetos. Para ello, se pasarán los dos objetos que se usarán de fuente,
* siendo el primero de los objetos sobreescrito con el resultado. En caso de
* que exista un mismo atributo en los dos objetos, el valor final será el del
* segundo objeto, perdiendose el valor del primer objeto.
*
* @param {Object} obj1 objeto base.
* @param {Object} obj2 objeto modificador. En caso de que este argumento sea
* null, esta función no hará nada.
*
* @return obj1 modificado
*/
Utils.merge = function merge(obj1, obj2) {
if (obj2 != null) {

for (var key in obj2) {
obj1[key] = obj2[key];
}
}

return obj1;
};

/**
* [updateObject description]
*
Expand Down Expand Up @@ -1103,6 +1060,118 @@ if (window.StyledElements == null) {
return (value instanceof StyledElements.StyledElement && value.get() == null) ? value.children : [value];
};

// ==================================================================================
// OBJECT HELPERS
// ==================================================================================

/**
* Checks if `value` is an empty object. Objects are considered empty if they have no
* own enumerable properties. Arrays or strings are considered empty if they have a
* `length` of `0`.
* @since 0.5
*
* @param {*} value Reference to check.
* @returns {Boolean} True if `value` is empty.
*
* @example
*
* isEmpty(null);
* => true
*
* isEmpty(true);
* => true
*
* isEmpty(1);
* => true
*
* isEmpty("hello world");
* => false
*
* isEmpty([1, 2, 3]);
* => false
*
* isEmpty({'a': 1});
* => false
*/
Utils.isEmpty = function isEmpty(value) {
return value == null || Object.keys(value).length === 0;
};

/**
* Merges own enumerable properties of source objects `sources` into the destination
* object `object`. Source objects are applied from left to right. Source objects
* that resolve to `undefined` or `null` are skipped. Subsequent sources overwrite
* property assignments of previous sources.
* @since 0.5
*
* @param {Object} object The destination object.
* @param {...Object} [sources] The source object(s).
* @returns {Object} The reference to `object`.
*
* @example
*
* var defaults = {
* depth: 0,
* state: "default",
* events: ["click", "focus"]
* };
*
* var options = {
* state: "primary",
* events: ["mouseover"]
* };
*
* merge({}, defaults, options);
* => {depth: 0, state: "primary", events: ["mouseover"]}
*
* defaults;
* => {depth: 0, state: "default", events: ["click", "focus"]}
*
* merge(defaults, options);
* => {depth: 0, state: "primary", events: ["mouseover"]}
*
* defaults;
* => {depth: 0, state: "primary", events: ["mouseover"]}
*/
Utils.merge = function merge(object) {

if (object == null) {
throw new TypeError("The argument `object` must be an `Object`.");
}

Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (source != null) {
Object.keys(source).forEach(function (key) {
object[key] = source[key];
});
}
});

return object;
};

/**
* Creates an array of the `object` own enumerable property values.
* @since 0.5
*
* @param {Object} object The object to query.
* @returns {Array} The array of property values.
*
* @example
*
* values({one: 1, two: 2, three: 3});
* => [1, 2, 3]
*/
Utils.values = function values(object) {
var values = [];

for (var key in object) {
values.push(object[key]);
}

return values;
};

StyledElements.Utils = Utils;

})();
Loading