blossom
Version:
Modern, Cross-Platform Application Framework
196 lines (155 loc) • 6.3 kB
JavaScript
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2010 Apple Inc. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*global module test equals context ok same */
// ..........................................................
// contentPropertyDidChange()
//
var view, content ;
suite('SC.Button#contentPropertyDidChange', {
setup: function() {
content = SC.Object.create();
view = SC.View.create(SC.Control, SC.Button);
view.set('title', 'hello world');
},
teardown: function() {
content = null;
view.destroy();
}
});
test("invoked with key = * whenever content changes", function() {
view.contentPropertyDidChange = function(target, key) {
ok(target === content, 'should pass content object as target target=%@'.fmt(target));
equals(key, '*', 'should pass * as key');
};
view.set('content', content);
});
test("invoked with key = foo whenever a propery on content is changed", function() {
var isTesting = false, curKey ;
view.contentPropertyDidChange = function(target, key) {
if (!isTesting) return ; //wait until testing should begin...
ok(target === content, 'should pass content object as target');
equals(key, curKey, 'should pass * as key');
};
view.set('content', content);
isTesting = true ;
curKey= 'foo';
content.set('foo', 'foo');
curKey = 'bar';
content.set('bar', 'bar');
});
test("should no longer be invoked when a key is changed on a former content object", function() {
var isTesting = false;
view.contentPropertyDidChange = function(target, key) {
if (!isTesting) return ; //wait until testing should begin...
ok(false, 'should not invoke contentPropertyDidChange after content is removed');
};
view.set('content', content);
content.set('foo', 'foo');
view.set('content', null);
isTesting= true ;
content.set('bar', 'bar');
});
test("should fire even on a content object set when the object is created", function() {
var callCount = 0;
var view = SC.View.create(SC.Control, {
contentPropertyDidChange: function() { callCount++; },
content: content
});
equals(callCount, 1, 'should call contentPropertyDidChange on init to do initial setup');
content.set('foo', 'foo');
equals(callCount, 2, 'should call contentPropertyDidChange when changing content.foo');
});
suite('SC.Button#titleRendering', {
setup: function() {
content = SC.Object.create();
view = SC.View.create(SC.Control, SC.Button);
view.set('title', 'hello world');
},
teardown: function() {
content = null;
view.destroy();
}
});
test("should return the title localized or not", function() {
equals(view.displayTitle(), 'hello world', 'should return an empty string as the title is not localized');
});
test("should return the button view context with the title set", function() {
context = SC.RenderContext();
ok((view.renderTitle(context, true).length>0), 'title string were added to the context');
});
// ..........................................................
// updatePropertyFromContent()
//
suite("SC.Button#updatePropertyFromContent()", {
setup: function() {
content = SC.Object.create({ foo: "foo", bar: "bar" });
view = SC.View.create(SC.Control, SC.Button, { content: content });
},
teardown: function() {
content = null ;
view.destroy();
}
}) ;
test("copies value of content[key] to this[prop] if changed key. Gets key from contentKey property", function() {
view.contentValueKey = 'foo'; // set key mapping.
view.updatePropertyFromContent('value', 'foo', 'contentValueKey');
equals(view.get('value'), 'foo', 'should copy foo from content.foo to value');
});
test("does nothing of changed key does not match contentKey value" ,function() {
view.value = "foo";
view.contentValueKey = "foo" ;
view.updatePropertyFromContent('value', 'bar', 'contentValueKey');
equals(view.get('value'), 'foo', 'should not have changed "value"');
});
test("if contentKey is not passed, assume contentPROPKey", function() {
view.contentValueKey = "foo";
view.updatePropertyFromContent("value", "foo");
equals(view.get('value'), 'foo', 'should have looked at foo since contentValueKey is set to foo');
});
test("if contentFooKey is not set on receiver, ask displayDelegate", function() {
view.displayDelegate = SC.Object.create({ contentValueKey: "foo" });
view.updatePropertyFromContent("value", "foo", "contentValueKey");
equals(view.get('value'), 'foo', 'should have looked at foo since contentValueKey is set to foo');
});
test("should be able to get value from a content object that is not SC.Object", function() {
view.content = { foo: "foo" };
view.contentValueKey = "foo";
view.updatePropertyFromContent("value", "foo", "contentValueKey");
equals(view.get('value'), 'foo', 'should have looked at foo since contentValueKey is set to foo');
});
// ..........................................................
// updateContentWithValueObserver()
//
suite("SC.Button#updatePropertyFromContent()", {
setup: function() {
content = SC.Object.create({ foo: "foo", bar: "bar" });
view = SC.View.create(SC.Control, SC.Button, {
value: "bar",
content: content,
contentValueKey: "bar",
displayDelegate: SC.Object.create({ contentValueKey: "foo" })
});
},
teardown: function() {
content = null ;
view.destroy();
}
}) ;
test("if contentValueKey is set, changing value will be pushed to content", function() {
view.set('value', 'baz');
equals(content.get('bar'), 'baz', 'should copy from view.value to content');
});
test("if contentValueKey is undefined, asks display delegate instead", function() {
delete view.contentValueKey ;
view.set('value', 'baz');
equals(content.get('foo'), 'baz', 'should copy from view.value to content');
});
test("if contentValueKey is not set & displayDelegate not set, does nothing", function() {
delete view.contentValueKey;
delete view.displayDelegate;
view.set('value', 'baz');
equals(content.get('foo'), 'foo', 'should not change');
});