Ejemplo n.º 1
0
QUnit.test("nullish values are not converted for type or Type", function(assert) {

	var Foo = function() {};

	var MyMap = define.Constructor({
		map: {
			Type: Foo
		},
		notype: {}
	});

	var vm = new MyMap({
		map: {},
		notype: {}
	});

	// Sanity check
	assert.ok(vm.map instanceof Foo, "map is another type");
	assert.ok(vm.notype instanceof Object, "notype is an Object");

	vm.map = null;
	vm.notype = null;

	assert.equal(vm.map, null, "map is null");
	assert.equal(vm.map, null, "notype is null");
});
Ejemplo n.º 2
0
QUnit.test('Compute type property with compute default value triggers change events when updated', function() {
	var expected = 0;
	var c = compute(0);

	var MyMap = define.Constructor({
		computeProp: {
			type: 'compute',
			value: function() {
				return c;
			}
		}
	});

	var m = new MyMap();

	c.bind('change', function(ev, newVal) {
		equal(newVal, expected, 'Compute fired change event');
	});

	m.on('computeProp', function(ev, newVal) {
		equal(newVal, expected, 'Map fired change event');
	});

	expected = 1;
	m.computeProp = expected;

	expected = 2;
	c(expected);
});
Ejemplo n.º 3
0
QUnit.test('Extensions can modify definitions', function() {
	var oldExtensions = define.extensions;

	define.behaviors.push('extended');

	define.extensions = function(objPrototype, prop, definition) {
		if (definition.extended) {
			return {
				value: 'extended'
			};
		}
	};

	var MyMap = define.Constructor({
		foo: {
			value: 'defined',
			extended: true,
		},
		bar: {
			value: 'defined'
		}
	});

	var map = new MyMap();

	QUnit.equal(map.foo, 'extended', 'Value was set via extension');
	QUnit.equal(map.bar, 'defined', 'Value was set via definition');

	define.extensions = oldExtensions;
});
Ejemplo n.º 4
0
test('value and get (#1521)', function() {
	// problem here is that previously, can.Map would set `size:1` on
	// the map. This would effectively set the "lastSetValue".

	// in this new version, default values are not set.  They
	// are only present. later.
	// one option is that there's a "read-mode" for last-set.  Until it's
	// been set, it should get it's value from any default value?

	var MyMap = define.Constructor({
		data: {
			value: function() {
				return new CanList(['test']);
			}
		},
		size: {
			value: 1,
			get: function(val) {
				var list = this.data;
				var length = list.attr('length');
				return val + length;
			}
		}
	});

	var map = new MyMap({});
	equal(map.size, 2);
});
Ejemplo n.º 5
0
test('Can bind to a defined property with a set/get method (#1648)', 3, function() {
	// Problem: "get" is not called before and after the "set"
	// Problem: Function bound to "foo" is not called
	// Problem: Cannot read the value of "foo"

	var Map = define.Constructor({
		foo: {
			value: '',
			set: function(setVal) {
				return setVal;
			},
			get: function(lastSetVal) {
				return lastSetVal;
			}
		}
	});

	var map = new Map();

	map.bind('foo', function() {
		ok(true, 'Bound function is called');
	});

	equal(map.foo, '', 'Calling .attr(\'foo\') returned the correct value');

	map.foo = 'baz';

	equal(map.foo, 'baz', 'Calling .attr(\'foo\') returned the correct value');
});
Ejemplo n.º 6
0
test('default behaviors with "*" work for attributes', function() {
	expect(5);
	var DefaultMap = define.Constructor({
		'*': {
			type: 'number',
			set: function(newVal) {
				ok(true, 'set called');
				return newVal;
			}
		},
		someNumber: {
			value: '5'
		},
		number: {}
	});

	var map = new DefaultMap();

	equal(map.someNumber, '5', 'default values are not type converted anymore');
	map.someNumber = '5';
	equal(map.someNumber, 5, 'on a set, they should be type converted');

	map.number = '10'; // Custom set should be called
	equal(map.number, 10, 'value of number should be converted to a number');

});
Ejemplo n.º 7
0
test("nested define", function() {
	var nailedIt = 'Nailed it';

	var Example = define.Constructor({
		name: {
			value: nailedIt
		}
	});

	var NestedMap = define.Constructor({
		isEnabled: {
			value: true
		},
		test: {
			Value: Example
		},
		examples: {
			type: {
				one: {
					Value: Example
				},
				two: {
					type: {
						deep: {
							Value: Example
						}
					},
					Value: Object
				}
			},
			Value: Object
		}
	});

	var nested = new NestedMap();

	// values are correct
	equal(nested.test.name, nailedIt);
	equal(nested.examples.one.name, nailedIt);
	equal(nested.examples.two.deep.name, nailedIt);

	// objects are correctly instanced
	ok(nested.test instanceof Example);
	ok(nested.examples.one instanceof Example);
	ok(nested.examples.two.deep instanceof Example);
});
Ejemplo n.º 8
0
test("One event on getters (#1585)", function() {
	var Person = define.Constructor({
		name: "*",
		id: "number"
	});

	var AppState = define.Constructor({
		person: {
			get: function(lastSetValue, resolve) {
				if (lastSetValue) {
					return lastSetValue;
				} else if (this.personId) {
					resolve(new Person({
						name: "Jose",
						id: 5
					}));
				} else {
					return null;
				}
			},
			Type: Person
		},
		personId: "*"
	});

	var appState = new AppState();
	var personEvents = 0;
	appState.bind("person", function(ev, person) {
		personEvents++;
	});

	equal(appState.person, null, "no personId and no lastSetValue");

	appState.personId = 5;
	equal(appState.person.name, "Jose", "a personId, providing Jose");
	ok(appState.person instanceof Person, "got a person instance");

	appState.person = {
		name: "Julia"
	};
	ok(appState.person instanceof Person, "got a person instance");

	equal(personEvents, 2);
});
Ejemplo n.º 9
0
QUnit.test("expandos are added in define.setup (#25)", function() {
	var MyMap = define.Constructor({});

	var map = new MyMap({
		prop: 4
	});
	map.on("prop", function() {
		QUnit.ok(true, "prop event called");
	});
	map.prop = 5;
});
Ejemplo n.º 10
0
test("stache with double property", function() {
	var nailedIt = 'Nailed it';
	var Example = define.Constructor({
		name: {
			value: nailedIt
		}
	});

	var NestedMap = define.Constructor({
		isEnabled: {
			value: true
		},
		test: {
			Value: Example
		},
		examples: {
			type: {
				one: {
					Value: Example
				},
				two: {
					type: {
						deep: {
							Value: Example
						}
					},
					Value: Object
				}
			},
			Value: Object
		}
	});

	var nested = new NestedMap();
	var template = stache('{{test.name}}');
	var frag = template(nested);
	equal(frag.firstChild.nodeValue, nailedIt);
});
Ejemplo n.º 11
0
test("Stache with boolean property with {{#if}}", function() {
	var nailedIt = 'Nailed it';
	var Example = define.Constructor({
		name: {
			value: nailedIt
		}
	});

	var NestedMap = define.Constructor({
		isEnabled: {
			value: true
		},
		test: {
			Value: Example
		},
		examples: {
			type: {
				one: {
					Value: Example
				},
				two: {
					type: {
						deep: {
							Value: Example
						}
					},
					Value: Object
				}
			},
			Value: Object
		}
	});

	var nested = new NestedMap();
	var template = stache('{{#if isEnabled}}Enabled{{/if}}');
	var frag = template(nested);
	equal(frag.firstChild.nodeValue, 'Enabled');
});
Ejemplo n.º 12
0
QUnit.test('Set property with type compute', function() {
	var MyMap = define.Constructor({
		computeProp: {
			type: 'compute'
		}
	});

	var m = new MyMap();

	m.computeProp = compute(0);
	equal(m.computeProp, 0, 'Property has correct value');

	m.computeProp = compute(1);
	equal(m.computeProp, 1, 'Property has correct value');
});
Ejemplo n.º 13
0
test("Stache with single property", function() {
	var Typer = define.Constructor({
		foo: {
			type: 'string'
		}
	});

	var template = stache('{{foo}}');
	var t = new Typer({
		foo: 'bar'
	});
	var frag = template(t);
	equal(frag.firstChild.nodeValue, 'bar');
	t.foo = "baz";
	equal(frag.firstChild.nodeValue, 'baz');
});
Ejemplo n.º 14
0
test('Initial value does not call getter', function() {
	expect(0);

	var Map = define.Constructor({
		count: {
			get: function(lastVal) {
				ok(false, 'Should not be called');
				return lastVal;
			}
		}
	});

	new Map({
		count: 100
	});
});
Ejemplo n.º 15
0
QUnit.test('Compute type property can have a default value', function() {
	var MyMap = define.Constructor({
		computeProp: {
			type: 'compute',
			value: function() {
				return 0;
			}
		}
	});

	var m = new MyMap();
	equal(m.computeProp, 0, 'Property has correct value');

	m.computeProp = 1;
	equal(m.computeProp, 1, 'Property has correct value');
});
Ejemplo n.º 16
0
	QUnit.test("logs work with maps", function() {
		var MyMap = define.Constructor({
			first: "string",
			last: "string"
		});
		var m = new MyMap({
			first: "J",
			last: "M"
		});
		var fullName = compute(function() {
			return m.first + m.last;
		});

		fullName.on("change", function() {});

		var t = fullName.computeInstance.trace();
		QUnit.equal(t.dependencies[0].obj, m);
		QUnit.equal(t.dependencies[1].obj, m);
	});
Ejemplo n.º 17
0
QUnit.test('Works with can-define', function () {

	var VM = define.Constructor({
		firstName: {
			type: 'string'
		},
		lastName: {
			type: 'string'
		},
		fullName: {
			get: function () {
				return [this.firstName, this.lastName].join(' ');
			}
		}
	});

	Component.extend({
		tag: 'can-define-component',
		ViewModel: VM,
		view: stache('Name: {{fullName}}')
	});

	var frag = stache('<can-define-component firstName:from="firstName" lastName:from="lastName" />')({
		firstName: 'Chris',
		lastName: 'Gomez'
	});

	var vm = viewModel(frag.firstChild);

	QUnit.ok(vm instanceof VM, 'Constructor was called');
	QUnit.equal(vm.firstName, 'Chris', 'ViewModel was set from scope');
	QUnit.equal(vm.lastName, 'Gomez', 'ViewModel was set from scope');
	QUnit.equal(frag.firstChild.innerHTML, 'Name: Chris Gomez', 'Rendered fullName');

	vm.firstName = 'Justin';
	vm.lastName = 'Meyer';

	QUnit.equal(frag.firstChild.innerHTML, 'Name: Justin Meyer', 'Rendered fullName after change');
});
Ejemplo n.º 18
0
test("Asynchronous virtual properties cause extra recomputes (#1915)", function() {

	stop();

	var ran = false;

	var VM = define.Constructor({
		foo: {
			get: function(lastVal, setVal) {
				setTimeout(function() {
					if (setVal) {
						setVal(5);
					}
				}, 10);
			}
		},
		bar: {
			get: function() {
				var foo = this.foo;
				if (foo) {
					if (ran) {
						ok(false, 'Getter ran twice');
					}
					ran = true;
					return foo * 2;
				}
			}
		}
	});

	var vm = new VM();
	vm.bind('bar', function() {});

	setTimeout(function() {
		equal(vm.bar, 10);
		start();
	}, 200);

});
Ejemplo n.º 19
0
test("getters produce change events", function() {
	var Map = define.Constructor({
		count: {
			get: function(lastVal) {
				return lastVal;
			}
		}

	});

	var map = new Map();

	// map.bind("change", function(){
	//   ok(true, "change called");
	// });

	map.bind('count', function() {
		ok(true, "change called");
	});

	map.count = 22;
});
Ejemplo n.º 20
0
test("getter with initial value", function() {

	var comp = compute(1);

	var Grabber = define.Constructor({
		vals: {
			type: "*",
			Value: Array,
			get: function(current, setVal) {
				if (setVal) {
					current.push(comp());
				}
				return current;
			}
		}
	});

	var g = new Grabber();
	// This assertion doesn't mean much.  It's mostly testing
	// that there were no errors.
	equal(g.vals.length, 0, "zero items in array");

});
Ejemplo n.º 21
0
test('Can read a defined property with a set/get method (#1648)', function() {
	// Problem: "get" is not passed the correct "lastSetVal"
	// Problem: Cannot read the value of "foo"

	var Map = define.Constructor({
		foo: {
			value: '',
			set: function(setVal) {
				return setVal;
			},
			get: function(lastSetVal) {
				return lastSetVal;
			}
		}
	});

	var map = new Map();

	equal(map.foo, '', 'Calling .foo returned the correct value');

	map.foo = 'baz';

	equal(map.foo, 'baz', 'Calling .foo returned the correct value');
});
Ejemplo n.º 22
0
test("getter and setter work", function() {
	expect(5);

	var Paginate = define.Constructor({
		limit: "*",
		offset: "*",
		page: {
			set: function(newVal) {
				this.offset = (parseInt(newVal) - 1) * this.limit;
			},
			get: function() {
				return Math.floor(this.offset / this.limit) + 1;
			}
		}
	});

	var p = new Paginate({
		limit: 10,
		offset: 20
	});

	equal(p.page, 3, "page get right");

	p.bind("page", function(ev, newValue, oldValue) {
		equal(newValue, 2, "got new value event");
		equal(oldValue, 3, "got old value event");
	});


	p.page = 2;

	equal(p.page, 2, "page set right");

	equal(p.offset, 10, "page offset set");

});
Ejemplo n.º 23
0
test("type converters handle null and undefined in expected ways (1693)", function() {

	var Typer = define.Constructor({
		date: {
			type: 'date'
		},
		string: {
			type: 'string'
		},
		number: {
			type: 'number'
		},
		'boolean': {
			type: 'boolean'
		},
		htmlbool: {
			type: 'htmlbool'
		},
		leaveAlone: {
			type: '*'
		}
	});

	var t = new Typer({
		date: undefined,
		string: undefined,
		number: undefined,
		'boolean': undefined,
		htmlbool: undefined,
		leaveAlone: undefined
	});

	equal(t.date, undefined, "converted to date");

	equal(t.string, undefined, "converted to string");

	equal(t.number, undefined, "converted to number");

	equal(t.boolean, undefined, "converted to boolean"); //Updated for canjs#2316

	equal(t.htmlbool, false, "converted to htmlbool");

	equal(t.leaveAlone, undefined, "left as object");

	t = new Typer({
		date: null,
		string: null,
		number: null,
		'boolean': null,
		htmlbool: null,
		leaveAlone: null
	});

	equal(t.date, null, "converted to date");

	equal(t.string, null, "converted to string");

	equal(t.number, null, "converted to number");

	equal(t.boolean, null, "converted to boolean"); //Updated for canjs#2316

	equal(t.htmlbool, false, "converted to htmlbool");

	equal(t.leaveAlone, null, "left as object");

});
Ejemplo n.º 24
0
test('Can make an attr alias a compute (#1470)', 9, function() {
	var computeValue = compute(1);

	var GetMap = define.Constructor({
		value: {
			set: function(newValue, setVal, oldValue) {
				//debugger;
				if (newValue.isComputed) {
					return newValue;
				}
				if (oldValue && oldValue.isComputed) {
					oldValue(newValue);
					return oldValue;
				}
				return newValue;
			},
			get: function(value) {
				return value && value.isComputed ? value() : value;
			}
		}
	});

	var getMap = new GetMap();

	getMap.value = computeValue;

	equal(getMap.value, 1, "initial value read from compute");

	var bindCallbacks = 0;

	getMap.bind("value", function(ev, newVal, oldVal) {

		switch (bindCallbacks) {
			case 0:
				equal(newVal, 2, "0 - bind called with new val");
				equal(oldVal, 1, "0 - bind called with old val");
				break;
			case 1:
				equal(newVal, 3, "1 - bind called with new val");
				equal(oldVal, 2, "1 - bind called with old val");
				break;
			case 2:
				equal(newVal, 4, "2 - bind called with new val");
				equal(oldVal, 3, "2 - bind called with old val");
				break;
		}


		bindCallbacks++;
	});

	// Try updating the compute's value
	computeValue(2);

	// Try setting the value of the property
	getMap.value = 3;

	equal(getMap.value, 3, "read value is 3");
	equal(computeValue(), 3, "the compute value is 3");

	// Try setting to a new comptue
	var newComputeValue = compute(4);

	getMap.value = newComputeValue;

});
Ejemplo n.º 25
0
test("Value generator can read other properties", function() {
	var Map = define.Constructor({
		letters: {
			value: "ABC"
		},
		numbers: {
			value: [1, 2, 3]
		},
		definedLetters: {
			value: 'DEF'
		},
		definedNumbers: {
			value: [4, 5, 6]
		},
		generatedLetters: {
			value: function() {
				return 'GHI';
			}
		},
		generatedNumbers: {
			value: function() {
				return new CanList([7, 8, 9]);
			}
		},

		// Get prototype defaults
		firstLetter: {
			value: function() {
				return this.letters.substr(0, 1);
			}
		},
		firstNumber: {
			value: function() {
				return this.numbers[0];
			}
		},

		// Get defined simple `value` defaults
		middleLetter: {
			value: function() {
				return this.definedLetters.substr(1, 1);
			}
		},
		middleNumber: {
			value: function() {
				return this.definedNumbers[1];
			}
		},

		// Get defined `value` function defaults
		lastLetter: {
			value: function() {
				return this.generatedLetters.substr(2, 1);
			}
		},
		lastNumber: {
			value: function() {
				return this.generatedNumbers[2];
			}
		}
	});

	var map = new Map();
	var prefix = 'Was able to read dependent value from ';

	equal(map.firstLetter, 'A',
		prefix + 'traditional can.Map style property definition');
	equal(map.firstNumber, 1,
		prefix + 'traditional can.Map style property definition');

	equal(map.middleLetter, 'E',
		prefix + 'define plugin style default property definition');
	equal(map.middleNumber, 5,
		prefix + 'define plugin style default property definition');

	equal(map.lastLetter, 'I',
		prefix + 'define plugin style generated default property definition');
	equal(map.lastNumber, 9,
		prefix + 'define plugin style generated default property definition');
});