Skip to content

Instantly share code, notes, and snippets.

@wagenet
Created October 8, 2009 22:41
Show Gist options
  • Save wagenet/205488 to your computer and use it in GitHub Desktop.
Save wagenet/205488 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2009 Apple Inc. and contributors.
// License: Licened under MIT license (see license.js)
// ==========================================================================
/*globals module ok equals same test MyApp */
var store, storeKey, json, hash, hash2;
module("SC.Store#createRecord", {
setup: function() {
MyRecordType = SC.Record.extend({
string: SC.Record.attr(String, { defaultValue: "Untitled" }),
number: SC.Record.attr(Number, { defaultValue: 5 }),
bool: SC.Record.attr(Boolean, { defaultValue: YES })
});
store = SC.Store.create();
json = {
string: "string",
number: 23,
bool: YES
};
storeKey = SC.Store.generateStoreKey();
store.writeDataHash(storeKey, json, SC.Record.READY_CLEAN);
}
});
test("create a record", function() {
SC.RunLoop.begin();
var sk;
var rec = SC.Record.create();
hash = {
guid: "1234abcd",
string: "abcd",
number: 1,
bool: NO
};
hash2 = {
string: "abcd",
number: 1,
bool: NO
};
rec = store.createRecord(SC.Record, hash);
ok(rec, "a record was created");
sk=store.storeKeyFor(SC.Record, rec.id());
equals(store.readDataHash(sk), hash, "data hashes are equivalent");
equals(rec.id(), "1234abcd", "id methods matches guid");
equals(rec.get('guid'), "1234abcd", "guids property matches guid");
rec = store.createRecord(SC.Record, hash2, "priKey");
ok(rec, "a record with a custom id was created");
sk=store.storeKeyFor(SC.Record, "priKey");
equals(store.readDataHash(sk), hash2, "data hashes are equivalent");
equals(rec.id(), "priKey", "id methods matches guid");
equals(rec.get('guid'), "priKey", "guids property matches guid");
equals(store.changelog.length, 2, "The changelog has the following number of entries:");
SC.RunLoop.end();
});
test("Creating an empty (null) record should make the hash available", function() {
SC.RunLoop.begin();
store.createRecord(MyRecordType, null, 'guid8');
var storeKey = store.storeKeyFor(MyRecordType, 'guid8');
ok(store.readDataHash(storeKey), 'data hash should not be empty/undefined');
SC.RunLoop.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment