Skip to content

Commit

Permalink
Add specs to demonstrate breaking changes (these pass on master)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Sep 30, 2012
1 parent fea438b commit fc79472
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spec/defaultBindings/withBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe('Binding: With', {
value_of(testNode.childNodes[0].childNodes.length).should_be(0);
},

'Should reconstruct and bind descendants when the data item notifies about mutation': function() {
var someItem = ko.observable({ childProp: 'Hello' });

testNode.innerHTML = "<div data-bind='with: someItem'><span data-bind='text: childProp'></span></div>";
ko.applyBindings({ someItem: someItem }, testNode);
value_of(testNode.childNodes[0].childNodes[0]).should_contain_text("Hello");

// Force "update" binding handler to fire, then check the DOM changed
someItem().childProp = 'Goodbye';
someItem.valueHasMutated();
value_of(testNode.childNodes[0].childNodes[0]).should_contain_text("Goodbye");
},

'Should not bind the same elements more than once even if the supplied value notifies a change': function() {
var countedClicks = 0;
var someItem = ko.observable({
Expand Down
29 changes: 29 additions & 0 deletions spec/templatingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ describe('Templating', {
value_of(testNode.childNodes[0].innerHTML).should_be("result = 123");
},

'Should re-render a named template when its data item notifies about mutation': function () {
ko.setTemplateEngine(new dummyTemplateEngine({ someTemplate: "result = [js: childProp]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"someTemplate\", data: someProp }'></div>";

var myData = ko.observable({ childProp: 123 });
ko.applyBindings({ someProp: myData }, testNode);
value_of(testNode.childNodes[0].innerHTML).should_be("result = 123");

// Now mutate and notify
myData().childProp = 456;
myData.valueHasMutated();
value_of(testNode.childNodes[0].innerHTML).should_be("result = 456");
},

'Should stop tracking inner observables immediately when the container node is removed from the document': function() {
var innerObservable = ko.observable("some value");
ko.setTemplateEngine(new dummyTemplateEngine({ someTemplate: "result = [js: childProp()]" }));
Expand All @@ -207,6 +221,21 @@ describe('Templating', {
value_of(innerObservable.getSubscriptionsCount()).should_be(0);
},

'Should be able to pick template via an observable model property': function () {
ko.setTemplateEngine(new dummyTemplateEngine({
firstTemplate: "First template output",
secondTemplate: "Second template output"
}));

var chosenTemplate = ko.observable("firstTemplate");
testNode.innerHTML = "<div data-bind='template: chosenTemplate'></div>";
ko.applyBindings({ chosenTemplate: chosenTemplate }, testNode);
value_of(testNode.childNodes[0].innerHTML).should_be("First template output");

chosenTemplate("secondTemplate");
value_of(testNode.childNodes[0].innerHTML).should_be("Second template output");
},

'Should be able to pick template as a function of the data item using data-bind syntax, with the binding context available as a second parameter': function () {
var templatePicker = function(dataItem, bindingContext) {
// Having the entire binding context available means you can read sibling or parent level properties
Expand Down

0 comments on commit fc79472

Please sign in to comment.