www

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

collectionsTest.js (5788B)


      1 describe("Zotero.Collections", function () {
      2 	describe("#getByLibrary()", function () {
      3 		it("should get all root collections in a library", function* () {
      4 			var group = yield createGroup();
      5 			var libraryID = group.libraryID;
      6 			
      7 			var col1 = yield createDataObject('collection', { libraryID });
      8 			var col2 = yield createDataObject('collection', { libraryID });
      9 			var col3 = yield createDataObject('collection', { libraryID, parentID: col2.id });
     10 			var cols = Zotero.Collections.getByLibrary(libraryID);
     11 			assert.lengthOf(cols, 2);
     12 			assert.sameMembers(cols.map(col => col.id), [col1.id, col2.id]);
     13 		})
     14 		
     15 		it("should get all collections in a library in recursive mode", function* () {
     16 			var group = yield createGroup();
     17 			var libraryID = group.libraryID;
     18 			
     19 			// Create collection in another library
     20 			yield createDataObject('collection');
     21 			
     22 			var col1 = yield createDataObject('collection', { libraryID, name: "C" });
     23 			var col2 = yield createDataObject('collection', { libraryID, name: "A" });
     24 			var col3 = yield createDataObject('collection', { libraryID, name: "D", parentID: col2.id });
     25 			var col4 = yield createDataObject('collection', { libraryID, name: "B", parentID: col2.id });
     26 			var col5 = yield createDataObject('collection', { libraryID, name: "E", parentID: col2.id });
     27 			var col6 = yield createDataObject('collection', { libraryID, name: "G", parentID: col3.id });
     28 			var col7 = yield createDataObject('collection', { libraryID, name: "F", parentID: col3.id });
     29 			var cols = Zotero.Collections.getByLibrary(libraryID, true);
     30 			assert.lengthOf(cols, 7);
     31 			var ids = cols.map(col => col.id);
     32 			assert.sameMembers(
     33 				ids, [col1.id, col2.id, col3.id, col4.id, col5.id, col6.id, col7.id]
     34 			);
     35 			assert.isBelow(ids.indexOf(col2.id), ids.indexOf(col4.id), "A before child B");
     36 			assert.isBelow(ids.indexOf(col4.id), ids.indexOf(col3.id), "B before D");
     37 			assert.isBelow(ids.indexOf(col3.id), ids.indexOf(col7.id), "D before child F");
     38 			assert.isBelow(ids.indexOf(col7.id), ids.indexOf(col6.id), "F before G");
     39 			assert.isBelow(ids.indexOf(col6.id), ids.indexOf(col5.id), "G before D sibling E");
     40 			assert.isBelow(ids.indexOf(col5.id), ids.indexOf(col1.id), "E before A sibling C");
     41 			
     42 			// 'level' property, which is a hack for indenting in the advanced search window
     43 			assert.equal(cols[0].level, 0);
     44 			assert.equal(cols[1].level, 1);
     45 			assert.equal(cols[2].level, 1);
     46 			assert.equal(cols[3].level, 2);
     47 			assert.equal(cols[4].level, 2);
     48 			assert.equal(cols[5].level, 1);
     49 			assert.equal(cols[6].level, 0);
     50 		})
     51 	})
     52 	
     53 	describe("#getByParent()", function () {
     54 		it("should get all direct subcollections of a library", function* () {
     55 			var col1 = yield createDataObject('collection');
     56 			var col2 = yield createDataObject('collection');
     57 			var col3 = yield createDataObject('collection', { parentID: col2.id });
     58 			assert.lengthOf(Zotero.Collections.getByParent(col1.id), 0);
     59 			var cols = Zotero.Collections.getByParent(col2.id);
     60 			assert.lengthOf(cols, 1);
     61 			assert.sameMembers(cols.map(col => col.id), [col3.id]);
     62 		})
     63 		
     64 		it("should get all collections underneath a collection in recursive mode", function* () {
     65 			var col1 = yield createDataObject('collection');
     66 			var col2 = yield createDataObject('collection');
     67 			var col3 = yield createDataObject('collection', { parentID: col2.id });
     68 			var col4 = yield createDataObject('collection', { parentID: col3.id });
     69 			assert.lengthOf(Zotero.Collections.getByParent(col1.id), 0);
     70 			var cols = Zotero.Collections.getByParent(col2.id, true);
     71 			assert.lengthOf(cols, 2);
     72 			assert.includeMembers(cols.map(col => col.id), [col3.id, col4.id]);
     73 		})
     74 	})
     75 	
     76 	describe("#getAsync()", function() {
     77 		it("should return a collection item for a collection ID", function* () {
     78 			let collection = new Zotero.Collection({ name: 'foo' });
     79 			collection = yield Zotero.Collections.getAsync(yield collection.saveTx());
     80 			
     81 			assert.notOk(collection.isFeed);
     82 			assert.instanceOf(collection, Zotero.Collection);
     83 			assert.notInstanceOf(collection, Zotero.Feed);
     84 		});
     85 	});
     86 	
     87 	
     88 	describe("#sortByLevel()", function () {
     89 		it("should return collections sorted from top-level to deepest", function* () {
     90 			// - A
     91 			//   - B
     92 			//     - C
     93 			//   - D
     94 			// - E
     95 			//   - F
     96 			//     - G
     97 			//       - H
     98 			//     - I
     99 			
    100 			// Leave out B and G
    101 			// Order should be {A, E}, {D, F}, {C, I}, {H} (internal order is undefined)
    102 			
    103 			var check = function (arr) {
    104 				assert.sameMembers(arr.slice(0, 2), [c1.id, c5.id]);
    105 				assert.sameMembers(arr.slice(2, 4), [c4.id, c6.id]);
    106 				assert.sameMembers(arr.slice(4, 6), [c3.id, c9.id]);
    107 				assert.equal(arr[6], c8.id);
    108 			};
    109 			
    110 			var c1 = yield createDataObject('collection', { "name": "A" });
    111 			var c2 = yield createDataObject('collection', { "name": "B", parentID: c1.id });
    112 			var c3 = yield createDataObject('collection', { "name": "C", parentID: c2.id });
    113 			var c4 = yield createDataObject('collection', { "name": "D", parentID: c1.id });
    114 			var c5 = yield createDataObject('collection', { "name": "E" });
    115 			var c6 = yield createDataObject('collection', { "name": "F", parentID: c5.id });
    116 			var c7 = yield createDataObject('collection', { "name": "G", parentID: c6.id });
    117 			var c8 = yield createDataObject('collection', { "name": "H", parentID: c7.id });
    118 			var c9 = yield createDataObject('collection', { "name": "I", parentID: c6.id });
    119 			
    120 			var arr = Zotero.Collections.sortByLevel([c1, c3, c4, c5, c6, c8, c9].map(c => c.id));
    121 			//Zotero.debug(arr.map(id => Zotero.Collections.get(id).name));
    122 			check(arr);
    123 			
    124 			// Check reverse order
    125 			arr = Zotero.Collections.sortByLevel([c1, c3, c4, c5, c6, c8, c9].reverse().map(c => c.id));
    126 			//Zotero.debug(arr.map(id => Zotero.Collections.get(id).name));
    127 			check(arr);
    128 		});
    129 	});
    130 })