Best JavaScript code snippet using storybook-root
CompositeEntityCollectionSpec.js
Source:CompositeEntityCollectionSpec.js
1defineSuite([2 'DataSources/CompositeEntityCollection',3 'Core/Iso8601',4 'Core/JulianDate',5 'Core/TimeInterval',6 'Core/TimeIntervalCollection',7 'DataSources/BillboardGraphics',8 'DataSources/CompositePositionProperty',9 'DataSources/CompositeProperty',10 'DataSources/ConstantProperty',11 'DataSources/Entity',12 'DataSources/EntityCollection'13 ], function(14 CompositeEntityCollection,15 Iso8601,16 JulianDate,17 TimeInterval,18 TimeIntervalCollection,19 BillboardGraphics,20 CompositePositionProperty,21 CompositeProperty,22 ConstantProperty,23 Entity,24 EntityCollection) {25 'use strict';2627 function CollectionListener() {28 this.timesCalled = 0;29 this.added = [];30 this.removed = [];31 }32 CollectionListener.prototype.onCollectionChanged = function(collection, added, removed) {33 this.timesCalled++;34 this.added = added.slice(0);35 this.removed = removed.slice(0);36 };3738 it('constructor has expected defaults', function() {39 var composite = new CompositeEntityCollection();40 expect(composite.collectionChanged).toBeDefined();41 expect(composite.getCollectionsLength()).toEqual(0);42 expect(composite.values.length).toEqual(0);43 });4445 it('constructor with owner', function() {46 var composite = new CompositeEntityCollection();47 var child = new CompositeEntityCollection(undefined, composite);4849 expect(child.owner).toEqual(composite);50 });5152 it('addCollection/removeCollection works', function() {53 var entityCollection = new EntityCollection();54 entityCollection.add(new Entity());5556 var entityCollection2 = new EntityCollection();57 entityCollection2.add(new Entity());5859 var composite = new CompositeEntityCollection();60 composite.addCollection(entityCollection);61 expect(composite.getCollectionsLength()).toEqual(1);62 expect(composite.values.length).toEqual(1);6364 composite.addCollection(entityCollection2);65 expect(composite.getCollectionsLength()).toEqual(2);66 expect(composite.values.length).toEqual(2);6768 expect(composite.removeCollection(entityCollection)).toEqual(true);69 expect(composite.values.length).toEqual(1);7071 expect(composite.removeCollection(entityCollection2)).toEqual(true);72 expect(composite.values.length).toEqual(0);73 expect(composite.getCollectionsLength()).toEqual(0);7475 expect(composite.removeCollection(entityCollection)).toEqual(false);76 });7778 it('addCollection works with index', function() {79 var entityCollection = new EntityCollection();80 var entityCollection2 = new EntityCollection();81 var entityCollection3 = new EntityCollection();8283 var composite = new CompositeEntityCollection();84 composite.addCollection(entityCollection);85 composite.addCollection(entityCollection3);8687 composite.addCollection(entityCollection2, 1);88 expect(composite.getCollection(0)).toBe(entityCollection);89 expect(composite.getCollection(1)).toBe(entityCollection2);90 expect(composite.getCollection(2)).toBe(entityCollection3);91 });9293 it('contains returns true if in collection', function() {94 var entityCollection = new EntityCollection();95 var composite = new CompositeEntityCollection();96 composite.addCollection(entityCollection);97 var entity = entityCollection.getOrCreateEntity('asd');98 expect(entityCollection.contains(entity)).toBe(true);99 });100101 it('contains returns false if not in collection', function() {102 var entityCollection = new CompositeEntityCollection();103 expect(entityCollection.contains(new Entity())).toBe(false);104 });105106 it('contains throws with undefined Entity', function() {107 var entityCollection = new CompositeEntityCollection();108 expect(function() {109 entityCollection.contains(undefined);110 }).toThrowDeveloperError();111 });112113 it('containsCollection works', function() {114 var entityCollection = new EntityCollection();115 var composite = new CompositeEntityCollection();116117 expect(composite.containsCollection(entityCollection)).toEqual(false);118 composite.addCollection(entityCollection);119 expect(composite.containsCollection(entityCollection)).toEqual(true);120 });121122 it('indexOfCollection works', function() {123 var entityCollection = new EntityCollection();124 var entityCollection2 = new EntityCollection();125 var composite = new CompositeEntityCollection();126127 expect(composite.indexOfCollection(entityCollection)).toEqual(-1);128129 composite.addCollection(entityCollection);130 composite.addCollection(entityCollection2);131132 expect(composite.indexOfCollection(entityCollection)).toEqual(0);133 expect(composite.indexOfCollection(entityCollection2)).toEqual(1);134135 composite.removeCollection(entityCollection);136137 expect(composite.indexOfCollection(entityCollection2)).toEqual(0);138 });139140 it('getCollection works', function() {141 var entityCollection = new EntityCollection();142 var entityCollection2 = new EntityCollection();143 var composite = new CompositeEntityCollection();144145 composite.addCollection(entityCollection);146 composite.addCollection(entityCollection2);147148 expect(composite.getCollection(0)).toBe(entityCollection);149 expect(composite.getCollection(1)).toBe(entityCollection2);150 expect(composite.getCollection(2)).toBeUndefined();151 });152153 it('raise/lower collection works', function() {154 var entityCollection = new EntityCollection();155 var entityCollection2 = new EntityCollection();156 var entityCollection3 = new EntityCollection();157 var composite = new CompositeEntityCollection();158159 composite.addCollection(entityCollection);160 composite.addCollection(entityCollection2);161 composite.addCollection(entityCollection3);162163 expect(composite.indexOfCollection(entityCollection2)).toEqual(1);164 composite.raiseCollection(entityCollection2);165166 expect(composite.indexOfCollection(entityCollection2)).toEqual(2);167 composite.lowerCollection(entityCollection2);168169 composite.lowerCollection(entityCollection2);170 expect(composite.indexOfCollection(entityCollection2)).toEqual(0);171172 composite.lowerCollection(entityCollection2);173 expect(composite.indexOfCollection(entityCollection2)).toEqual(0);174175 composite.raiseCollectionToTop(entityCollection2);176 expect(composite.indexOfCollection(entityCollection2)).toEqual(2);177178 composite.raiseCollectionToTop(entityCollection2);179 expect(composite.indexOfCollection(entityCollection2)).toEqual(2);180181 composite.lowerCollectionToBottom(entityCollection2);182 expect(composite.indexOfCollection(entityCollection2)).toEqual(0);183184 composite.lowerCollectionToBottom(entityCollection2);185 expect(composite.indexOfCollection(entityCollection2)).toEqual(0);186 });187188 it('add/remove works', function() {189 var entity = new Entity();190 var entity2 = new Entity();191 var entityCollection = new EntityCollection();192193 var composite = new CompositeEntityCollection([entityCollection]);194195 entityCollection.add(entity);196 expect(composite.values.length).toEqual(1);197198 entityCollection.add(entity2);199 expect(composite.values.length).toEqual(2);200201 entityCollection.remove(entity2);202 expect(composite.values.length).toEqual(1);203204 entityCollection.remove(entity);205 expect(composite.values.length).toEqual(0);206 });207208 it('add/remove raises expected events', function() {209 var entity = new Entity();210 var entity2 = new Entity();211 var entityCollection = new EntityCollection();212213 var composite = new CompositeEntityCollection([entityCollection]);214 var listener = new CollectionListener();215 composite.collectionChanged.addEventListener(listener.onCollectionChanged, listener);216217 entityCollection.add(entity);218 expect(listener.timesCalled).toEqual(1);219 expect(listener.added.length).toEqual(1);220 expect(listener.added[0].id).toBe(entity.id);221 expect(listener.removed.length).toEqual(0);222223 entityCollection.add(entity2);224 expect(listener.timesCalled).toEqual(2);225 expect(listener.added.length).toEqual(1);226 expect(listener.added[0].id).toBe(entity2.id);227 expect(listener.removed.length).toEqual(0);228229 entityCollection.remove(entity2);230 expect(listener.timesCalled).toEqual(3);231 expect(listener.added.length).toEqual(0);232 expect(listener.removed.length).toEqual(1);233 expect(listener.removed[0].id).toBe(entity2.id);234235 entityCollection.remove(entity);236 expect(listener.timesCalled).toEqual(4);237 expect(listener.added.length).toEqual(0);238 expect(listener.removed.length).toEqual(1);239 expect(listener.removed[0].id).toBe(entity.id);240241 composite.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);242 });243244 it('suspended add/remove raises expected events', function() {245 var entity = new Entity();246 var entity2 = new Entity();247 var entity3 = new Entity();248249 var entityCollection = new EntityCollection();250251 var composite = new CompositeEntityCollection([entityCollection]);252 var listener = new CollectionListener();253 composite.collectionChanged.addEventListener(listener.onCollectionChanged, listener);254255 composite.suspendEvents();256 composite.suspendEvents();257 entityCollection.add(entity);258 entityCollection.add(entity2);259 entityCollection.add(entity3);260 entityCollection.remove(entity2);261262 expect(listener.timesCalled).toEqual(0);263 composite.resumeEvents();264265 expect(listener.timesCalled).toEqual(0);266 composite.resumeEvents();267268 expect(listener.timesCalled).toEqual(1);269 expect(listener.added.length).toEqual(2);270 expect(listener.added[0].id).toBe(entity.id);271 expect(listener.added[1].id).toBe(entity3.id);272 expect(listener.removed.length).toEqual(0);273274 composite.suspendEvents();275 entityCollection.remove(entity);276 entityCollection.remove(entity3);277 entityCollection.add(entity);278 entityCollection.add(entity3);279 composite.resumeEvents();280281 expect(listener.timesCalled).toEqual(1);282283 composite.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);284 });285286 it('removeAllCollections works', function() {287 var entity = new Entity();288 var entity2 = new Entity();289 var entityCollection = new EntityCollection();290291 var composite = new CompositeEntityCollection([entityCollection]);292293 entityCollection.add(entity);294 entityCollection.add(entity2);295 composite.removeAllCollections();296 expect(composite.values.length).toEqual(0);297 });298299 it('removeAllCollections raises expected events', function() {300 var entity = new Entity();301 var entity2 = new Entity();302 var entityCollection = new EntityCollection();303304 var listener = new CollectionListener();305 var composite = new CompositeEntityCollection([entityCollection]);306307 entityCollection.add(entity);308 entityCollection.add(entity2);309310 composite.collectionChanged.addEventListener(listener.onCollectionChanged, listener);311 composite.removeAllCollections();312313 expect(listener.timesCalled).toEqual(1);314 expect(listener.removed.length).toEqual(2);315 expect(listener.removed[0].id).toBe(entity.id);316 expect(listener.removed[1].id).toBe(entity2.id);317 expect(listener.added.length).toEqual(0);318319 composite.removeAllCollections();320 expect(listener.timesCalled).toEqual(1);321322 composite.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);323 });324325 it('suspended removeAllCollections raises expected events', function() {326 var entity = new Entity();327 var entity2 = new Entity();328 var entityCollection = new EntityCollection();329330 var listener = new CollectionListener();331 var composite = new CompositeEntityCollection([entityCollection]);332333 entityCollection.add(entity);334 entityCollection.add(entity2);335336 composite.collectionChanged.addEventListener(listener.onCollectionChanged, listener);337338 composite.suspendEvents();339 composite.removeAllCollections();340 composite.resumeEvents();341 expect(listener.timesCalled).toEqual(1);342 expect(listener.removed.length).toEqual(2);343 expect(listener.removed[0].id).toBe(entity.id);344 expect(listener.removed[1].id).toBe(entity2.id);345 expect(listener.added.length).toEqual(0);346 expect(composite.getCollectionsLength()).toEqual(0);347348 composite.suspendEvents();349 composite.addCollection(entityCollection);350 entityCollection.removeAll();351 composite.resumeEvents();352 expect(listener.timesCalled).toEqual(1);353354 composite.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);355 });356357 it('getById works', function() {358 var entity = new Entity();359 var entity2 = new Entity();360 var entityCollection = new EntityCollection();361362 entityCollection.add(entity);363 entityCollection.add(entity2);364365 var composite = new CompositeEntityCollection();366 composite.addCollection(entityCollection);367 expect(composite.getById(entity.id).id).toEqual(entity.id);368 expect(composite.getById(entity2.id).id).toEqual(entity2.id);369 });370371 it('getById returns undefined for non-existent object', function() {372 var composite = new CompositeEntityCollection();373 expect(composite.getById('123')).toBeUndefined();374 });375376 it('computeAvailability returns infinite with no data.', function() {377 var entityCollection = new EntityCollection();378 var composite = new CompositeEntityCollection();379 composite.addCollection(entityCollection);380 var availability = composite.computeAvailability();381 expect(availability.start).toEqual(Iso8601.MINIMUM_VALUE);382 expect(availability.stop).toEqual(Iso8601.MAXIMUM_VALUE);383 });384385 it('computeAvailability returns intersection of collections.', function() {386 var entityCollection = new EntityCollection();387388 var entity = entityCollection.getOrCreateEntity('1');389 var entity2 = entityCollection.getOrCreateEntity('2');390 var entity3 = entityCollection.getOrCreateEntity('3');391392 entity.availability = new TimeIntervalCollection();393 entity.availability.addInterval(TimeInterval.fromIso8601({394 iso8601 : '2012-08-01/2012-08-02'395 }));396 entity2.availability = new TimeIntervalCollection();397 entity2.availability.addInterval(TimeInterval.fromIso8601({398 iso8601 : '2012-08-05/2012-08-06'399 }));400 entity3.availability = undefined;401402 var composite = new CompositeEntityCollection();403 composite.addCollection(entityCollection);404 var availability = composite.computeAvailability();405 expect(availability.start).toEqual(JulianDate.fromIso8601('2012-08-01'));406 expect(availability.stop).toEqual(JulianDate.fromIso8601('2012-08-06'));407 });408409 it('computeAvailability works if only start or stop time is infinite.', function() {410 var entityCollection = new EntityCollection();411412 var entity = entityCollection.getOrCreateEntity('1');413 var entity2 = entityCollection.getOrCreateEntity('2');414 var entity3 = entityCollection.getOrCreateEntity('3');415416 entity.availability = new TimeIntervalCollection();417 entity.availability.addInterval(TimeInterval.fromIso8601({418 iso8601 : '2012-08-01/9999-12-31T24:00:00Z'419 }));420 entity2.availability = new TimeIntervalCollection();421 entity2.availability.addInterval(TimeInterval.fromIso8601({422 iso8601 : '0000-01-01T00:00:00Z/2012-08-06'423 }));424 entity3.availability = undefined;425426 var composite = new CompositeEntityCollection();427 composite.addCollection(entityCollection);428 var availability = composite.computeAvailability();429 expect(availability.start).toEqual(JulianDate.fromIso8601('2012-08-01'));430 expect(availability.stop).toEqual(JulianDate.fromIso8601('2012-08-06'));431 });432433 it('coarse property compositing works', function() {434 var composite = new CompositeEntityCollection();435436 var collection1 = new EntityCollection();437 var collection2 = new EntityCollection();438 var collection3 = new EntityCollection();439440 //Add collections in reverse order to lower numbers of priority441 composite.addCollection(collection3);442 composite.addCollection(collection2);443 composite.addCollection(collection1);444445 //Start with an object in the middle with defined position and orientation446 var entity2 = new Entity();447 collection2.add(entity2);448 entity2.position = new CompositePositionProperty();449 entity2.orientation = new CompositeProperty();450451 //Initial composite should match both properties452 var compositeObject = composite.getById(entity2.id);453 expect(compositeObject).toBeDefined();454 expect(composite.values.length).toEqual(1);455 expect(compositeObject.position).toBe(entity2.position);456 expect(compositeObject.orientation).toBe(entity2.orientation);457458 //Add a lower-priority object with position and viewFrom.459 var entity3 = new Entity({460 id : entity2.id461 });462 collection3.add(entity3);463 entity3.position = new CompositePositionProperty();464 entity3.viewFrom = new CompositeProperty();465466 //We keep the orientation and position from higher priority entity2467 //But add the viewFrom from 3.468 expect(composite.values.length).toEqual(1);469 expect(compositeObject.position).toBe(entity2.position);470 expect(compositeObject.orientation).toBe(entity2.orientation);471 expect(compositeObject.viewFrom).toBe(entity3.viewFrom);472473 //Add a higher priority object with position474 var entity1 = new Entity({475 id : entity2.id476 });477 collection1.add(entity1);478 entity1.position = new CompositePositionProperty();479480 //We now use the position from the higher priority481 //object with other properties unchanged.482 expect(composite.values.length).toEqual(1);483 expect(compositeObject.position).toBe(entity1.position);484 expect(compositeObject.orientation).toBe(entity2.orientation);485 expect(compositeObject.viewFrom).toBe(entity3.viewFrom);486 });487488 it('sub-property compositing works', function() {489 var id = 'test';490 var collection1 = new EntityCollection();491 var entity1 = new Entity({492 id : id493 });494 entity1.billboard = new BillboardGraphics();495 collection1.add(entity1);496497 var collection2 = new EntityCollection();498 var entity2 = new Entity({499 id : id500 });501 entity2.billboard = new BillboardGraphics();502 collection2.add(entity2);503504 var collection3 = new EntityCollection();505 var entity3 = new Entity({506 id : id507 });508 entity3.billboard = new BillboardGraphics();509 collection3.add(entity3);510511 //Add collections in reverse order to lower numbers of priority512 var composite = new CompositeEntityCollection();513 composite.addCollection(collection3);514 composite.addCollection(collection2);515 composite.addCollection(collection1);516517 var compositeObject = composite.getById(id);518519 // Start with an object in the middle with defined billboard520 entity2.billboard.show = new CompositeProperty();521 expect(compositeObject.billboard.show).toBe(entity2.billboard.show);522523 entity3.billboard.show = new CompositeProperty();524 expect(compositeObject.billboard.show).toBe(entity2.billboard.show);525526 entity1.billboard.show = new CompositeProperty();527 expect(compositeObject.billboard.show).toBe(entity1.billboard.show);528529 entity2.billboard.show = undefined;530 expect(compositeObject.billboard.show).toBe(entity1.billboard.show);531532 entity1.billboard.show = undefined;533 expect(compositeObject.billboard.show).toBe(entity3.billboard.show);534535 entity3.billboard.show = undefined;536 expect(compositeObject.billboard.show).toBeUndefined();537 });538539 it('works when collection being composited suspends updates', function() {540 var collection = new EntityCollection();541 var composite = new CompositeEntityCollection([collection]);542543 collection.suspendEvents();544 collection.getOrCreateEntity('id1');545 collection.getOrCreateEntity('id2');546547 expect(composite.getById('id1')).toBeUndefined();548 expect(composite.getById('id2')).toBeUndefined();549550 collection.resumeEvents();551552 expect(composite.getById('id1')).toBeDefined();553 expect(composite.getById('id2')).toBeDefined();554 });555556 it('custom entity properties are properly registed on new composited entity.', function() {557 var oldValue = 'tubelcane';558 var newValue = 'fizzbuzz';559 var propertyName = 'customProperty';560561 var collection = new EntityCollection();562 var e1 = collection.getOrCreateEntity('id1');563 e1.addProperty(propertyName);564 e1[propertyName] = oldValue;565566 var composite = new CompositeEntityCollection([collection]);567 var e1Composite = composite.getById('id1');568 expect(e1Composite[propertyName]).toEqual(e1[propertyName]);569570 var listener = jasmine.createSpy('listener');571 e1Composite.definitionChanged.addEventListener(listener);572573 e1[propertyName] = newValue;574 expect(listener).toHaveBeenCalledWith(e1Composite, propertyName, newValue, oldValue);575 });576577 it('custom entity properties are properly registed on existing composited entity.', function() {578 var oldValue = 'tubelcane';579 var newValue = 'fizzbuzz';580 var propertyName = 'customProperty';581582 var collection = new EntityCollection();583 var e1 = collection.getOrCreateEntity('id1');584585 var composite = new CompositeEntityCollection([collection]);586587 e1.addProperty(propertyName);588 e1[propertyName] = oldValue;589590 var e1Composite = composite.getById('id1');591 expect(e1Composite[propertyName]).toEqual(e1[propertyName]);592593 var listener = jasmine.createSpy('listener');594 e1Composite.definitionChanged.addEventListener(listener);595596 e1[propertyName] = newValue;597 expect(listener).toHaveBeenCalledWith(e1Composite, propertyName, newValue, oldValue);598 });599600 it('can use the same entity collection in multiple composites', function() {601 var id = 'test';602603 // the entity in collection1 has show === true604 var collection1 = new EntityCollection();605 var entity1 = new Entity({606 id : id607 });608 entity1.billboard = new BillboardGraphics();609 entity1.billboard.show = new ConstantProperty(true);610 collection1.add(entity1);611612 // the entity in collection1 has show === false613 var collection2 = new EntityCollection();614 var entity2 = new Entity({615 id : id616 });617 entity2.billboard = new BillboardGraphics();618 entity2.billboard.show = new ConstantProperty(false);619 collection2.add(entity2);620621 // composite1 has collection1 as higher priority622 var composite1 = new CompositeEntityCollection();623 composite1.addCollection(collection2);624 composite1.addCollection(collection1);625626 // composite2 has collection2 as higher priority627 var composite2 = new CompositeEntityCollection();628 composite2.addCollection(collection1);629 composite2.addCollection(collection2);630631 expect(composite1.getById(id).billboard.show.getValue(JulianDate.now())).toEqual(true);632 expect(composite2.getById(id).billboard.show.getValue(JulianDate.now())).toEqual(false);633634 // switch the billboard show for the entity in collection2 to true, this should affect635 // composite2 but not composite1636 entity2.billboard.show = new ConstantProperty(true);637 expect(composite2.getById(id).billboard.show.getValue(JulianDate.now())).toEqual(true);638 expect(composite1.getById(id).billboard.show).toBe(entity1.billboard.show);639 expect(composite2.getById(id).billboard.show).toBe(entity2.billboard.show);640641 // add a position property to the entity in collection1642 entity1.position = new CompositePositionProperty();643644 // both composites should use the position from the object in collection1645 expect(composite1.getById(id).position).toBe(entity1.position);646 expect(composite2.getById(id).position).toBe(entity1.position);647648 // add a position property to the entity in collection1649 entity2.position = new CompositePositionProperty();650651 // composite1 should use the position from the object in collection1652 // composite2 should use the position from the object in collection2653 expect(composite1.getById(id).position).toBe(entity1.position);654 expect(composite2.getById(id).position).toBe(entity2.position);655 });656657 it('has entity with link to entity collection', function() {658 var id = 'test';659 var collection = new EntityCollection();660 var entity = new Entity({661 id : id662 });663 collection.add(entity);664 var composite = new CompositeEntityCollection();665 composite.addCollection(collection);666 var compositeEntity = composite.getCollection(0).values[0];667 expect(compositeEntity.entityCollection).toEqual(collection);668 });669670 it('suspend events suspends recompositing', function() {671 var id = 'test';672 var collection1 = new EntityCollection();673 var entity1 = new Entity({674 id : id675 });676 collection1.add(entity1);677678 var collection2 = new EntityCollection();679 var entity2 = new Entity({680 id : id681 });682 collection2.add(entity2);683 //Add collections in reverse order to lower numbers of priority684 var composite = new CompositeEntityCollection();685 composite.addCollection(collection2);686687 // suspend events688 composite.suspendEvents();689 composite.addCollection(collection1);690691 // add a billboard692 var compositeObject = composite.getById(id);693 entity1.billboard = new BillboardGraphics();694 entity1.billboard.show = new ConstantProperty(false);695 // should be undefined because we haven't recomposited696 expect(compositeObject.billboard).toBeUndefined();697 // resume events698 composite.resumeEvents();699700 expect(compositeObject.billboard.show).toBe(entity1.billboard.show);701 });702703 it('prevents names from colliding between property events and object events', function() {704 var id = 'test';705 var collection1 = new EntityCollection();706 var entity1 = new Entity({707 id : id708 });709 collection1.add(entity1);710711 var collection2 = new EntityCollection();712 var entity2 = new Entity({713 id : id714 });715 collection2.add(entity2);716717 //Add collections in reverse order to lower numbers of priority718 var composite = new CompositeEntityCollection();719 composite.addCollection(collection2);720 composite.addCollection(collection1);721722 var compositeObject = composite.getById(id);723724 // Add a billboard725 entity1.billboard = new BillboardGraphics();726 entity1.billboard.show = new ConstantProperty(false);727728 expect(compositeObject.billboard.show).toBe(entity1.billboard.show);729730 // Add a new object731 var newObject = new Entity({732 id : id + 'billboard'733 });734 collection1.add(newObject);735736 // Replace the billboard on the original object737 entity1.billboard = new BillboardGraphics();738 entity1.billboard.show = new ConstantProperty(false);739740 // Add a property to the new object741 newObject.position = new CompositePositionProperty();742743 // It should appear on the composite744 expect(composite.getById(newObject.id).position).toBe(newObject.position);745 });746747 it('addCollection throws with undefined collection', function() {748 var composite = new CompositeEntityCollection();749 expect(function() {750 composite.addCollection(undefined);751 }).toThrowDeveloperError();752 });753754 it('addCollection throws if negative index', function() {755 var collection = new EntityCollection();756 var composite = new CompositeEntityCollection();757 expect(function() {758 composite.addCollection(collection, -1);759 }).toThrowDeveloperError();760 });761762 it('addCollection throws if index greater than length', function() {763 var collection = new EntityCollection();764 var composite = new CompositeEntityCollection();765 expect(function() {766 composite.addCollection(collection, 1);767 }).toThrowDeveloperError();768 });769770 it('getCollection throws with undefined index', function() {771 var composite = new CompositeEntityCollection();772 expect(function() {773 composite.getCollection(undefined);774 }).toThrowDeveloperError();775 });776777 it('raiseCollection throws if collection not in composite', function() {778 var composite = new CompositeEntityCollection();779 var collection = new EntityCollection();780 expect(function() {781 composite.raiseCollection(collection);782 }).toThrowDeveloperError();783 });784785 it('raiseCollectionToTop throws if collection not in composite', function() {786 var composite = new CompositeEntityCollection();787 var collection = new EntityCollection();788 expect(function() {789 composite.raiseCollectionToTop(collection);790 }).toThrowDeveloperError();791 });792793 it('lowerCollection throws if collection not in composite', function() {794 var composite = new CompositeEntityCollection();795 var collection = new EntityCollection();796 expect(function() {797 composite.lowerCollection(collection);798 }).toThrowDeveloperError();799 });800801 it('lowerCollectionToBottom throws if collection not in composite', function() {802 var composite = new CompositeEntityCollection();803 var collection = new EntityCollection();804 expect(function() {805 composite.lowerCollectionToBottom(collection);806 }).toThrowDeveloperError();807 });808809 it('raiseCollection throws if collection not defined', function() {810 var composite = new CompositeEntityCollection();811 expect(function() {812 composite.raiseCollection(undefined);813 }).toThrowDeveloperError();814 });815816 it('raiseCollectionToTop throws if collection not defined', function() {817 var composite = new CompositeEntityCollection();818 expect(function() {819 composite.raiseCollectionToTop(undefined);820 }).toThrowDeveloperError();821 });822823 it('lowerCollection throws if collection not defined', function() {824 var composite = new CompositeEntityCollection();825 expect(function() {826 composite.lowerCollection(undefined);827 }).toThrowDeveloperError();828 });829830 it('lowerCollectionToBottom throws if collection not defined', function() {831 var composite = new CompositeEntityCollection();832 expect(function() {833 composite.lowerCollectionToBottom(undefined);834 }).toThrowDeveloperError();835 });836837 it('resumeEvents throws if no matching suspendEvents', function() {838 var composite = new CompositeEntityCollection();839 expect(function() {840 composite.resumeEvents();841 }).toThrowDeveloperError();842 });843844 it('getById throws if no id specified', function() {845 var composite = new CompositeEntityCollection();846 expect(function() {847 composite.getById(undefined);848 }).toThrowDeveloperError();849 });
...
EntityCollectionSpec.js
Source:EntityCollectionSpec.js
1defineSuite([2 'DataSources/EntityCollection',3 'Core/Iso8601',4 'Core/JulianDate',5 'Core/TimeInterval',6 'Core/TimeIntervalCollection',7 'DataSources/Entity'8 ], function(9 EntityCollection,10 Iso8601,11 JulianDate,12 TimeInterval,13 TimeIntervalCollection,14 Entity) {15 'use strict';1617 function CollectionListener() {18 this.timesCalled = 0;19 this.added = undefined;20 this.removed = undefined;21 this.changed = undefined;22 }23 CollectionListener.prototype.onCollectionChanged = function(collection, added, removed, changed) {24 this.timesCalled++;25 this.added = added.slice(0);26 this.removed = removed.slice(0);27 this.changed = changed.slice(0);28 };2930 it('constructor has expected defaults', function() {31 var entityCollection = new EntityCollection();32 expect(entityCollection.id).toBeDefined();33 expect(entityCollection.values.length).toEqual(0);34 });3536 it('add/remove works', function() {37 var entity = new Entity();38 var entity2 = new Entity();39 var entityCollection = new EntityCollection();4041 entityCollection.add(entity);42 expect(entityCollection.values.length).toEqual(1);4344 entityCollection.add(entity2);45 expect(entityCollection.values.length).toEqual(2);4647 entityCollection.remove(entity2);48 expect(entityCollection.values.length).toEqual(1);4950 entityCollection.remove(entity);51 expect(entityCollection.values.length).toEqual(0);52 });5354 it('add sets entityCollection on entity', function() {55 var entity = new Entity();56 var entityCollection = new EntityCollection();5758 entityCollection.add(entity);59 expect(entity.entityCollection).toBe(entityCollection);60 });6162 it('Entity.isShowing changes when collection show changes.', function() {63 var entity = new Entity();64 var entityCollection = new EntityCollection();6566 entityCollection.add(entity);6768 expect(entity.isShowing).toBe(true);6970 var listener = jasmine.createSpy('listener');71 entity.definitionChanged.addEventListener(listener);7273 entityCollection.show = false;7475 expect(listener.calls.count()).toBe(1);76 expect(listener.calls.argsFor(0)).toEqual([entity, 'isShowing', false, true]);77 expect(entity.isShowing).toBe(false);78 });7980 it('add with template', function() {81 var entityCollection = new EntityCollection();8283 var entity = entityCollection.add({84 id : '1'85 });8687 expect(entityCollection.values.length).toEqual(1);88 expect(entity.id).toBe('1');89 expect(entity.constructor).toBe(Entity);90 });9192 it('add/remove raises expected events', function() {93 var entity = new Entity();94 var entity2 = new Entity();95 var entityCollection = new EntityCollection();9697 var listener = new CollectionListener();98 entityCollection.collectionChanged.addEventListener(listener.onCollectionChanged, listener);99100 entityCollection.add(entity);101 expect(listener.timesCalled).toEqual(1);102 expect(listener.added.length).toEqual(1);103 expect(listener.added[0]).toBe(entity);104 expect(listener.removed.length).toEqual(0);105 expect(listener.changed.length).toEqual(0);106107 entity.name = 'newName';108 expect(listener.timesCalled).toEqual(2);109 expect(listener.added.length).toEqual(0);110 expect(listener.removed.length).toEqual(0);111 expect(listener.changed.length).toEqual(1);112 expect(listener.changed[0]).toBe(entity);113114 entityCollection.add(entity2);115 expect(listener.timesCalled).toEqual(3);116 expect(listener.added.length).toEqual(1);117 expect(listener.added[0]).toBe(entity2);118 expect(listener.removed.length).toEqual(0);119 expect(listener.changed.length).toEqual(0);120121 entityCollection.remove(entity2);122 expect(listener.timesCalled).toEqual(4);123 expect(listener.added.length).toEqual(0);124 expect(listener.removed.length).toEqual(1);125 expect(listener.removed[0]).toBe(entity2);126 expect(listener.changed.length).toEqual(0);127128 entityCollection.remove(entity);129 expect(listener.timesCalled).toEqual(5);130 expect(listener.added.length).toEqual(0);131 expect(listener.removed.length).toEqual(1);132 expect(listener.removed[0]).toBe(entity);133134 entityCollection.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);135 });136137 it('raises expected events when reentrant', function() {138 var entityCollection = new EntityCollection();139140 var entity = new Entity();141 var entity2 = new Entity();142 entityCollection.add(entity);143 entityCollection.add(entity2);144145 var entityToDelete = new Entity();146 entityCollection.add(entityToDelete);147148 var entityToAdd = new Entity();149150 var inCallback = false;151 var listener = jasmine.createSpy('listener').and.callFake(function(collection, added, removed, changed) {152 //When we set the name to `newName` below, this code will modify entity2's name, thus triggering153 //another event firing that occurs after all current subscribers have been notified of the154 //event we are inside of.155156 //By checking that inCallback is false, we are making sure the entity2.name assignment157 //is delayed until after the first round of events is fired.158 expect(inCallback).toBe(false);159 inCallback = true;160 if (entity2.name !== 'Bob') {161 entity2.name = 'Bob';162 }163 if (entityCollection.contains(entityToDelete)) {164 entityCollection.removeById(entityToDelete.id);165 }166 if (!entityCollection.contains(entityToAdd)) {167 entityCollection.add(entityToAdd);168 }169 inCallback = false;170 });171 entityCollection.collectionChanged.addEventListener(listener);172173 entity.name = 'newName';174 expect(listener.calls.count()).toBe(2);175 expect(listener.calls.argsFor(0)).toEqual([entityCollection, [], [], [entity]]);176 expect(listener.calls.argsFor(1)).toEqual([entityCollection, [entityToAdd], [entityToDelete], [entity2]]);177178 expect(entity.name).toEqual('newName');179 expect(entity2.name).toEqual('Bob');180 expect(entityCollection.contains(entityToDelete)).toEqual(false);181 expect(entityCollection.contains(entityToAdd)).toEqual(true);182 });183184 it('suspended add/remove raises expected events', function() {185 var entity = new Entity();186 var entity2 = new Entity();187 var entity3 = new Entity();188189 var entityCollection = new EntityCollection();190191 var listener = new CollectionListener();192 entityCollection.collectionChanged.addEventListener(listener.onCollectionChanged, listener);193194 entityCollection.suspendEvents();195 entityCollection.suspendEvents();196 entityCollection.add(entity);197 entityCollection.add(entity2);198 entityCollection.add(entity3);199 entity2.name = 'newName2';200 entity3.name = 'newName3';201 entityCollection.remove(entity2);202203 expect(listener.timesCalled).toEqual(0);204 entityCollection.resumeEvents();205206 expect(listener.timesCalled).toEqual(0);207 entityCollection.resumeEvents();208209 expect(listener.timesCalled).toEqual(1);210 expect(listener.added.length).toEqual(2);211 expect(listener.added[0]).toBe(entity);212 expect(listener.added[1]).toBe(entity3);213 expect(listener.removed.length).toEqual(0);214 expect(listener.changed.length).toEqual(0);215216 entityCollection.suspendEvents();217 entity.name = 'newName';218 entity3.name = 'newewName3';219 entityCollection.remove(entity3);220 entityCollection.resumeEvents();221222 expect(listener.timesCalled).toEqual(2);223 expect(listener.added.length).toEqual(0);224 expect(listener.removed.length).toEqual(1);225 expect(listener.removed[0]).toBe(entity3);226 expect(listener.changed.length).toEqual(1);227 expect(listener.changed[0]).toBe(entity);228229 entityCollection.suspendEvents();230 entityCollection.remove(entity);231 entityCollection.add(entity);232 entityCollection.resumeEvents();233234 expect(listener.timesCalled).toEqual(2);235236 entityCollection.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);237 });238239 it('removeAll works', function() {240 var entity = new Entity();241 var entity2 = new Entity();242 var entityCollection = new EntityCollection();243244 entityCollection.add(entity);245 entityCollection.add(entity2);246 entityCollection.removeAll();247 expect(entityCollection.values.length).toEqual(0);248 });249250 it('removeAll raises expected events', function() {251 var entity = new Entity();252 var entity2 = new Entity();253 var entityCollection = new EntityCollection();254255 var listener = new CollectionListener();256257 entityCollection.add(entity);258 entityCollection.add(entity2);259260 entityCollection.collectionChanged.addEventListener(listener.onCollectionChanged, listener);261 entityCollection.removeAll();262263 expect(listener.timesCalled).toEqual(1);264 expect(listener.removed.length).toEqual(2);265 expect(listener.removed[0]).toBe(entity);266 expect(listener.removed[1]).toBe(entity2);267 expect(listener.added.length).toEqual(0);268269 entityCollection.removeAll();270 expect(listener.timesCalled).toEqual(1);271272 entityCollection.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);273 });274275 it('suspended removeAll raises expected events', function() {276 var entity = new Entity();277 var entity2 = new Entity();278 var entityCollection = new EntityCollection();279280 var listener = new CollectionListener();281282 entityCollection.add(entity);283 entityCollection.add(entity2);284285 entityCollection.collectionChanged.addEventListener(listener.onCollectionChanged, listener);286287 entityCollection.suspendEvents();288 entity2.name = 'newName';289 entityCollection.removeAll();290 entityCollection.resumeEvents();291 expect(listener.timesCalled).toEqual(1);292 expect(listener.removed.length).toEqual(2);293 expect(listener.removed[0]).toBe(entity);294 expect(listener.removed[1]).toBe(entity2);295 expect(listener.added.length).toEqual(0);296 expect(listener.changed.length).toEqual(0);297298 entityCollection.suspendEvents();299 entityCollection.add(entity);300 entityCollection.add(entity2);301 entityCollection.remove(entity2);302 entityCollection.removeAll();303 entityCollection.resumeEvents();304 expect(listener.timesCalled).toEqual(1);305306 entityCollection.collectionChanged.removeEventListener(listener.onCollectionChanged, listener);307 });308309 it('removeById returns false if id not in collection.', function() {310 var entityCollection = new EntityCollection();311 expect(entityCollection.removeById('notThere')).toBe(false);312 });313314 it('getById works', function() {315 var entity = new Entity();316 var entity2 = new Entity();317 var entityCollection = new EntityCollection();318319 entityCollection.add(entity);320 entityCollection.add(entity2);321322 expect(entityCollection.getById(entity.id)).toBe(entity);323 expect(entityCollection.getById(entity2.id)).toBe(entity2);324 });325326 it('getById returns undefined for non-existent object', function() {327 var entityCollection = new EntityCollection();328 expect(entityCollection.getById('123')).toBeUndefined();329 });330331 it('getOrCreateEntity creates a new object if it does not exist.', function() {332 var entityCollection = new EntityCollection();333 expect(entityCollection.values.length).toEqual(0);334 var testObject = entityCollection.getOrCreateEntity('test');335 expect(entityCollection.values.length).toEqual(1);336 expect(entityCollection.values[0]).toEqual(testObject);337 });338339 it('getOrCreateEntity does not create a new object if it already exists.', function() {340 var entityCollection = new EntityCollection();341 expect(entityCollection.values.length).toEqual(0);342 var testObject = entityCollection.getOrCreateEntity('test');343 expect(entityCollection.values.length).toEqual(1);344 expect(entityCollection.values[0]).toEqual(testObject);345 var testObject2 = entityCollection.getOrCreateEntity('test');346 expect(entityCollection.values.length).toEqual(1);347 expect(entityCollection.values[0]).toEqual(testObject);348 expect(testObject2).toEqual(testObject);349 });350351 it('computeAvailability returns infinite with no data.', function() {352 var entityCollection = new EntityCollection();353 var availability = entityCollection.computeAvailability();354 expect(availability.start).toEqual(Iso8601.MINIMUM_VALUE);355 expect(availability.stop).toEqual(Iso8601.MAXIMUM_VALUE);356 });357358 it('computeAvailability returns intersction of collections.', function() {359 var entityCollection = new EntityCollection();360361 var entity = entityCollection.getOrCreateEntity('1');362 var entity2 = entityCollection.getOrCreateEntity('2');363 var entity3 = entityCollection.getOrCreateEntity('3');364365 entity.availability = new TimeIntervalCollection();366 entity.availability.addInterval(TimeInterval.fromIso8601({367 iso8601 : '2012-08-01/2012-08-02'368 }));369 entity2.availability = new TimeIntervalCollection();370 entity2.availability.addInterval(TimeInterval.fromIso8601({371 iso8601 : '2012-08-05/2012-08-06'372 }));373 entity3.availability = undefined;374375 var availability = entityCollection.computeAvailability();376 expect(availability.start).toEqual(JulianDate.fromIso8601('2012-08-01'));377 expect(availability.stop).toEqual(JulianDate.fromIso8601('2012-08-06'));378 });379380 it('computeAvailability works if only start or stop time is infinite.', function() {381 var entityCollection = new EntityCollection();382383 var entity = entityCollection.getOrCreateEntity('1');384 var entity2 = entityCollection.getOrCreateEntity('2');385 var entity3 = entityCollection.getOrCreateEntity('3');386387 entity.availability = new TimeIntervalCollection();388 entity.availability.addInterval(TimeInterval.fromIso8601({389 iso8601 : '2012-08-01/9999-12-31T24:00:00Z'390 }));391 entity2.availability = new TimeIntervalCollection();392 entity2.availability.addInterval(TimeInterval.fromIso8601({393 iso8601 : '0000-01-01T00:00:00Z/2012-08-06'394 }));395 entity3.availability = undefined;396397 var availability = entityCollection.computeAvailability();398 expect(availability.start).toEqual(JulianDate.fromIso8601('2012-08-01'));399 expect(availability.stop).toEqual(JulianDate.fromIso8601('2012-08-06'));400 });401402 it('resumeEvents throws if no matching suspendEvents ', function() {403 var entityCollection = new EntityCollection();404 expect(function() {405 entityCollection.resumeEvents();406 }).toThrowDeveloperError();407 });408409 it('add throws with undefined Entity', function() {410 var entityCollection = new EntityCollection();411 expect(function() {412 entityCollection.add(undefined);413 }).toThrowDeveloperError();414 });415416 it('add throws for Entity with same id', function() {417 var entityCollection = new EntityCollection();418 var entity = new Entity({419 id : '1'420 });421 var entity2 = new Entity({422 id : '1'423 });424 entityCollection.add(entity);425426 expect(function() {427 entityCollection.add(entity2);428 }).toThrowRuntimeError();429 });430431 it('contains returns true if in collection', function() {432 var entityCollection = new EntityCollection();433 var entity = entityCollection.getOrCreateEntity('asd');434 expect(entityCollection.contains(entity)).toBe(true);435 });436437 it('contains returns false if not in collection', function() {438 var entityCollection = new EntityCollection();439 expect(entityCollection.contains(new Entity())).toBe(false);440 });441442 it('contains throws with undefined Entity', function() {443 var entityCollection = new EntityCollection();444 expect(function() {445 entityCollection.contains(undefined);446 }).toThrowDeveloperError();447 });448449 it('remove returns false with undefined Entity', function() {450 var entityCollection = new EntityCollection();451 expect(entityCollection.remove(undefined)).toBe(false);452 });453454 it('removeById returns false with undefined id', function() {455 var entityCollection = new EntityCollection();456 expect(entityCollection.removeById(undefined)).toBe(false);457 });458459 it('getById throws if no id specified', function() {460 var entityCollection = new EntityCollection();461 expect(function() {462 entityCollection.getById(undefined);463 }).toThrowDeveloperError();464 });465466 it('getOrCreateEntity throws if no id specified', function() {467 var entityCollection = new EntityCollection();468 expect(function() {469 entityCollection.getOrCreateEntity(undefined);470 }).toThrowDeveloperError();471 });
...
collection.js
Source:collection.js
1/**2 * @fileOverview Backstage server aware data collections.3 * @author David Huynh4 * @author <a href="mailto:ryanlee@zepheira.com">Ryan Lee</a>5 */6/**7 * @@@ A number of the methods referred to below do not exist, probably8 * because the intent was to replace Exhibit.Collection with9 * Backstage.Collection on load, which is commented out. What10 * should be done with these calls to nowhere?11 */12/**13 * NB, all Backstage.Collection()._listeners related material is commented14 * out, largely because Backstage does not need listeners to operate (the15 * interaction with the server covers and responds with all changes, so16 * local listeners are not wholly necessary at this stage). If they come17 * back, look to replace the listener-related material with jQuery events.18 */19/**20 * @class21 * @constructor22 * @param {String} id23 * @param {Exhibit.Database} database24 */ 25Backstage.Collection = function(id, database) {26 this._id = id;27 this._database = database;28 29 //this._listeners = new SimileAjax.ListenerQueue();30 this._facets = [];31 this._updating = false;32 33 this._items = null;34 this._restrictedItems = null;35};36/**37 * @static38 * @param {String} id39 * @param {Object} configuration40 * @param {Backstage._Impl} backstage41 * @returns {Backstage.Collection}42 */43Backstage.Collection.create = function(id, configuration, backstage) {44 var collection = new Backstage.Collection(id, backstage);45 46 if (typeof configuration.itemTypes !== "undefined") {47 collection._type = "types-based";48 collection._itemTypes = configuration.itemTypes;49 collection._update = Backstage.Collection._typeBasedCollection_update;50 collection.getServerSideConfiguration = Backstage.Collection._typeBasedCollection_getServerSideConfiguration;51 } else {52 collection._type = "all-items";53 collection._update = Backstage.Collection._allItemsCollection_update;54 collection.getServerSideConfiguration = Backstage.Collection._allItemsCollection_getServerSideConfiguration;55 }56 57 return collection;58};59/**60 * @static61 * @param {String} id62 * @param {Object} configuration63 * @param {Backstage.UIContext} uiContext64 * @returns {Backstage.Collection}65 */66Backstage.Collection.create2 = function(id, configuration, uiContext) {67 var backstage, collection;68 backstage = uiContext.getMain();69 70 if (typeof configuration.expression !== "undefined") {71 collection = new Backstage.Collection(id, backstage);72 73 collection._expression = Exhibit.ExpressionParser.parse(configuration.expression);74 collection._baseCollection = (typeof configuration.baseCollectionID !== "undefined") ? 75 uiContext.getExhibit().getCollection(configuration.baseCollectionID) : 76 uiContext.getCollection();77 78 return collection;79 } else {80 return Backstage.Collection.create(id, configuration, backstage);81 }82};83/**84 * @static85 * @param {String} id86 * @param {Element} elmt87 * @param {Backstage._Impl} backstage88 * @returns {Backstage.Collection}89 */90Backstage.Collection.createFromDOM = function(id, elmt, backstage) {91 var collection, itemTypes;92 collection = new Backstage.Collection(id, backstage);93 94 itemTypes = Exhibit.getAttribute(elmt, "itemTypes", ",");95 if (itemTypes !== null && itemTypes.length > 0) {96 collection._type = "types-based";97 collection._itemTypes = itemTypes;98 collection._update = Backstage.Collection._typeBasedCollection_update;99 collection.getServerSideConfiguration = Backstage.Collection._typeBasedCollection_getServerSideConfiguration;100 } else {101 collection._type = "all-items";102 collection._update = Backstage.Collection._allItemsCollection_update;103 collection.getServerSideConfiguration = Backstage.Collection._allItemsCollection_getServerSideConfiguration;104 }105 106 return collection;107};108/**109 * @static110 * @param {String} id111 * @param {Element} elmt112 * @param {Backstage.UIContext} uiContext113 * @returns {Backstage.Collection}114 */115Backstage.Collection.createFromDOM2 = function(id, elmt, uiContext) {116 var backstage, expressionString, collection, baseCollectionID;117 backstage = uiContext.getMain();118 119 expressionString = Exhibit.getAttribute(elmt, "expression");120 if (expressionString !== null && expressionString.length > 0) {121 collection = new Backstage.Collection(id, backstage);122 123 collection._expression = Exhibit.ExpressionParser.parse(expressionString);124 125 baseCollectionID = Exhibit.getAttribute(elmt, "baseCollectionID");126 collection._baseCollection = (baseCollectionID !== null && baseCollectionID.length > 0) ? 127 uiContext.getExhibit().getCollection(baseCollectionID) : 128 uiContext.getCollection();129 130 return collection;131 } else {132 return Backstage.Collection.createFromDOM(id, elmt, backstage);133 }134};135/**136 * @static137 * @param {String} id138 * @param {Backstage._Impl} backstage139 * @returns {Backstage.Collection}140 */141Backstage.Collection.createAllItemsCollection = function(id, backstage) {142 var collection = new Backstage.Collection(id, backstage);143 collection._type = "all-items";144 collection._update = Backstage.Collection._allItemsCollection_update;145 collection.getServerSideConfiguration = Backstage.Collection._allItemsCollection_getServerSideConfiguration;146 147 return collection;148};149/*======================================================================150 * Implementation151 *======================================================================152 */153/**154 * 155 */156Backstage.Collection._allItemsCollection_update = function() {157 this._items = this._database.getAllItems();158 this._onRootItemsChanged();159};160/**161 * @returns {Object}162 */163Backstage.Collection._allItemsCollection_getServerSideConfiguration = function() {164 return {165 id: this._id,166 type: this._type167 };168};169/**170 *171 */172Backstage.Collection._typeBasedCollection_update = function() {173 var newItems, i;174 newItems = new Exhibit.Set();175 for (i = 0; i < this._itemTypes.length; i++) {176 this._database.getSubjects(this._itemTypes[i], "type", newItems);177 }178 179 this._items = newItems;180 this._onRootItemsChanged();181};182/**183 * @returns {Object}184 */185Backstage.Collection._typeBasedCollection_getServerSideConfiguration = function() {186 return {187 id: this._id,188 type: this._type,189 itemTypes: this._itemTypes.join(";")190 };191};192/**193 *194 */195Backstage.Collection._basedCollection_update = function() {196 this._items = this._expression.evaluate(197 { "value" : this._baseCollection.getRestrictedItems() }, 198 { "value" : "item" }, 199 "value",200 this._database201 ).values;202 203 this._onRootItemsChanged();204};205/**206 * @returns {String}207 */208Backstage.Collection.prototype.getID = function() {209 return this._id;210};211/**212 *213 */214Backstage.Collection.prototype.dispose = function() {215 if (typeof this._baseCollection !== "undefined") {216 //this._baseCollection.removeListener(this._listener);217 this._baseCollection = null;218 this._expression = null;219 //} else {220 //this._database.removeListener(this._listener);221 }222 this._database = null;223 this._listener = null;224 225 //this._listeners = null;226 this._items = null;227 this._restrictedItems = null;228};229/**230 * 231 */232Backstage.Collection.prototype.addListener = function(listener) {233 //this._listeners.add(listener);234};235/**236 *237 */238Backstage.Collection.prototype.removeListener = function(listener) {239 //this._listeners.remove(listener);240};241/**242 * @param {Backstage.Facet} facet243 */244Backstage.Collection.prototype.addFacet = function(facet) {245 this._facets.push(facet);246 247 if (facet.hasRestrictions()) {248 this._computeRestrictedItems();249 this._updateFacets(null);250 //this._listeners.fire("onItemsChanged", []);251 } else {252 facet.update(this.getRestrictedItems());253 }254};255/**256 * @param {Backstage.Facet} facet257 */258Backstage.Collection.prototype.removeFacet = function(facet) {259 var i;260 for (i = 0; i < this._facets.length; i++) {261 if (facet === this._facets[i]) {262 this._facets.splice(i, 1);263 if (facet.hasRestrictions()) {264 this._computeRestrictedItems();265 this._updateFacets(null);266 //this._listeners.fire("onItemsChanged", []);267 }268 break;269 }270 }271};272/**273 *274 */275Backstage.Collection.prototype.clearAllRestrictions = function() {276 var restrictions, i;277 restrictions = [];278 279 this._updating = true;280 for (i = 0; i < this._facets.length; i++) {281 restrictions.push(this._facets[i].clearAllRestrictions());282 }283 this._updating = false;284 285 this.onFacetUpdated(null);286 287 return restrictions;288};289/**290 * @param {Array} restrictions291 */292Backstage.Collection.prototype.applyRestrictions = function(restrictions) {293 var i;294 this._updating = true;295 for (i = 0; i < this._facets.length; i++) {296 this._facets[i].applyRestrictions(restrictions[i]);297 }298 this._updating = false;299 300 this.onFacetUpdated(null);301};302/**303 * @returns {Exhibit.Set}304 */305Backstage.Collection.prototype.getAllItems = function() {306 return new Exhibit.Set(this._items);307};308/**309 * @returns {Number}310 */311Backstage.Collection.prototype.countAllItems = function() {312 return this._items.size();313};314/**315 * @returns {Exhibit.Set}316 */317Backstage.Collection.prototype.getRestrictedItems = function() {318 return new Exhibit.Set(this._restrictedItems);319};320/**321 * @returns {Number}322 */323Backstage.Collection.prototype.countRestrictedItems = function() {324 return this._restrictedItems.size();325};326/**327 * @param {Backstage.Facet} facetChanged328 */329Backstage.Collection.prototype.onFacetUpdated = function(facetChanged) {330 if (!this._updating) {331 this._computeRestrictedItems();332 this._updateFacets(facetChanged);333 //this._listeners.fire("onItemsChanged", []);334 }335};336/**337 *338 */339Backstage.Collection.prototype._onRootItemsChanged = function() {340 //this._listeners.fire("onRootItemsChanged", []);341 342 this._computeRestrictedItems();343 this._updateFacets(null);344 345 //this._listeners.fire("onItemsChanged", []);346};...
Using AI Code Generation
1import { storiesOf } from '@storybook/react';2import { withKnobs, text } from '@storybook/addon-knobs';3import { withInfo } from '@storybook/addon-info';4import { withReadme } from 'storybook-readme';5import { withDocs } from 'storybook-readme';6import { withDocsCustom } from 'storybook-readme';7import { withDocsOnly } from 'storybook-readme';8import { setDefaults } from 'storybook-readme';9import { setOptions } from '@storybook/addon-options';10import { configure, addDecorator } from '@storybook/react';11import { configureViewport } from '@storybook/addon-viewport';12import { withViewport } from '@storybook/addon-viewport';13import { setAddon } from '@storybook/react';14import { setDefaults } from '@storybook/addon-info';15import { setOptions } from '@storybook/addon-options';16import { setAddon } from '@storybook/react';17import { setDefaults } from '@storybook/addon-info';18import { setOptions } from '@storybook/addon-options';19import { setAddon } from '@storybook/react';20import { setDefaults } from '@storybook/addon-info';21import { setOptions } from '@storybook/addon-options';22import { setAddon } from '@storybook/react';23import { setDefaults } from '@storybook/addon-info';24import { setOptions } from '@storybook/addon-options';25import { setAddon } from '@storybook/react';26import { setDefaults } from '@storybook/addon-info';27import { setOptions } from '@storybook/addon-options';28import { setAddon } from '@storybook/react';29import { setDefaults } from '@storybook/addon-info';30import { setOptions } from '@storybook/addon-options';31import { setAddon } from '@storybook/react';32import { setDefaults } from '@storybook/addon-info';33import { setOptions } from '@storybook/addon-options';34import { setAddon } from '@storybook/react';35import { setDefaults } from '@storybook/addon-info';36import { setOptions } from '@storybook/addon-options';37import { setAddon } from '@storybook/react';38import { setDefaults } from '@storybook/addon-info';39import { setOptions } from '@storybook/addon-options';40import { setAddon } from '@storybook/react';41import { setDefaults } from '@storybook/addon-info';42import { setOptions } from '@storybook/addon-options';43import { setAddon } from '@storybook/react';44import {
Using AI Code Generation
1import { storiesOf, addDecorator } from 'storybook-root';2import { withKnobs, text } from '@storybook/addon-knobs';3addDecorator(withKnobs);4storiesOf('Storybook Knobs', module)5 .add('with a button', () => {6 const name = text('Name', 'Arunoda');7 const age = number('Age', 89);8 return `<button>Hello ${name} (${age})</button>`;9 });
Using AI Code Generation
1import { storiesOf } from 'storybook-root';2storiesOf('MyComponent', module)3 .add('story1', () => <div>...</div>)4 .add('story2', () => <div>...</div>);5import { storiesOf } from 'storybook-root';6storiesOf('MyComponent', module)7 .add('story1', () => <div>...</div>)8 .add('story2', () => <div>...</div>);9import { storiesOf } from 'storybook-root';10storiesOf('MyComponent', module)11 .add('story1', () => <div>...</div>)12 .add('story2', () => <div>...</div>);13import { storiesOf } from 'storybook-root';14storiesOf('MyComponent', module)15 .add('story1', () => <div>...</div>)16 .add('story2', () => <div>...</div>);17import { storiesOf } from 'storybook-root';18storiesOf('MyComponent', module)19 .add('story1', () => <div>...</div>)20 .add('story2', () => <div>...</div>);21import { storiesOf } from 'storybook-root';22storiesOf('MyComponent', module)23 .add('story1', () => <div>...</div>)24 .add('story2', () => <div>...</div>);25import { storiesOf } from 'storybook-root';26storiesOf('MyComponent', module)27 .add('story1', () => <div>...</div>)28 .add('story2', () => <div>...</div>);29import { storiesOf } from 'storybook-root';30storiesOf('MyComponent', module)31 .add('story1', () => <div>...</div>)32 .add('story2', () => <div>...</div>);33import { storiesOf } from 'storybook-root';34storiesOf('MyComponent', module)
Using AI Code Generation
1import { storiesOf } from 'storybook-root';2storiesOf('My First Story', module)3 .add('First Story', () => <div>First Story</div>)4 .add('Second Story', () => <div>Second Story</div>);5import { storiesOf } from 'storybook-root';6storiesOf('My Second Story', module)7 .add('First Story', () => <div>First Story</div>)8 .add('Second Story', () => <div>Second Story</div>);
Using AI Code Generation
1import { collection } from 'storybook-child-config';2 {3 render: () => <Button label="Hello Button" />,4 },5 {6 render: () => <Welcome showApp={linkTo('Button')} />,7 },8];9export default collection(stories, module, 'storybook-child-config');10import { configure } from '@storybook/react';11import { configureStorybook } from 'storybook-child-config';12configureStorybook(configure);13const path = require('path');14module.exports = ({ config }) => {15 config.module.rules.push({16 test: /\.(ts|tsx)$/,17 include: path.resolve(__dirname, '../'),18 loader: require.resolve('ts-loader'),19 });20 config.resolve.extensions.push('.ts', '.tsx');21 return config;22};23import '@storybook/addon-actions/register';24import '@storybook/addon-links/register';25import 'storybook-root-config/register';26{27 "compilerOptions": {
Using AI Code Generation
1var storybook = require('storybook-root');2storybook.collection('test', function (err, collection) {3 if (err) {4 console.log(err);5 return;6 }7});8var storybook = require('storybook-root');9storybook.collection('test', function (err, collection) {10 if (err) {11 console.log(err);12 return;13 }14});15var storybook = require('storybook-root');16storybook.collection('test', function (err, collection) {17 if (err) {18 console.log(err);19 return;20 }21});22var storybook = require('storybook-root');23storybook.collection('test', function (err, collection) {24 if (err) {25 console.log(err);26 return;27 }28});29var storybook = require('storybook-root');30storybook.collection('test', function (err, collection) {31 if (err) {32 console.log(err);33 return;34 }35});36var storybook = require('storybook-root');37storybook.collection('test', function (err, collection) {38 if (err) {39 console.log(err);40 return;41 }42});43var storybook = require('storybook-root');44storybook.collection('test', function (err, collection) {45 if (err) {46 console.log(err);47 return;48 }49});50var storybook = require('storybook-root');51storybook.collection('test', function (err, collection) {52 if (err) {53 console.log(err);54 return;55 }56});
Using AI Code Generation
1storiesOf('collection', module)2 .add('should return an array of stories', () => {3 const stories = collection('path/to/stories');4 expect(stories).toEqual([5 {6 },7 {8 },9 ]);10 });11import { storiesOf } from '@storybook/react';12storiesOf('Story 1', module).add('default', () => <Story1 />);13storiesOf('Story 2', module).add('default', () => <Story2 />);14import { getStorybook } from '@storybook/react';15export function collection(path) {16}17import { getStorybook } from '@storybook/react';18export function collection(path) {19 const stories = getStorybook();20 return stories.map(story => ({21 component: story.render(),22 }));23}24import { getStorybook } from '@storybook/react';25export function collection(path) {26 const stories = getStorybook();27 return stories.map(story => ({28 component: story.render(),29 }));30}31import { getStorybook } from '@storybook/react';32export function collection(path) {33 const stories = getStorybook();34 .filter(story => story.kind === path)35 .map(story => ({36 component: story.render(),37 }));38}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!