Fix Knockout tests for 0.9/generics

Allows ko.observable()
This commit is contained in:
Nicholas Wolverson
2013-06-19 13:32:56 +01:00
parent 50b3d35c54
commit 92011c1772
4 changed files with 36 additions and 37 deletions

View File

@@ -2,8 +2,6 @@
/// <reference path="../knockout.d.ts" />
/// <reference path="../../knockout.mapping/knockout.mapping.d.ts" />
declare var $;
var dummyTemplateEngine = function (templates?) {
var inMemoryTemplates = templates || {};
var inMemoryTemplateData = {};
@@ -137,7 +135,7 @@ describe('Templating', function() {
});
it('Should automatically rerender into DOM element when dependencies change', function () {
var dependency = new ko.observable("A");
var dependency = ko.observable("A");
ko.setTemplateEngine(new dummyTemplateEngine({ someTemplate: function () {
return "Value = " + dependency();
}
@@ -153,7 +151,7 @@ describe('Templating', function() {
});
it('Should not rerender DOM element if observable accessed in \'afterRender\' callaback is changed', function () {
var observable = new ko.observable("A"), count = 0;
var observable = ko.observable("A"), count = 0;
var myCallback = function(elementsArray, dataItem) {
observable(); // access observable in callback
};
@@ -171,7 +169,7 @@ describe('Templating', function() {
});
it('If the supplied data item is observable, evaluates it and has subscription on it', function () {
var observable = new ko.observable("A");
var observable = ko.observable("A");
ko.setTemplateEngine(new dummyTemplateEngine({ someTemplate: function (data) {
return "Value = " + data;
}
@@ -184,7 +182,7 @@ describe('Templating', function() {
});
it('Should stop updating DOM nodes when the dependency next changes if the DOM node has been removed from the document', function () {
var dependency = new ko.observable("A");
var dependency = ko.observable("A");
var template = { someTemplate: function () { return "Value = " + dependency() } };
ko.setTemplateEngine(new dummyTemplateEngine(template));
@@ -275,7 +273,7 @@ describe('Templating', function() {
});
it('Should rerender chained templates when their dependencies change, without rerendering parent templates', function () {
var observable = new ko.observable("ABC");
var observable = ko.observable("ABC");
var timesRenderedOuter = 0, timesRenderedInner = 0;
ko.setTemplateEngine(new dummyTemplateEngine({
outerTemplate: function () { timesRenderedOuter++; return "outer template output, [renderTemplate:innerTemplate]" }, // [renderTemplate:...] is special syntax supported by dummy template engine
@@ -390,7 +388,7 @@ describe('Templating', function() {
});
it('Data binding syntax should support \'foreach\' option, whereby it renders for each item in an array but doesn\'t rerender everything if you push or splice', function () {
var myArray = new ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
var myArray = ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "<div>The item is [js: personName]</div>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -406,7 +404,7 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should apply bindings within the context of each item in the array', function () {
var myArray = new ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
var myArray = ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item is <span data-bind='text: personName'></span>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -479,7 +477,7 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should apply bindings with an $index in the context', function () {
var myArray = new ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
var myArray = ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item # is <span data-bind='text: $index'></span>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -488,7 +486,7 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should update bindings that reference an $index if the list changes', function () {
var myArray = new ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
var myArray = ko.observableArray([{ personName: "Bob" }, { personName: "Frank"}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item <span data-bind='text: personName'></span>is <span data-bind='text: $index'></span>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -504,7 +502,7 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should accept array with "undefined" and "null" items', function () {
var myArray = new ko.observableArray([undefined, null]);
var myArray = ko.observableArray([undefined, null]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item is <span data-bind='text: String($data)'></span>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -513,8 +511,8 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should update DOM nodes when a dependency of their mapping function changes', function() {
var myObservable = new ko.observable("Steve");
var myArray = new ko.observableArray([{ personName: "Bob" }, { personName: myObservable }, { personName: "Another" }]);
var myObservable = ko.observable("Steve");
var myArray = ko.observableArray([{ personName: "Bob" }, { personName: myObservable }, { personName: "Another" }]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "<div>The item is [js: ko.utils.unwrapObservable(personName)]</div>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -535,7 +533,7 @@ describe('Templating', function() {
});
it('Data binding \'foreach\' option should treat a null parameter as meaning \'no items\'', function() {
var myArray = new ko.observableArray(["A", "B"]);
var myArray = ko.observableArray(["A", "B"]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "hello" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -551,7 +549,7 @@ describe('Templating', function() {
it('Data binding \'foreach\' option should accept an \"as\" option to define an alias for the iteration variable', function() {
// Note: There are more detailed specs (e.g., covering nesting) associated with the "foreach" binding which
// uses this templating functionality internally.
var myArray = new ko.observableArray(["A", "B"]);
var myArray = ko.observableArray(["A", "B"]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "[js:myAliasedItem]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection, as: \"myAliasedItem\" }'></div>";
@@ -561,7 +559,7 @@ describe('Templating', function() {
it('Data binding \'foreach\' option should stop tracking inner observables when the container node is removed', function() {
var innerObservable = ko.observable("some value");
var myArray = new ko.observableArray([{obsVal:innerObservable}, {obsVal:innerObservable}]);
var myArray = ko.observableArray([{obsVal:innerObservable}, {obsVal:innerObservable}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item is [js: ko.utils.unwrapObservable(obsVal)]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -574,7 +572,7 @@ describe('Templating', function() {
it('Data binding \'foreach\' option should stop tracking inner observables related to each array item when that array item is removed', function() {
var innerObservable = ko.observable("some value");
var myArray = new ko.observableArray([{obsVal:innerObservable}, {obsVal:innerObservable}]);
var myArray = ko.observableArray([{obsVal:innerObservable}, {obsVal:innerObservable}]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "The item is [js: ko.utils.unwrapObservable(obsVal)]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -588,7 +586,7 @@ describe('Templating', function() {
});
it('Data binding syntax should omit any items whose \'_destroy\' flag is set (unwrapping the flag if it is observable)', function() {
var myArray = new ko.observableArray([{ someProp: 1 }, { someProp: 2, _destroy: 'evals to true' }, { someProp : 3 }, { someProp: 4, _destroy: ko.observable(false) }]);
var myArray = ko.observableArray([{ someProp: 1 }, { someProp: 2, _destroy: 'evals to true' }, { someProp : 3 }, { someProp: 4, _destroy: ko.observable(false) }]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "<div>someProp=[js: someProp]</div>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection }'></div>";
@@ -597,7 +595,7 @@ describe('Templating', function() {
});
it('Data binding syntax should include any items whose \'_destroy\' flag is set if you use includeDestroyed', function() {
var myArray = new ko.observableArray([{ someProp: 1 }, { someProp: 2, _destroy: 'evals to true' }, { someProp : 3 }]);
var myArray = ko.observableArray([{ someProp: 1 }, { someProp: 2, _destroy: 'evals to true' }, { someProp : 3 }]);
ko.setTemplateEngine(new dummyTemplateEngine({ itemTemplate: "<div>someProp=[js: someProp]</div>" }));
testNode.innerHTML = "<div data-bind='template: { name: \"itemTemplate\", foreach: myCollection, includeDestroyed: true }'></div>";
@@ -677,7 +675,7 @@ describe('Templating', function() {
});
it('Should be able to render a different template for each array entry by passing a function as template name, with the array entry\'s binding context available as a second parameter', function() {
var myArray = new ko.observableArray([
var myArray = ko.observableArray([
{ preferredTemplate: 1, someProperty: 'firstItemValue' },
{ preferredTemplate: 2, someProperty: 'secondItemValue' }
]);
@@ -700,7 +698,7 @@ describe('Templating', function() {
it('Data binding \'templateOptions\' should be passed to template', function() {
var myModel = {
someAdditionalData: { myAdditionalProp: "someAdditionalValue" },
people: new ko.observableArray([
people: ko.observableArray([
{ name: "Alpha" },
{ name: "Beta" }
])

View File

@@ -53,7 +53,7 @@ function test_computed() {
});
}
function MyViewModel() {
function MyViewModel1() {
this.price = ko.observable(25.99);
this.formattedPrice = ko.computed({
@@ -68,7 +68,7 @@ function test_computed() {
});
}
function MyViewModel() {
function MyViewModel2() {
this.acceptedNumericValue = ko.observable(123);
this.lastInputWasValid = ko.observable(true);
@@ -90,13 +90,13 @@ function test_computed() {
}
class GetterViewModel {
private _selectedRange: KnockoutObservableAny;
private _selectedRange: KnockoutObservable<any>;
constructor() {
this._selectedRange = ko.observable();
}
public range: KnockoutObservableAny;
public range: KnockoutObservable<any>;
}
function testGetter() {
@@ -333,12 +333,12 @@ function test_more() {
return target;
};
function AppViewModel(first, last) {
function AppViewModel2(first, last) {
this.firstName = ko.observable(first).extend({ required: "Please enter a first name" });
this.lastName = ko.observable(last).extend({ required: "" });
}
ko.applyBindings(new AppViewModel("Bob", "Smith"));
ko.applyBindings(new AppViewModel2("Bob", "Smith"));
var first;
this.firstName = ko.observable(first).extend({ required: "Please enter a first name", logChange: "first name" });
@@ -347,7 +347,7 @@ function test_more() {
return name.toUpperCase();
}).extend({ throttle: 500 });
function AppViewModel() {
function AppViewModel3() {
this.instantaneousValue = ko.observable();
this.throttledValue = ko.computed(this.instantaneousValue)
.extend({ throttle: 400 });
@@ -420,7 +420,7 @@ function test_more() {
this.done = ko.observable(done);
}
function AppViewModel() {
function AppViewModel4() {
this.tasks = ko.observableArray([
new Task('Find new desktop background', true),
new Task('Put shiny stickers on laptop', false),
@@ -430,7 +430,7 @@ function test_more() {
this.doneTasks = this.tasks.filterByProperty("done", true);
}
ko.applyBindings(new AppViewModel());
ko.applyBindings(new AppViewModel4());
this.doneTasks = ko.computed(function () {
var all = this.tasks(), done = [];
for (var i = 0; i < all.length; i++)
@@ -441,7 +441,7 @@ function test_more() {
}
function test_mappingplugin() {
var viewModel = {
var viewModel0 = {
serverTime: ko.observable(),
numUsers: ko.observable()
}
@@ -449,8 +449,8 @@ function test_mappingplugin() {
serverTime: '2010-01-07',
numUsers: 3
};
viewModel.serverTime(data.serverTime);
viewModel.numUsers(data.numUsers);
viewModel0.serverTime(data.serverTime);
viewModel0.numUsers(data.numUsers);
var viewModel = ko.mapping.fromJS(data);
ko.mapping.fromJS(data, viewModel);
@@ -526,7 +526,7 @@ function test_misc() {
return this;
};
this.myObservable = <KnockoutObservableString>ko.observable("myValue").publishOn("myTopic");
this.myObservable = <KnockoutObservable<string>>ko.observable("myValue").publishOn("myTopic");
ko.subscribable.fn.subscribeTo = function (topic) {
postbox.subscribe(this, null, topic);
@@ -534,7 +534,7 @@ function test_misc() {
return this;
};
this.observableFromAnotherVM = <KnockoutObservableAny>ko.observable().subscribeTo("myTopic");
this.observableFromAnotherVM = <KnockoutObservable<any>>ko.observable().subscribeTo("myTopic");
postbox.subscribe(function (newValue) {
this(newValue);