I’m writing a suite of knockout bindings that I commonly use in my projects, its grown big and I need tests. So I tried doing it with Qunit, heres a basic test
module("Message binding");
test("When updating message observable with a splash message ", function () {
var div = document.createElement("div");
var message = ko.observable();
ko.applyBindingsToNode(div, { message: message });
message({ splash: "Test" });
});
edit: Before anyone adds a lame comment 😀 I know that there is no Assert yet, it will come when I get the binding to behave like in a live environment
When i apply the binding it fires the update method of the binding with a null value (Which is correct since the observable has a null value)
But when I set the message observable message({ splash: “Test” }); the update method does not fire again. I could offcourse call the init, and update methods myself, but then I’m not using the KO observable API and the tests will be a bit contra productive
Knockout will dispose of the computed observable used to track dependencies for an element’s bindings, if it detects that the element is not part of the document.
So, you would want to append your element to the document before applying bindings. Then, you can remove it after your test has completed.