Skip to content

Instantly share code, notes, and snippets.

@wtarr
Created August 17, 2013 17:22
Show Gist options
  • Save wtarr/6257879 to your computer and use it in GitHub Desktop.
Save wtarr/6257879 to your computer and use it in GitHub Desktop.
Qunit test of ko observable array after a (mocked) ajax call
module("module", {
setup: function () {
$.mockjax( {
url: "*",
responseTime: 1,
dataType: 'text/json',
responseText: [{'name' : 'wally'},{ "name":"molly" }]
});
},
teardown: function () {
$.mockjaxClear();
}
});
test("Test ko observable array after ajax call", function(){
var impl = new MockClass();
ko.applyBindings(impl);
impl.add([{'name' : 'wally'}]);
stop();
impl.get();
setTimeout(function()
{
ok(impl.collect().length == 2);
start();
}, 100);
});
var MockClass = function(){
var self = this;
self.collect = ko.observableArray([]);
self.add = function(data){
self.collect.push(data);
};
self.get = function()
{
$.getJSON( 'goingnowhere', function(data){
self.add(data);
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment