// ========================================================================== // Project: SproutCore - JavaScript Application Framework // Copyright: ©2006-2009 Sprout Systems, Inc. and contributors. // Portions ©2008-2009 Apple Inc. All rights reserved. // License: Licened under MIT license (see license.js) // ========================================================================== /*globals throws */ var content, controller, extra; var TestObject = SC.Object.extend({ title: "test", toString: function() { return "TestObject(%@)".fmt(this.get("title")); } }); // .......................................................... // EMPTY SET // module("SC.ArrayController - enum_case - EMPTY SET", { setup: function() { content = SC.Set.create(); controller = SC.ArrayController.create({ content: content, orderBy: "title" }); extra = TestObject.create({ title: "FOO" }); }, teardown: function() { controller.destroy(); } }); test("state properties", function() { equals(controller.get("hasContent"), YES, 'c.hasContent'); equals(controller.get("canRemoveContent"), YES, "c.canRemoveContent"); equals(controller.get("canReorderContent"), NO, "c.canReorderContent"); equals(controller.get("canAddContent"), YES, "c.canAddContent"); }); // addObject should append to end of array + notify observers on Array itself test("addObject", function() { var callCount = 0; controller.addObserver('[]', function() { callCount++; }); SC.run(function() { controller.addObject(extra); }); var expected = SC.Set.create().add(extra); same(content, expected, 'addObject(extra) should work'); equals(callCount, 1, 'should notify observer that content has changed'); equals(content.get('length'), 1, 'should update length of controller'); }); test("removeObject", function() { var callCount = 0; controller.addObserver('[]', function() { callCount++; }); SC.run(function() { controller.removeObject(extra); }); same(content, SC.Set.create(), 'removeObject(extra) should have no effect'); equals(callCount, 0, 'should not notify observer since content did not change'); }); test("basic array READ operations", function() { equals(controller.get("length"), 0, 'length should be empty'); equals(controller.objectAt(0), undefined, "objectAt() should return undefined"); }); test("basic array WRITE operations", function() { var callCount = 0; controller.addObserver('[]', function() { callCount++; }); should_throw(function() { controller.replace(0,1,[extra]); }, Error, "calling replace on an enumerable should throw"); }); test("arrangedObjects", function() { equals(controller.get("arrangedObjects"), controller, 'c.arrangedObjects should return receiver'); }); // .......................................................... // NON-EMPTY SET // module("SC.ArrayController - enum_case - NON-EMPTY SET", { setup: function() { content = SC.Set.create(); "1 2 3 4 5".w().forEach(function(x) { content.add(TestObject.create({ title: x })); }); controller = SC.ArrayController.create({ content: content, orderBy: "title" }); extra = TestObject.create({ title: "FOO" }); }, teardown: function() { controller.destroy(); } }); test("state properties", function() { equals(controller.get("hasContent"), YES, 'c.hasContent'); equals(controller.get("canRemoveContent"), YES, "c.canRemoveContent"); equals(controller.get("canReorderContent"), NO, "c.canReorderContent"); equals(controller.get("canAddContent"), YES, "c.canAddContent"); }); // addObject should regenerate ordered + notify observers on Array itself test("addObject", function() { var expected = content.copy(); expected.add(extra); var callCount = 0; controller.addObserver('[]', function() { callCount++; }); SC.run(function() { controller.addObject(extra); }); same(content, expected, 'addObject(extra) should work'); equals(callCount, 1, 'should notify observer that content has changed'); equals(content.get('length'), expected.length, 'should update length of controller'); var idx, len = controller.get('length'); expected = SC.A(expected).sort(function(a,b) { return SC.compare(a.get('title'), b.get('title')); }); for(idx=0;idx=0) { equals(controller.objectAt(loc), expected[loc], "objectAt(%@) should return same value at content[%@]".fmt(loc, loc)); } }); test("basic array WRITE operations", function() { var callCount = 0; controller.addObserver('[]', function() { callCount++; }); should_throw(function() { controller.replace(0,1,[extra]); }, Error, "calling replace on an enumerable should throw"); }); test("arrangedObjects", function() { equals(controller.get("arrangedObjects"), controller, 'c.arrangedObjects should return receiver'); }); // .......................................................... // ADD SPECIAL CASES HERE //