Skip to content

Commit

Permalink
New specs to show proper behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Jun 3, 2017
1 parent 5dc0ba0 commit 01237e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 20 additions & 0 deletions spec/asyncBindingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,24 @@ describe("Deferred bindings", function() {
jasmine.Clock.tick(1);
expect(testNode.childNodes[0]).toContainText('');
});

it('Should leave descendant nodes unchanged if the value is truthy and remains truthy when changed', function() {
var someItem = ko.observable(true);
testNode.innerHTML = "<div data-bind='if: someItem'><span data-bind='text: (++counter)'></span></div>";
var originalNode = testNode.childNodes[0].childNodes[0];

// Value is initially true, so nodes are retained
ko.applyBindings({ someItem: someItem, counter: 0 }, testNode);
expect(testNode.childNodes[0].childNodes[0].tagName.toLowerCase()).toEqual("span");
expect(testNode.childNodes[0].childNodes[0]).toEqual(originalNode);
expect(testNode).toContainText("1");

// Change the value to a different truthy value; see the previous SPAN remains
someItem('different truthy value');
jasmine.Clock.tick(1);
expect(testNode.childNodes[0].childNodes[0].tagName.toLowerCase()).toEqual("span");
expect(testNode.childNodes[0].childNodes[0]).toEqual(originalNode);
expect(testNode).toContainText("1");
});

});
6 changes: 4 additions & 2 deletions spec/defaultBindings/ifBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ describe('Binding: If', function() {

it('Should leave descendant nodes unchanged if the value is truthy and remains truthy when changed', function() {
var someItem = ko.observable(true);
testNode.innerHTML = "<div data-bind='if: someItem'><span></span></div>";
testNode.innerHTML = "<div data-bind='if: someItem'><span data-bind='text: (++counter)'></span></div>";
var originalNode = testNode.childNodes[0].childNodes[0];

// Value is initially true, so nodes are retained
ko.applyBindings({ someItem: someItem }, testNode);
ko.applyBindings({ someItem: someItem, counter: 0 }, testNode);
expect(testNode.childNodes[0].childNodes[0].tagName.toLowerCase()).toEqual("span");
expect(testNode.childNodes[0].childNodes[0]).toEqual(originalNode);
expect(testNode).toContainText("1");

// Change the value to a different truthy value; see the previous SPAN remains
someItem('different truthy value');
expect(testNode.childNodes[0].childNodes[0].tagName.toLowerCase()).toEqual("span");
expect(testNode.childNodes[0].childNodes[0]).toEqual(originalNode);
expect(testNode).toContainText("1");
});

it('Should toggle the presence and bindedness of descendant nodes according to the truthiness of the value', function() {
Expand Down

0 comments on commit 01237e7

Please sign in to comment.