From 13c63bf2ce6a117ab5f36b0d70fc6c9e1dbc856a Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 11 Feb 2011 10:23:42 -0500 Subject: [PATCH] fixed white space and requested changes added unit test --- popcorn.js | 22 +++++++++++++++++++--- test/popcorn.unit.js | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/popcorn.js b/popcorn.js index 2e80729..1d07986 100644 --- a/popcorn.js +++ b/popcorn.js @@ -24,19 +24,35 @@ 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]]; }; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a1f131e..6b72db7 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -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 () { -- 2.43.0