QUnit.test("attrs will use can.callbackMap symbol if available.", function(){
	var doc = globals.getKeyValue('document');
	var map = new Map();
	map.set("foo2", function(el, attrData) {
		el.appendChild(doc.createTextNode("foo"));
	});
	map.set(/bar2/, function(el, attrData) {
		el.appendChild(doc.createTextNode("bar"));
	});

	var bindings = {
		bindings: map
	};
	bindings[canSymbol.for("can.callbackMap")] = map;
	callbacks.attrs(bindings);

	var el1 = doc.createElement("span");
	el1.setAttribute("foo2", "");

	var el2 = doc.createElement("span");
	el2.setAttribute("bar2", "");


	callbacks.attr("foo2")(el1);
	callbacks.attr("bar2")(el2);

	QUnit.equal(el1.firstChild.nodeValue, "foo");
	QUnit.equal(el2.firstChild.nodeValue, "bar");
});
 contributionMonths.on("length", function(ev, newLength){
   clearTimeout(timeout);
   QUnit.equal(newLength, 4, "got an item");
   QUnit.equal(moment(contributionMonths[3].date).toDate().getTime(),
       moment().startOf('month').toDate().getTime(), "created with the right date");
   QUnit.start();
 });
Example #3
0
QUnit.test("Click Radio Buttons", function () {

	var radio1 = 0,
		radio2 = 0;

	st.g("radio1")
		.checked = false;
	//make sure changes are called
	st.bind(st.g("radio1"), "change", function (ev) {
		radio1++;
	});
	st.bind(st.g("radio2"), "change", function (ev) {
		radio2++;
	});

	syn.trigger(st.g("radio1"), "click", {});

	QUnit.equal(radio1, 1, "radio event");
	QUnit.ok(st.g("radio1")
		.checked, "radio checked");

	syn.trigger(st.g("radio2"), "click", {});

	QUnit.equal(radio2, 1, "radio event");
	QUnit.ok(st.g("radio2")
		.checked, "radio checked");

	QUnit.ok(!st.g("radio1")
		.checked, "radio unchecked");

});
Example #4
0
QUnit.test("Click Forms", function () {
	var submit = 0,
		submitf = function (ev) {
			submit++;
			if (ev.preventDefault) {
				ev.preventDefault();
			}
			ev.returnValue = false;
			return false;
		};
	st.bind(st.g("outer"), "submit", submitf);
	syn.trigger(st.g("submit"), "click", {});
	syn("submit", "outer", {});

	QUnit.equal(submit, 2, "Click on submit");

	//make sure clicking the div does not submit the form
	var click = 0,
		clickf = function (ev) {
			click++;
			if (ev.preventDefault) {
				ev.preventDefault();
			}
			return false;
		};
	st.binder("inner", "click", clickf);

	syn.trigger(st.g("submit"), "click", {});

	QUnit.equal(submit, 2, "Submit prevented");
	QUnit.equal(click, 1, "Clicked");

	st.unbinder("outer", "submit", submitf);
	st.unbinder("inner", "click", clickf);
});
Example #5
0
QUnit.test("type converters", function() {

	var Typer = function(date, string, number, bool, htmlbool, leaveAlone) {
		this.date = date;
		this.string = string;
		this.number = number;
		this.bool = bool;
		this.htmlbool = htmlbool;
		this.leaveAlone = leaveAlone;
	};

	define(Typer.prototype, {
		date: {
			type: 'date'
		},
		string: {
			type: 'string'
		},
		number: {
			type: 'number'
		},
		bool: {
			type: 'boolean'
		},
		htmlbool: {
			type: 'htmlbool'
		},
		leaveAlone: {
			type: '*'
		},
	});

	var obj = {};

	var t = new Typer(
		1395896701516,
		5,
		'5',
		'false',
		"",
		obj
	);

	QUnit.ok(t.date instanceof Date, "converted to date");

	QUnit.equal(t.string, '5', "converted to string");

	QUnit.equal(t.number, 5, "converted to number");

	QUnit.equal(t.bool, false, "converted to boolean");

	QUnit.equal(t.htmlbool, true, "converted to htmlbool");

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

	t.number = '15';

	QUnit.ok(t.number === 15, "converted to number");

});
test("works with can-reflect", 8, function(){
	var b = new SimpleMap({ "foo": "bar" });
	// var c = new (SimpleMap.extend({
	// 	"baz": canCompute(function(){
	// 		return b.attr("foo");
	// 	})
	// }))({ "foo": "bar", thud: "baz" });

	QUnit.equal( canReflect.getKeyValue(b, "foo"), "bar", "get value");

	var handler = function(newValue){
		QUnit.equal(newValue, "quux", "observed new value");

		// Turn off the "foo" handler but "thud" should still be bound.
		canReflect.offKeyValue(b, "foo", handler);
	};
	QUnit.ok(!canReflect.isValueLike(b), "isValueLike is false");
	QUnit.ok(canReflect.isMapLike(b), "isMapLike is true");
	QUnit.ok(!canReflect.isListLike(b), "isListLike is false");

	QUnit.ok( !canReflect.keyHasDependencies(b, "foo"), "keyHasDependencies -- false");

	canReflect.onKeyValue(b, "foo", handler);
	// Do a second binding to check that you can unbind correctly.
	canReflect.onKeyValue(b, "baz", handler);

	b.attr("foo", "quux");

	QUnit.equal( canReflect.getKeyValue(b, "foo"), "quux", "bound value");
	// sanity checks to ensure that handler doesn't get called again.
	b.attr("foo", "thud");
	b.attr("baz", "quux");

});
Example #7
0
QUnit.test("chooses select option by the index from a list", function(){
	var template = stache('<select {($value)}="index-to-selected(~person, people)"><option value="none"></option>{{#each people}}<option value="{{%index}}">{{name}}</option>{{/each}}</select>');

	var map = new DefineMap({
		person: "Anne",
		people: [
			"Matthew",
			"Anne",
			"Wilbur"
		]
	});

	var select = template(map).firstChild;

	// Initial value
	QUnit.equal(select.value, 1, "initially set to the first value");

	// Select a different thing.
	select.value = 2;
	canEvent.trigger.call(select, "change");

	QUnit.equal(map.person, "Wilbur", "now it is me");

	// Change the selected the other way.
	map.person = map.people.item(0);

	QUnit.equal(select.value, 0, "set back");

	// Can be set to other stuff too
	select.value = "none";
	canEvent.trigger.call(select, "change");

	QUnit.equal(map.person, undefined, "now undefined because not in the list");
});
Example #8
0
QUnit.test("Works with checkboxes", function(){
	var template = stache("<input type='checkbox' {($checked)}='boolean-to-inList(item, list)' />");
	var map = new DefineMap({
		item: 2,
		list: new DefineList([ 1, 2, 3 ])
	});

	var frag = template(map);
	var input = frag.firstChild;

	QUnit.ok(input.checked, "it is initially checked");
	QUnit.equal(map.list.indexOf(2), 1, "two is in the list");

	input.checked = false;
	canEvent.trigger.call(input, "change");

	QUnit.equal(map.list.indexOf(2), -1, "No longer in the list");

	map.item = 3;
	QUnit.ok(input.checked, "3 is in the list");

	// Add something to the list
	map.item = 5;
	QUnit.ok(!input.checked, "5 is not in the list");

	map.list.push(5);
	QUnit.ok(input.checked, "Now 5 is in the list");

	map.item = 6;
	input.checked = true;
	canEvent.trigger.call(input, "change");

	QUnit.equal(map.list.indexOf(6), 3, "pushed into the list");
});
Example #9
0
QUnit.test("create an observable let scope (#593)", function(){
	var template = stache("<div>{{# for(thing of this.stuff)}}"+
		"{{let theValue=null}}"+
		"{{write theValue}}"+
		"<label>{{theValue}}</label>"+
    "{{/ for}}</div>");

	var obs = [];
	var frag = template({
		stuff: [{},{}],
		write: function(theValueObservable){
			obs.push(theValueObservable);
		}
	});

	canReflect.setValue( obs[0] , 1);
	canReflect.setValue( obs[1] , 2);

	var labels = frag.firstChild.getElementsByTagName("label");

	QUnit.equal(labels[0].innerHTML,"1", "first element");
	QUnit.equal(labels[1].innerHTML,"2", "first element");


});
QUnit.test("Components can be instantiated with <content> - with scope - leakScope false", function() {
	var ComponentConstructor = Component.extend({
		leakScope: false,
		tag: "new-instantiation-content-leakscope-false",
		view: "Hello <content>{{message}}</content>",
		ViewModel: {
			message: {default: "world"}
		}
	});

	var scopeVM = new DefineMap({});
	var componentInstance = new ComponentConstructor({
		content: "<em>{{message}}</em>",
		scope: scopeVM
	});
	var element = componentInstance.element;

	// Start off without the key defined in the scope; with leakScope false,
	// no message will be rendered
	QUnit.equal(element.innerHTML, "Hello <em></em>", "content is rendered with the provided scope");

	// Set the key in the scope; now a message will be rendered
	scopeVM.set("message", "mundo");
	QUnit.equal(element.innerHTML, "Hello <em>mundo</em>", "content updates with the provided scope");
});
Example #11
0
	Game.get({id: 1, populate: "teamRef"}).then(function(game){
		game.on("teamRef", handler);
		game.teamRef.on("value", handler);
		var teamRef = game.teamRef;
		QUnit.ok( teamRef.value instanceof Team);
		QUnit.equal(teamRef.value.name, "Cubs");
		QUnit.equal(teamRef.id, 2);

		Game.getList({}).then(function(games){
			QUnit.ok( games[0].teamRef === teamRef, "same team ref");
			QUnit.ok( games[2].teamRef === teamRef, "same team ref on a different object");
			QUnit.ok( teamRef.value instanceof Team);
			QUnit.equal(teamRef.id, 2);
			QUnit.equal(teamRef.value.name, "Cubs");
			QUnit.equal(games[1].teamRef.id, 3);

			QUnit.equal(games[0].teamRef.isResolved(), true);

			games[1].teamRef.on("value", function(ev, newVal){
				QUnit.ok(newVal instanceof Team);
				QUnit.equal(newVal.name, "Bears");
				QUnit.start();
			});

			QUnit.equal(games[1].teamRef.isResolved(), false);

		});
	}, function(error){
Example #12
0
QUnit.test('Default values cannot be set (#8)', function() {
	var Person = function() {};

	define(Person.prototype, {
		first: {
			type: 'string',
			value: 'Chris'
		},
		last: {
			type: 'string',
			value: 'Gomez'
		},
		fullName: {
			get: function() {
				return this.first + ' ' + this.last;
			}
		}
	});

	var p = new Person();

	QUnit.equal(p.fullName, 'Chris Gomez', 'Fullname is correct');
	p.first = 'Sara';
	QUnit.equal(p.fullName, 'Sara Gomez', 'Fullname is correct after update');
});
Example #13
0
	helpers.soon(function(){
		var child = parent.shadowRoot.querySelector("child-one");
		QUnit.equal(child.foo, "bar", "data was passed from parent to child");
		QUnit.equal(child.shadowRoot.textContent, "bar",
								"content was rendered correctly");
		QUnit.start();
	});
Example #14
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;
});
Example #15
0
QUnit.test("basic Type", function() {
	var Foo = function(name) {
		this.name = name;
	};
	Foo.prototype.getName = function() {
		return this.name;
	};

	var Typer = function(foo) {
		this.foo = foo;
	};

	define(Typer.prototype, {
		foo: {
			Type: Foo
		}
	});

	var t = new Typer("Justin");
	QUnit.equal(t.foo.getName(), "Justin", "correctly created an instance");

	var brian = new Foo("brian");

	t.foo = brian;

	QUnit.equal(t.foo, brian, "same instances");

});
Example #16
0
QUnit.skip("DOM events work when can-defined", function(){
	var view = stache("{{foo}}");

	var EventEl = class extends CanElement {
		static get view() { return view; }
	};

	define(EventEl.prototype, {
		foo: "string"
	});

	customElements.define("el-with-events", EventEl);

	var events = 0, inc = function() { events++; };

	var el = new EventEl();

	el.addEventListener("foo", inc);
	el.addEventListener("some-event", inc);

	el.foo = "bar";
	QUnit.equal(events, 1, "one event");

	domDispatch.call(el, "some-event");
	QUnit.equal(events, 2, "two events");
});
Example #17
0
	helpers.soon(function(){
		var el = fixture().querySelector("data-from-scope");

		QUnit.equal(el.foo, "bar", "got data from the scope");
		QUnit.equal(el.shadowRoot.textContent, "bar");
		QUnit.start();
	});
QUnit.test("Components can be instantiated with templates", function() {
	var ComponentConstructor = Component.extend({
		tag: "new-instantiation-templates",
		view: "<can-slot name='messageInput' />"
	});

	var scopeVM = new DefineMap({
		message: "world"
	});
	var componentInstance = new ComponentConstructor({
		scope: scopeVM,
		templates: {
			messageInput: "<input value:bind='message' />"
		}
	});

	// Basics look correct
	var element = componentInstance.element;
	var inputElement = element.querySelector("input");
	QUnit.ok(inputElement, "template rendered");
	QUnit.equal(inputElement.value, "world", "input has correct value");

	// Updating the scopeVM should update the template
	scopeVM.message = "mundo";
	QUnit.equal(inputElement.value, "mundo", "input has correct value after updating scopeVM");
});
Example #19
0
QUnit.test("basics", function(){
    var items = new DefineMap({ people: [{name: "Justin"},{name: "Brian"}], count: 1000 });
    QUnit.ok(items.people instanceof types.DefineList, "people is list");
    QUnit.ok(items.people.item(0) instanceof types.DefineMap, "1st object is Map");
    QUnit.ok(items.people.item(1) instanceof types.DefineMap, "2nd object is Map");
    QUnit.equal(items.people.item(1).name, "Brian", "2nd object's name is right");
    QUnit.equal(items.count, 1000, "count is number");
});
QUnit.test("initialization does not cause Observation.add", function(){
	ObservationRecorder.start();
	var m = new SimpleMap();
	m = new SimpleMap({first: "second"});
	var observationRecord = ObservationRecorder.stop();

	QUnit.equal(observationRecord.keyDependencies.size , 0, "no key deps");
	QUnit.equal(observationRecord.valueDependencies.size , 0, "no value deps");
});
	afterMutation(function() {
		QUnit.equal(innerElCounter, 1, "inner called once");
		QUnit.equal(outerElCounter, 1, "outer called once");
		globals.setKeyValue("customElements", function(){
			return oldCE;
		});
		callbacks.tagHandler = origTagHandler;
		QUnit.start();
	});
Example #22
0
	helpers.soon(function(){
		var myApp = fixture().querySelector("my-app");

		QUnit.equal(myApp.name, "world");
		QUnit.ok(myApp instanceof HTMLElement);

		QUnit.equal(myApp.shadowRoot.textContent, "hello world");

		QUnit.start();
	});
Example #23
0
	each(vm, function(value, key) {
		if (i === 0) {
			QUnit.equal(key, "foo");
			QUnit.equal(value, "bar");
		} else {
			QUnit.equal(key, "baz");
			QUnit.equal(value, "qux");
		}
		i++;
	});
Example #24
0
QUnit.test("basic type", function() {

	QUnit.expect(6);

	var Typer = function(arrayWithAddedItem, listWithAddedItem) {
		this.arrayWithAddedItem = arrayWithAddedItem;
		this.listWithAddedItem = listWithAddedItem;
	};

	define(Typer.prototype, {
		arrayWithAddedItem: {
			type: function(value) {
				if (value && value.push) {
					value.push("item");
				}
				return value;
			}
		},
		listWithAddedItem: {
			type: function(value) {
				if (value && value.push) {
					value.push("item");
				}
				return value;
			},
			Type: CanList
		}
	});




	var t = new Typer();
	deepEqual(Object.keys(t), [], "no keys");

	var array = [];
	t.arrayWithAddedItem = array;

	deepEqual(array, ["item"], "updated array");
	QUnit.equal(t.arrayWithAddedItem, array, "leave value as array");

	t.listWithAddedItem = [];

	QUnit.ok(t.listWithAddedItem instanceof CanList, "convert to CanList");
	QUnit.equal(t.listWithAddedItem[0], "item", "has item in it");

	compute(function() {
		return t.listWithAddedItem.attr("length");
	}).addEventListener("change", function(ev, newVal) {
		QUnit.equal(newVal, 2, "got a length change");
	});

	t.listWithAddedItem.push("another item");

});
Example #25
0
QUnit.test('compute props can be set to null or undefined (#2372)', function() {
	var VM = DefineMap.extend({
		computeProp: {
			type: 'compute'
		}
	});

	var vmNull = new VM({computeProp: null});
	QUnit.equal(vmNull.get('computeProp'), null, 'computeProp is null, no error thrown');
	var vmUndef = new VM({computeProp: undefined});
	QUnit.equal(vmUndef.get('computeProp'), undefined, 'computeProp is undefined, no error thrown');
});
Example #26
0
QUnit.test("Calling .unbind() with no arguments should tear down all event handlers", function () {
	var count = compute(0);
	count.on('change', function() {
		console.log('Count changed');
	});
	var handlers = count.computeInstance[canSymbol.for("can.meta")].handlers;

	QUnit.equal(handlers.get(["change"]).length, 1, "Change event added");

	count.unbind();
	QUnit.equal(handlers.get(["change"]).length, 0, "All events for compute removed");
});
QUnit.test("Components can be instantiated with viewModel", function() {

	// These are the observables that would typically be outside the component’s scope
	var bindMap = new SimpleMap({inner: new SimpleMap({key: "original bind value"})});
	var fromMap = new SimpleMap({inner: new SimpleMap({key: "original from value"})});
	var toMap = new SimpleMap({inner: new SimpleMap({key: "original to value"})});

	// Our component
	var ComponentConstructor = Component.extend({
		tag: "new-instantiation-viewmodel",
		view: "Hello",
		ViewModel: {
			fromChildProp: "string",
			plainProp: "string",
			toParentProp: "string",
			twoWayProp: "string",
			nullProp: {
				default: function() {
					return 'bar';
				}
			}
		}
	});

	// Create a new instance of our component
	var componentInstance = new ComponentConstructor({
		// Pass the viewModel with a mix of plain and observable values
		viewModel: {
			plainProp: "plain value",
			fromChildProp: value.from(fromMap, "inner.key"),
			toParentProp: value.to(toMap, "inner.key"),
			twoWayProp: value.bind(bindMap, "inner.key"),
			nullProp: null
		}
	});
	var viewModel = componentInstance.viewModel;

	// Initial values are correct
	QUnit.equal(viewModel.fromChildProp, "original from value", "fromChildProp init");
	QUnit.equal(viewModel.plainProp, "plain value", "plainProp init");
	QUnit.equal(viewModel.toParentProp, undefined, "toParentProp init");
	QUnit.equal(viewModel.twoWayProp, "original bind value", "twoWayProp init");
	QUnit.equal(viewModel.nullProp, null, "nullProp init");

	// Updating the fromChildProp
	fromMap.get("inner").set("key", "new from value");
	QUnit.equal(viewModel.fromChildProp, "new from value", "viewModel updated after fromMap set");

	// Updating the toParentProp
	viewModel.toParentProp = "new to value";
	QUnit.equal(toMap.get("inner").get("key"), "new to value", "toMap updated after viewModel set");

	// Updating the twoWayProp
	bindMap.get("inner").set("key", "new bind value");
	QUnit.equal(viewModel.twoWayProp, "new bind value", "viewModel updated after bindMap set");
	viewModel.twoWayProp = "newest bind value";
	QUnit.equal(bindMap.get("inner").get("key"), "newest bind value", "bindMap updated after viewModel set");
});
Example #28
0
QUnit.test("#is works with call expressions", function(){
	var template = stache("{{#is(foo, true)}}foo{{else}}bar{{/eq}}");
	var map = new DefineMap({
		foo: true
	});
	var div = document.createElement("div");
	var frag = template(map);

	div.appendChild(frag);
	QUnit.equal(div.innerHTML, "foo");
	map.foo = false;
	QUnit.equal(div.innerHTML, "bar");
});
Example #29
0
QUnit.test("innerHTML supported", function(){

  var document = new Document();
  document.__addSerializerAndParser(new Serializer(voidMap), new Parser(tokenize, document, voidMap));

  document.body.innerHTML = "<span class='bar'>HI</span>";

  QUnit.equal( document.body.firstChild.nodeName, "SPAN");
  QUnit.equal( document.body.firstChild.className, "bar");
  QUnit.equal( document.body.firstChild.firstChild.nodeValue, "HI");

  QUnit.equal( document.body.innerHTML, "<span class=\"bar\">HI</span>");
});
Example #30
0
QUnit.test("outerHTML supported", function(){

  var document = new Document();
  document.__addSerializerAndParser(new Serializer(voidMap), new Parser(tokenize, document, voidMap));

  document.body.innerHTML = "<span/><div id='item'>HI</div><span/>";

  var item = document.getElementById('item');

  QUnit.equal( item.outerHTML, "<div id=\"item\">HI</div>", "getter");
  item.outerHTML = "<label>IT</label>";

  QUnit.equal( document.body.innerHTML,  "<span></span><label>IT</label><span></span>", "setter");
});