Skip to content

Commit

Permalink
datastore/data_source: added more tests; fixed additional bug with re…
Browse files Browse the repository at this point in the history
…turn status of commitRecords
  • Loading branch information
Tim Evans authored and wagenet committed May 12, 2011
1 parent 462b2f9 commit e27e7a1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frameworks/datastore/data_sources/data_source.js
Expand Up @@ -456,7 +456,7 @@ SC.DataSource = SC.Object.extend( /** @scope SC.DataSource.prototype */ {
ret = (cur === NO) ? NO : SC.MIXED_STATE ;
}
}
return ret ? ret : null ;
return !SC.none(ret) ? ret : null ;
},


Expand Down
109 changes: 107 additions & 2 deletions frameworks/datastore/tests/data_sources/data_source.js
Expand Up @@ -9,7 +9,6 @@ var MyApp, wasCalled;
module("SC.DataSource", {
setup: function () {
MyApp = window.MyApp = {};

MyApp.store = SC.Store.create();
MyApp.Foo = SC.Record.extend();

Expand Down Expand Up @@ -44,6 +43,11 @@ module("SC.DataSource", {
return YES;
}
});
SC.RunLoop.begin();
},

teardown: function () {
SC.RunLoop.end();
}
});

Expand All @@ -54,7 +58,7 @@ test("The dataSource will forward calls to the appropriate methods", function ()
"the fetch should return a record array");
ok(wasCalled, "`fetch` should have been called");
wasCalled = NO;

ok(MyApp.store.find(MyApp.Foo, "testing retrieve"),
"retrieve should return a new record (because the dataSource handled the request YES)");
ok(wasCalled, "`retrieve` should have been called");
Expand Down Expand Up @@ -83,3 +87,104 @@ test("The dataSource will forward calls to the appropriate methods", function ()
"destroying the record should return YES");
ok(wasCalled, "`destroyRecord` should have been called");
});

test("The dataSource will return YES when all records committed return YES", function () {
var ds = MyApp.DataSource.create({
createRecord: function () { return YES; },
updateRecord: function () { return YES; },
destroyRecord: function () { return YES; }
});

MyApp.store.set('dataSource', ds);

var rec1 = MyApp.store.createRecord(MyApp.Foo, {}),
rec2, rec3;

equals(MyApp.store.commitRecords(), YES,
"commiting a single new record should return YES");

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);

rec1.set('zero', 0);
rec2 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), YES,
"commiting records for an 'update' and 'create' should return YES");

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);
MyApp.store.writeStatus(rec2.get('storeKey'), SC.Record.READY_CLEAN);

rec1.destroy();
rec2.set('one', 1);
rec3 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), YES,
"commiting records for an 'update', 'create', and 'destroy' should return YES");
});

test("The dataSource will return SC.MIXED_STATE when all records committed return YES and NO", function () {
var ds = MyApp.DataSource.create({
createRecord: function () { return NO; },
updateRecord: function () { return YES; },
destroyRecord: function () { return NO; }
});

MyApp.store.set('dataSource', ds);

var rec1 = MyApp.store.createRecord(MyApp.Foo, {}),
rec2, rec3;

equals(MyApp.store.commitRecords(), NO,
"commiting a single new record should return NO");

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);

rec1.set('zero', 0);
rec2 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), SC.MIXED_STATE,
"commiting records for an 'update' and 'create' should return %@".fmt(SC.MIXED_STATE));

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);
MyApp.store.writeStatus(rec2.get('storeKey'), SC.Record.READY_CLEAN);

rec1.destroy();
rec2.set('one', 1);
rec3 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), SC.MIXED_STATE,
"commiting records for an 'update', 'create', and 'destroy' should return %@".fmt(SC.MIXED_STATE));
});

test("The dataSource will return NO when all records committed return NO", function () {
var ds = MyApp.DataSource.create({
createRecord: function () { return NO; },
updateRecord: function () { return NO; },
destroyRecord: function () { return NO; }
});
MyApp.store.set('dataSource', ds);

var rec1 = MyApp.store.createRecord(MyApp.Foo, {}),
rec2, rec3;

equals(MyApp.store.commitRecords(), NO,
"commiting a single new record should return NO");

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);

rec1.set('zero', 0);
rec2 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), NO,
"commiting records for an 'update' and 'create' should return NO");

MyApp.store.writeStatus(rec1.get('storeKey'), SC.Record.READY_CLEAN);
MyApp.store.writeStatus(rec2.get('storeKey'), SC.Record.READY_CLEAN);

rec1.destroy();
rec2.set('one', 1);
rec3 = MyApp.store.createRecord(MyApp.Foo, {});

equals(MyApp.store.commitRecords(), NO,
"commiting records for an 'update', 'create', and 'destroy' should return NO");
});

0 comments on commit e27e7a1

Please sign in to comment.