Просмотр исходного кода

Improved App.Collection.loadAssets()

Martin Edenhofer 10 лет назад
Родитель
Сommit
25516c19f6

+ 3 - 2
app/assets/javascripts/app/lib/app_post/collection.js.coffee

@@ -119,8 +119,9 @@ class _collectionSingleton extends Spine.Module
         # check if new object is newer, just load newer objects
         if object.updated_at && appObject.exists( key )
           exists = appObject.find( key )
-          if exists.updated_at && exists.updated_at isnt object.updated_at
-            appObject.refresh( object )
+          if exists.updated_at
+            if exists.updated_at < object.updated_at
+              appObject.refresh( object )
           else
             appObject.refresh( object )
         else

+ 142 - 0
public/assets/tests/model.js

@@ -1,3 +1,5 @@
+window.onload = function() {
+
 // model
 test( "model basic tests", function() {
 
@@ -198,3 +200,143 @@ test( "model basic tests", function() {
   ok( !error['test2'], 'test2 is not required')
 
 });
+
+// model
+test( "model loadAssets tests - 1", function() {
+  window.refreshCounter1 = 0
+  var callback1 = function(state) {
+    window.refreshCounter1 = window.refreshCounter1 + 1
+    equal( state.id, 9999, 'id check')
+
+    if ( window.refreshCounter1 == 1 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          9999: {
+            name: 'some some name', id: 9999, updated_at: "2014-11-07T23:43:08.000Z"
+          }
+        }
+      })
+
+    }
+    if ( window.refreshCounter1 == 2 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          9999: {
+            name: 'some some name', id: 9999, updated_at: "2014-11-07T23:43:08.000Z"
+          }
+        }
+      })
+
+    }
+  }
+  App.Collection.loadAssets({
+    TicketState: {
+      9999: {
+        name: 'some some name', id: 9999, updated_at: "2014-11-06T23:43:08.000Z"
+      }
+    }
+  })
+
+  App.TicketState.full(9999, callback1, false, true)
+
+});
+
+App.Delay.set( function() {
+    test( "model loadAssets tests - 1 / check refresh counter", function() {
+      equal( window.refreshCounter1, 2, 'check refresh counter')
+    });
+  },
+  1000
+);
+
+test( "model loadAssets tests - 2", function() {
+  window.refreshCounter2 = 0
+  var callback2 = function(state) {
+    window.refreshCounter2 = window.refreshCounter2 + 1
+    equal( state.id, 10000, 'id check')
+
+    if ( window.refreshCounter2 == 1 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          10000: {
+            name: 'some some name', id: 10000, updated_at: "2014-11-07T23:43:08.000Z"
+          }
+        }
+      })
+    }
+    if ( window.refreshCounter2 == 2 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          10000: {
+            name: 'some some name', id: 10000, updated_at: "2014-11-05T23:43:08.000Z"
+          }
+        }
+      })
+    }
+  }
+  App.Collection.loadAssets({
+    TicketState: {
+      10000: {
+        name: 'some some name', id: 10000, updated_at: "2014-11-06T23:43:08.000Z"
+      }
+    }
+  })
+
+  App.TicketState.full(10000, callback2, false, true)
+
+});
+
+App.Delay.set( function() {
+    test( "model loadAssets tests - 2 / check refresh counter", function() {
+      equal( window.refreshCounter2, 2, 'check refresh counter')
+    });
+  },
+  1200
+);
+
+test( "model loadAssets tests - 3", function() {
+  window.refreshCounter3 = 0
+  var callback3 = function(state) {
+    window.refreshCounter3 = window.refreshCounter3 + 1
+    equal( state.id, 10001, 'id check')
+
+    if ( window.refreshCounter3 == 1 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          10001: {
+            name: 'some some name', id: 10001, updated_at: "2014-11-07T23:43:08.000Z"
+          }
+        }
+      })
+    }
+    if ( window.refreshCounter3 == 2 ) {
+      App.Collection.loadAssets({
+        TicketState: {
+          10001: {
+            name: 'some some name', id: 10001, updated_at: "2014-11-08T23:43:08.000Z"
+          }
+        }
+      })
+    }
+  }
+  App.Collection.loadAssets({
+    TicketState: {
+      10001: {
+        name: 'some some name', id: 10001, updated_at: "2014-11-06T23:43:08.000Z"
+      }
+    }
+  })
+
+  App.TicketState.full(10001, callback3, false, true)
+
+});
+
+App.Delay.set( function() {
+    test( "model loadAssets tests - 3 / check refresh counter", function() {
+      equal( window.refreshCounter3, 3, 'check refresh counter')
+    });
+  },
+  1400
+);
+
+}