1 describe('Binding: Text', {
2 before_each: JSSpec.prepareTestNode,
4 'Should assign the value to the node, HTML-encoding the value': function () {
5 var model = { textProp: "'Val <with> \"special\" <i>characters</i>'" };
6 testNode.innerHTML = "<span data-bind='text:textProp'></span>";
7 ko.applyBindings(model, testNode);
8 value_of(testNode.childNodes[0].textContent || testNode.childNodes[0].innerText).should_be(model.textProp);
11 'Should assign an empty string as value if the model value is null': function () {
12 testNode.innerHTML = "<span data-bind='text:(null)' ></span>";
13 ko.applyBindings(null, testNode);
14 var actualText = "textContent" in testNode.childNodes[0] ? testNode.childNodes[0].textContent : testNode.childNodes[0].innerText;
15 value_of(actualText).should_be("");
18 'Should assign an empty string as value if the model value is undefined': function () {
19 testNode.innerHTML = "<span data-bind='text:undefined' ></span>";
20 ko.applyBindings(null, testNode);
21 var actualText = "textContent" in testNode.childNodes[0] ? testNode.childNodes[0].textContent : testNode.childNodes[0].innerText;
22 value_of(actualText).should_be("");
25 'Should work with virtual elements, adding a text node between the comments': function () {
26 var observable = ko.observable("Some text");
27 testNode.innerHTML = "xxx <!-- ko text: textProp --><!-- /ko -->";
28 ko.applyBindings({textProp: observable}, testNode);
29 value_of(testNode).should_contain_text("xxx Some text");
30 value_of(testNode).should_contain_html("xxx <!-- ko text: textprop -->some text<!-- /ko -->");
32 // update observable; should update text
33 observable("New text");
34 value_of(testNode).should_contain_text("xxx New text");
35 value_of(testNode).should_contain_html("xxx <!-- ko text: textprop -->new text<!-- /ko -->");
37 // clear observable; should remove text
38 observable(undefined);
39 value_of(testNode).should_contain_text("xxx ");
40 value_of(testNode).should_contain_html("xxx <!-- ko text: textprop --><!-- /ko -->");
43 'Should work with virtual elements, removing any existing stuff between the comments': function () {
44 testNode.innerHTML = "xxx <!--ko text: undefined-->some random thing<span> that won't be here later</span><!--/ko-->";
45 ko.applyBindings(null, testNode);
46 value_of(testNode).should_contain_text("xxx ");
47 value_of(testNode).should_contain_html("xxx <!--ko text: undefined--><!--/ko-->");