Commit 6009912c authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Evita asignar valor por defecto a props anulables

Testea casos de uso en los que un esquema provee valor por defecto para
propiedades de tipo object u otro simple, pero que también pueden ser
nulo. En estos casos, se espera que no se realice la asignación.
parent 9229119e
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -101,6 +101,11 @@ define([
			this._schema = schema;
			this._build();

			var propertyType = this._schema.type;
			if (propertyType instanceof Array && propertyType.indexOf('null') !== -1) {
				return;
			}

			this._setDefaultValue();
		},

+17 −0
Original line number Diff line number Diff line
@@ -687,6 +687,23 @@ define([
				assert.strictEqual(attr.get("value"), null,
					"El default se ha añadido al value siendo incorrecto para este tipo");
			}));
		},

		'Should_OmitDefaultValue_When_PropertyIsNullable': function() {

			var dfd = this.async(timeout);

			schema = {
				type: ['number', 'null'],
				'default': 1
			};

			attr = new Attr();
			attr.build(schema).then(dfd.callback(function() {

				assert.strictEqual(attr.get('value'), null,
					'El valor por defecto se ha asignado aunque la propiedad era anulable');
			}));
		}
	});
});
+40 −2
Original line number Diff line number Diff line
@@ -1231,7 +1231,7 @@ define([
						}
					},
					name: {
						type: ["string", "null"],
						type: "string",
						"default": "hola"
					}
				}
@@ -1544,4 +1544,42 @@ define([
	};

	registerSuite("ObjAttr with default value tests", specificTests);

	specificProps = {
		before: function() {

			var dfd = this.async(timeout);

			schema = {
				type: ['object', 'null'],
				properties: {
					name: {
						type: 'string'
					}
				},
				'default': '{"name": "pepito"}'
			};

			objAttr = new ObjAttr();

			objAttr.build(schema).then(dfd.callback(function() {}));
		},

		beforeEach: function() {

			objAttr.reset();
		}
	};

	specificTests = {
		'Should_OmitDefaultValue_When_PropertyIsNullable': function() {

			var serialized = objAttr.serialize();

			assert.deepEqual(serialized, null, 'El valor por defecto se ha asignado aunque la propiedad era anulable');
		}
	};

	specificProps.tests = specificTests;
	registerSuite('ObjAttr with null type and default value', specificProps);
});