aboutsummaryrefslogtreecommitdiffstats
path: root/test/moveToAngularCom/ModelTest.js
blob: dbd9777848902619b5a5d25d2cd223ad548001a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
ModelTest = TestCase('ModelTest');

ModelTest.prototype.testLoadSaveOperations = function(){
  var m1 = new DataStore().entity('A')();
  m1.a = 1;

  var m2 =  {b:1};

  m1.$loadFrom(m2);

  assertTrue(!m1.a);
  assertEquals(m1.b, 1);
};

ModelTest.prototype.testLoadfromDoesNotClobberFunctions = function(){
  var m1 = new DataStore().entity('A')();
  m1.id = function(){return 'OK';};
  m1.$loadFrom({id:null});
  assertEquals(m1.id(), 'OK');

  m1.b = 'OK';
  m1.$loadFrom({b:function(){}});
  assertEquals(m1.b, 'OK');
};

ModelTest.prototype.testDataStoreDoesNotGetClobbered = function(){
  var ds = new DataStore();
  var m = ds.entity('A')();
  assertTrue(m.$$entity.datastore === ds);
  m.$loadFrom({});
  assertTrue(m.$$entity.datastore === ds);
};

ModelTest.prototype.testManagedModelDelegatesMethodsToDataStore = function(){
  expectAsserts(7);
  var datastore = new DataStore();
  var model = datastore.entity("A", {a:1})();
  var fn = {};
  datastore.save = function(instance, callback) {
    assertTrue(model === instance);
    assertTrue(callback === fn);
  };
  datastore.remove = function(instance, callback) {
    assertTrue(model === instance);
    assertTrue(callback === fn);
  };
  datastore.load = function(instance, id, callback) {
    assertTrue(model === instance);
    assertTrue(id === "123");
    assertTrue(callback === fn);
  };
  model.$save(fn);
  model.$delete(fn);
  model.$loadById("123", fn);
};

ModelTest.prototype.testManagedModelCanBeForcedToFlush = function(){
  expectAsserts(6);
  var datastore = new DataStore();
  var model = datastore.entity("A", {a:1})();

  datastore.save = function(instance, callback) {
    assertTrue(model === instance);
    assertTrue(callback === undefined);
  };
  datastore.remove = function(instance, callback) {
    assertTrue(model === instance);
    assertTrue(callback === undefined);
  };
  datastore.flush = function(){
    assertTrue(true);
  };
  model.$save(true);
  model.$delete(true);
};


ModelTest.prototype.testItShouldMakeDeepCopyOfInitialValues = function (){
  var initial = {a:[]};
  var entity = new DataStore().entity("A", initial);
  var model = entity();
  model.a.push(1);
  assertEquals(0, entity().a.length);
};