]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
fixed white space and requested changes
authorunknown <Venom@.(none)>
Fri, 11 Feb 2011 15:23:42 +0000 (10:23 -0500)
committerunknown <Venom@.(none)>
Fri, 11 Feb 2011 15:23:42 +0000 (10:23 -0500)
added unit test

popcorn.js
test/popcorn.unit.js

index 2e80729f16837618464f21f5c419fcf190a2a030..1d07986bcd107c89facadaec5f429092e8dd73a1 100644 (file)
   
   Popcorn.instances = [];
   Popcorn.instanceIds = {};
+  
   Popcorn.removeInstance = function(id) {
-    Popcorn.instances.splice(Popcorn.instanceIds[id], 1);
-    delete Popcorn.instanceIds[id];
+    //  If called prior to any instances being created
+    //  Return early to avoid splicing on nothing
+    if ( !Popcorn.instances.length ) {
+      return;
+    }
+  
+    Popcorn.instances.splice( Popcorn.instanceIds[ id ], 1 );
+
+    delete Popcorn.instanceIds[ id ];
+    
   };
 
+  //addes a Popcorn instance to the Popcorn instance array
   Popcorn.addInstance = function(popcornInstance) {
-    if (popcornInstance.video.id === "undefined" || !popcornInstance.video.id.length) {
+    if (!popcornInstance.video.id ) { 
+    
       popcornInstance.video.id = "__popcorn" + Popcorn.instances.length;
+    
     }
+    
     Popcorn.instanceIds[popcornInstance.video.id] = Popcorn.instances.length;
+    
     Popcorn.instances.push(popcornInstance);
+    
   };
 
+  //User passes in the name of the Popcorn instance and receive a popcorn object
   Popcorn.getInstanceById = function(name) {
     return Popcorn.instances[Popcorn.instanceIds[name]];
   };
index a1f131ea274d08ad701c164ad9654b81cea24ace..6b72db79bdd3f57a0d585ab42b856f7e6b83c04f 100644 (file)
@@ -68,6 +68,28 @@ test("Utility", function () {
   
 });
 
+test("Instances", function() {
+  expect(8);
+  
+  equals( typeof Popcorn.addInstance, "function" , "Popcorn.addInstance is a provided utility function");
+  equals( typeof Popcorn.removeInstance, "function" , "Popcorn.removeInstance is a provided utility function");
+  equals( typeof Popcorn.getInstanceById, "function" , "Popcorn.getInstanceById is a provided utility function");
+  equals( typeof Popcorn.instanceIds, "object" , "Popcorn.instanceIds is a provided cache object");
+  ok( "length" in Popcorn.instances && "join" in Popcorn.instances, "Popcorn.error is a provided cache array");  
+  
+  var instance;
+  
+  for ( var prop in Popcorn.instanceIds ) {
+    
+    instance = Popcorn.getInstanceById( prop );
+    
+    ok( instance.video, "Stored instance as a `video` property" );
+    ok( instance.data, "Stored instance as a `data` property" );
+    ok( instance instanceof Popcorn, "instance instanceof Popcorn" );
+  
+  }
+
+});
 
 test("guid", function () {