www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

collectionTest.js (12687B)


      1 "use strict";
      2 
      3 describe("Zotero.Collection", function() {
      4 	describe("#save()", function () {
      5 		it("should save a new collection", function* () {
      6 			var name = "Test";
      7 			var collection = new Zotero.Collection;
      8 			collection.name = name;
      9 			var id = yield collection.saveTx();
     10 			assert.equal(collection.name, name);
     11 			collection = yield Zotero.Collections.getAsync(id);
     12 			assert.equal(collection.name, name);
     13 		});
     14 	})
     15 	
     16 	describe("#erase()", function () {
     17 		it("should delete a collection but not its descendant item by default", function* () {
     18 			var collection = yield createDataObject('collection');
     19 			var item = yield createDataObject('item', { collections: [collection.id] });
     20 			assert.isTrue(collection.hasItem(item.id));
     21 			
     22 			yield collection.eraseTx();
     23 			
     24 			assert.isFalse((yield Zotero.Items.getAsync(item.id)).deleted);
     25 		})
     26 		
     27 		it("should delete a collection and trash its descendant items with deleteItems: true", function* () {
     28 			var collection = yield createDataObject('collection');
     29 			var item1 = yield createDataObject('item', { collections: [collection.id] });
     30 			var item2 = yield createDataObject('item', { collections: [collection.id] });
     31 			assert.isTrue(collection.hasItem(item1.id));
     32 			assert.isTrue(collection.hasItem(item2.id));
     33 			
     34 			yield collection.eraseTx({ deleteItems: true });
     35 			
     36 			assert.isTrue((yield Zotero.Items.getAsync(item1.id)).deleted);
     37 			assert.isTrue((yield Zotero.Items.getAsync(item2.id)).deleted);
     38 		});
     39 		
     40 		it("should clear collection from item cache", function* () {
     41 			var collection = yield createDataObject('collection');
     42 			var item = yield createDataObject('item', { collections: [collection.id] });
     43 			assert.lengthOf(item.getCollections(), 1);
     44 			yield collection.eraseTx();
     45 			assert.lengthOf(item.getCollections(), 0);
     46 		});
     47 		
     48 		it("should clear subcollection from descendent item cache", function* () {
     49 			var collection = yield createDataObject('collection');
     50 			var subcollection = yield createDataObject('collection', { parentID: collection.id });
     51 			var item = yield createDataObject('item', { collections: [subcollection.id] });
     52 			assert.lengthOf(item.getCollections(), 1);
     53 			yield collection.eraseTx();
     54 			assert.lengthOf(item.getCollections(), 0);
     55 		});
     56 		
     57 		it("should clear collection from item cache in deleteItems mode", function* () {
     58 			var collection = yield createDataObject('collection');
     59 			var item = yield createDataObject('item', { collections: [collection.id] });
     60 			assert.lengthOf(item.getCollections(), 1);
     61 			yield collection.eraseTx({ deleteItems: true });
     62 			assert.lengthOf(item.getCollections(), 0);
     63 		});
     64 	})
     65 	
     66 	describe("#version", function () {
     67 		it("should set object version", function* () {
     68 			var version = 100;
     69 			var collection = new Zotero.Collection
     70 			collection.version = version;
     71 			collection.name = "Test";
     72 			var id = yield collection.saveTx();
     73 			assert.equal(collection.version, version);
     74 			collection = yield Zotero.Collections.getAsync(id);
     75 			assert.equal(collection.version, version);
     76 		});
     77 	})
     78 	
     79 	describe("#parentKey", function () {
     80 		it("should set parent collection for new collections", function* () {
     81 			var parentCol = new Zotero.Collection
     82 			parentCol.name = "Parent";
     83 			var parentID = yield parentCol.saveTx();
     84 			var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
     85 			
     86 			var col = new Zotero.Collection
     87 			col.name = "Child";
     88 			col.parentKey = parentKey;
     89 			var id = yield col.saveTx();
     90 			assert.equal(col.parentKey, parentKey);
     91 			col = yield Zotero.Collections.getAsync(id);
     92 			assert.equal(col.parentKey, parentKey);
     93 		});
     94 		
     95 		it("should change parent collection for existing collections", function* () {
     96 			// Create initial parent collection
     97 			var parentCol = new Zotero.Collection
     98 			parentCol.name = "Parent";
     99 			var parentID = yield parentCol.saveTx();
    100 			var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
    101 			
    102 			// Create subcollection
    103 			var col = new Zotero.Collection
    104 			col.name = "Child";
    105 			col.parentKey = parentKey;
    106 			var id = yield col.saveTx();
    107 			
    108 			// Create new parent collection
    109 			var newParentCol = new Zotero.Collection
    110 			newParentCol.name = "New Parent";
    111 			var newParentID = yield newParentCol.saveTx();
    112 			var {libraryID, key: newParentKey} = Zotero.Collections.getLibraryAndKeyFromID(newParentID);
    113 			
    114 			// Change parent collection
    115 			col.parentKey = newParentKey;
    116 			yield col.saveTx();
    117 			assert.equal(col.parentKey, newParentKey);
    118 			col = yield Zotero.Collections.getAsync(id);
    119 			assert.equal(col.parentKey, newParentKey);
    120 		});
    121 		
    122 		it("should not mark collection as unchanged if set to existing value", function* () {
    123 			// Create initial parent collection
    124 			var parentCol = new Zotero.Collection
    125 			parentCol.name = "Parent";
    126 			var parentID = yield parentCol.saveTx();
    127 			var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID);
    128 			
    129 			// Create subcollection
    130 			var col = new Zotero.Collection
    131 			col.name = "Child";
    132 			col.parentKey = parentKey;
    133 			var id = yield col.saveTx();
    134 			
    135 			// Set to existing parent
    136 			col.parentKey = parentKey;
    137 			assert.isFalse(col.hasChanged());
    138 		});
    139 		
    140 		it("should not resave a collection with no parent if set to false", function* () {
    141 			var col = new Zotero.Collection
    142 			col.name = "Test";
    143 			var id = yield col.saveTx();
    144 			
    145 			col.parentKey = false;
    146 			var ret = yield col.saveTx();
    147 			assert.isFalse(ret);
    148 		});
    149 	})
    150 	
    151 	describe("#hasChildCollections()", function () {
    152 		it("should be false if child made top-level", function* () {
    153 			var collection1 = yield createDataObject('collection');
    154 			var collection2 = yield createDataObject('collection', { parentID: collection1.id });
    155 			
    156 			assert.isTrue(collection1.hasChildCollections());
    157 			collection2.parentKey = false;
    158 			yield collection2.saveTx();
    159 			assert.isFalse(collection1.hasChildCollections());
    160 		})
    161 		
    162 		it("should be false if child moved to another collection", function* () {
    163 			var collection1 = yield createDataObject('collection');
    164 			var collection2 = yield createDataObject('collection', { parentID: collection1.id });
    165 			var collection3 = yield createDataObject('collection');
    166 			
    167 			assert.isTrue(collection1.hasChildCollections());
    168 			collection2.parentKey = collection3.key;
    169 			yield collection2.saveTx();
    170 			assert.isFalse(collection1.hasChildCollections());
    171 		})
    172 	})
    173 	
    174 	describe("#getChildCollections()", function () {
    175 		it("should include child collections", function* () {
    176 			var collection1 = yield createDataObject('collection');
    177 			var collection2 = yield createDataObject('collection', { parentID: collection1.id });
    178 			yield collection1.saveTx();
    179 			
    180 			var childCollections = collection1.getChildCollections();
    181 			assert.lengthOf(childCollections, 1);
    182 			assert.equal(childCollections[0].id, collection2.id);
    183 		})
    184 		
    185 		it("should not include collections that have been removed", function* () {
    186 			var collection1 = yield createDataObject('collection');
    187 			var collection2 = yield createDataObject('collection', { parentID: collection1.id });
    188 			yield collection1.saveTx();
    189 			
    190 			collection2.parentID = false;
    191 			yield collection2.save()
    192 			
    193 			var childCollections = collection1.getChildCollections();
    194 			assert.lengthOf(childCollections, 0);
    195 		})
    196 		
    197 		it("should not include collections that have been deleted", function* () {
    198 			var collection1 = yield createDataObject('collection');
    199 			var collection2 = yield createDataObject('collection', { parentID: collection1.id });
    200 			yield collection1.saveTx();
    201 			
    202 			yield collection2.eraseTx()
    203 			
    204 			var childCollections = collection1.getChildCollections();
    205 			assert.lengthOf(childCollections, 0);
    206 		})
    207 	})
    208 	
    209 	describe("#getChildItems()", function () {
    210 		it("should include child items", function* () {
    211 			var collection = yield createDataObject('collection');
    212 			var item = createUnsavedDataObject('item');
    213 			item.addToCollection(collection.key);
    214 			yield item.saveTx();
    215 			
    216 			assert.lengthOf(collection.getChildItems(), 1);
    217 		})
    218 		
    219 		it("should not include items in trash by default", function* () {
    220 			var collection = yield createDataObject('collection');
    221 			var item = createUnsavedDataObject('item');
    222 			item.deleted = true;
    223 			item.addToCollection(collection.key);
    224 			yield item.saveTx();
    225 			
    226 			assert.lengthOf(collection.getChildItems(), 0);
    227 		})
    228 		
    229 		it("should include items in trash if includeTrashed=true", function* () {
    230 			var collection = yield createDataObject('collection');
    231 			var item = createUnsavedDataObject('item');
    232 			item.deleted = true;
    233 			item.addToCollection(collection.key);
    234 			yield item.saveTx();
    235 			
    236 			assert.lengthOf(collection.getChildItems(false, true), 1);
    237 		})
    238 		
    239 		it("should not include removed items", function* () {
    240 			var col = yield createDataObject('collection');
    241 			var item = yield createDataObject('item', { collections: [ col.id ] });
    242 			assert.lengthOf(col.getChildItems(), 1);
    243 			item.setCollections([]);
    244 			yield item.saveTx();
    245 			Zotero.debug(col.getChildItems());
    246 			assert.lengthOf(col.getChildItems(), 0);
    247 		});
    248 		
    249 		it("should not include deleted items", function* () {
    250 			var col = yield createDataObject('collection');
    251 			var item = yield createDataObject('item', { collections: [ col.id ] });
    252 			assert.lengthOf(col.getChildItems(), 1);
    253 			yield item.erase();
    254 			assert.lengthOf(col.getChildItems(), 0);
    255 		});
    256 		
    257 		it("should not include items emptied from trash", function* () {
    258 			var col = yield createDataObject('collection');
    259 			var item = yield createDataObject('item', { collections: [ col.id ], deleted: true });
    260 			yield item.erase();
    261 			assert.lengthOf(col.getChildItems(), 0);
    262 		});
    263 	})
    264 	
    265 	describe("#toJSON()", function () {
    266 		it("should set 'parentCollection' to false when cleared", function* () {
    267 			var col1 = yield createDataObject('collection');
    268 			var col2 = yield createDataObject('collection', { parentID: col1.id });
    269 			// Create initial JSON with parentCollection
    270 			var patchBase = col2.toJSON();
    271 			// Clear parent collection and regenerate JSON
    272 			col2.parentID = false;
    273 			var json = col2.toJSON({ patchBase });
    274 			assert.isFalse(json.parentCollection);
    275 		});
    276 	});
    277 	
    278 	describe("#getDescendents()", function () {
    279 		var collection0, collection1, collection2, collection3, item1, item2, item3;
    280 		
    281 		before(function* () {
    282 			collection0 = yield createDataObject('collection');
    283 			item1 = yield createDataObject('item', { collections: [collection0.id] });
    284 			collection1 = yield createDataObject('collection', { parentKey: collection0.key });
    285 			item2 = yield createDataObject('item', { collections: [collection1.id] });
    286 			collection2 = yield createDataObject('collection', { parentKey: collection1.key });
    287 			collection3 = yield createDataObject('collection', { parentKey: collection1.key });
    288 			item3 = yield createDataObject('item', { collections: [collection2.id] });
    289 			item3.deleted = true;
    290 			yield item3.saveTx();
    291 		});
    292 		
    293 		it("should return a flat array of collections and items", function* () {
    294 			var desc = collection0.getDescendents();
    295 			assert.lengthOf(desc, 5);
    296 			assert.sameMembers(
    297 				desc.map(x => x.type + ':' + x.id + ':' + (x.name || '') + ':' + x.parent),
    298 				[
    299 					'item:' + item1.id + '::' + collection0.id,
    300 					'item:' + item2.id + '::' + collection1.id,
    301 					'collection:' + collection1.id + ':' + collection1.name + ':' + collection0.id,
    302 					'collection:' + collection2.id + ':' + collection2.name + ':' + collection1.id,
    303 					'collection:' + collection3.id + ':' + collection3.name + ':' + collection1.id
    304 				]
    305 			);
    306 		});
    307 		
    308 		it("should return nested arrays of collections and items", function* () {
    309 			var desc = collection0.getDescendents(true);
    310 			assert.lengthOf(desc, 2);
    311 			assert.sameMembers(
    312 				desc.map(x => x.type + ':' + x.id + ':' + (x.name || '') + ':' + x.parent),
    313 				[
    314 					'item:' + item1.id + '::' + collection0.id,
    315 					'collection:' + collection1.id + ':' + collection1.name + ':' + collection0.id,
    316 				]
    317 			);
    318 			var c = desc[0].type == 'collection' ? desc[0] : desc[1];
    319 			assert.lengthOf(c.children, 3);
    320 			assert.sameMembers(
    321 				c.children.map(x => x.type + ':' + x.id + ':' + (x.name || '') + ':' + x.parent),
    322 				[
    323 					'item:' + item2.id + '::' + collection1.id,
    324 					'collection:' + collection2.id + ':' + collection2.name + ':' + collection1.id,
    325 					'collection:' + collection3.id + ':' + collection3.name + ':' + collection1.id
    326 				]
    327 			);
    328 		});
    329 		
    330 		it("should not include deleted items", function* () {
    331 			var col = yield createDataObject('collection');
    332 			var item = yield createDataObject('item', { collections: [col.id] });
    333 			assert.lengthOf(col.getDescendents(), 1);
    334 			yield item.eraseTx();
    335 			assert.lengthOf(col.getDescendents(), 0);
    336 		});
    337 
    338 	});
    339 })