www

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

tagsTest.js (5020B)


      1 "use strict";
      2 
      3 describe("Zotero.Tags", function () {
      4 	describe("#getID()", function () {
      5 		it("should return tag id", function* () {
      6 			var tagName = Zotero.Utilities.randomString();
      7 			var item = createUnsavedDataObject('item');
      8 			item.addTag(tagName);
      9 			yield item.saveTx();
     10 			
     11 			assert.typeOf(Zotero.Tags.getID(tagName), "number");
     12 		})
     13 	})
     14 	
     15 	describe("#getName()", function () {
     16 		it("should return tag id", function* () {
     17 			var tagName = Zotero.Utilities.randomString();
     18 			var item = createUnsavedDataObject('item');
     19 			item.addTag(tagName);
     20 			yield item.saveTx();
     21 			
     22 			var libraryID = Zotero.Libraries.userLibraryID;
     23 			var tagID = Zotero.Tags.getID(tagName);
     24 			assert.equal(Zotero.Tags.getName(tagID), tagName);
     25 		})
     26 	})
     27 	
     28 	describe("#rename()", function () {
     29 		it("should mark items as changed", function* () {
     30 			var item1 = yield createDataObject('item', { tags: [{ tag: "A" }], synced: true });
     31 			var item2 = yield createDataObject('item', { tags: [{ tag: "A" }, { tag: "B" }], synced: true });
     32 			var item3 = yield createDataObject('item', { tags: [{ tag: "B" }, { tag: "C" }], synced: true });
     33 			
     34 			yield Zotero.Tags.rename(item1.libraryID, "A", "D");
     35 			assert.isFalse(item1.synced);
     36 			assert.isFalse(item2.synced);
     37 			assert.isTrue(item3.synced);
     38 		});
     39 	});
     40 	
     41 	describe("#removeFromLibrary()", function () {
     42 		it("should remove tags in given library", async function () {
     43 			var libraryID = Zotero.Libraries.userLibraryID;
     44 			var groupLibraryID = (await getGroup()).libraryID;
     45 			
     46 			var tags = [];
     47 			var items = [];
     48 			await Zotero.DB.executeTransaction(async function () {
     49 				for (let i = 0; i < 10; i++) {
     50 					let tagName = Zotero.Utilities.randomString();
     51 					tags.push(tagName);
     52 					let item = createUnsavedDataObject('item', { tags: [tagName] });
     53 					await item.save();
     54 					items.push(item);
     55 				}
     56 			});
     57 			
     58 			var groupTagName = tags[0];
     59 			var groupItem = await createDataObject(
     60 				'item',
     61 				{
     62 					libraryID: groupLibraryID,
     63 					tags: [groupTagName]
     64 				}
     65 			);
     66 			
     67 			var tagIDs = tags.map(tag => Zotero.Tags.getID(tag));
     68 			await Zotero.Tags.removeFromLibrary(libraryID, tagIDs);
     69 			items.forEach(item => assert.lengthOf(item.getTags(), 0));
     70 			
     71 			// Group item should still have the tag
     72 			assert.sameDeepMembers(groupItem.getTags(), [{ tag: groupTagName }]);
     73 			assert.equal(
     74 				await Zotero.DB.valueQueryAsync(
     75 					"SELECT COUNT(*) FROM itemTags WHERE itemID=?",
     76 					groupItem.id
     77 				),
     78 				1
     79 			);
     80 		});
     81 		
     82 		
     83 		it("should reload tags of associated items", function* () {
     84 			var libraryID = Zotero.Libraries.userLibraryID;
     85 			
     86 			var tagName = Zotero.Utilities.randomString();
     87 			var item = createUnsavedDataObject('item');
     88 			item.addTag(tagName);
     89 			yield item.saveTx();
     90 			assert.lengthOf(item.getTags(), 1);
     91 			
     92 			var tagID = Zotero.Tags.getID(tagName);
     93 			yield Zotero.Tags.removeFromLibrary(libraryID, tagID);
     94 			assert.lengthOf(item.getTags(), 0);
     95 		})
     96 	})
     97 	
     98 	describe("#purge()", function () {
     99 		it("should remove orphaned tags", function* () {
    100 			var libraryID = Zotero.Libraries.userLibraryID;
    101 			
    102 			var tagName = Zotero.Utilities.randomString();
    103 			var item = createUnsavedDataObject('item');
    104 			item.addTag(tagName);
    105 			yield item.saveTx();
    106 			
    107 			var tagID = Zotero.Tags.getID(tagName);
    108 			assert.typeOf(tagID, "number");
    109 			
    110 			yield item.eraseTx();
    111 			
    112 			assert.equal(Zotero.Tags.getName(tagID), tagName);
    113 			
    114 			yield Zotero.DB.executeTransaction(function* () {
    115 				yield Zotero.Tags.purge();
    116 			});
    117 			
    118 			assert.isFalse(Zotero.Tags.getName(tagID));
    119 		})
    120 	})
    121 	
    122 	
    123 	describe("#setColor()", function () {
    124 		var libraryID;
    125 		
    126 		beforeEach(function* () {
    127 			libraryID = Zotero.Libraries.userLibraryID;
    128 			
    129 			// Clear library tag colors
    130 			var colors = Zotero.Tags.getColors(libraryID);
    131 			for (let color of colors.keys()) {
    132 				yield Zotero.Tags.setColor(libraryID, color);
    133 			}
    134 		});
    135 		
    136 		it("should set color for a tag", function* () {
    137 			var aColor = '#ABCDEF';
    138 			var bColor = '#BCDEF0';
    139 			yield Zotero.Tags.setColor(libraryID, "A", aColor);
    140 			yield Zotero.Tags.setColor(libraryID, "B", bColor);
    141 			
    142 			var o = Zotero.Tags.getColor(libraryID, "A")
    143 			assert.equal(o.color, aColor);
    144 			assert.equal(o.position, 0);
    145 			var o = Zotero.Tags.getColor(libraryID, "B")
    146 			assert.equal(o.color, bColor);
    147 			assert.equal(o.position, 1);
    148 			
    149 			var o = Zotero.SyncedSettings.get(libraryID, 'tagColors');
    150 			assert.isArray(o);
    151 			assert.lengthOf(o, 2);
    152 			assert.sameMembers(o.map(c => c.color), [aColor, bColor]);
    153 		});
    154 		
    155 		it("should clear color for a tag", function* () {
    156 			var aColor = '#ABCDEF';
    157 			yield Zotero.Tags.setColor(libraryID, "A", aColor);
    158 			var o = Zotero.Tags.getColor(libraryID, "A")
    159 			assert.equal(o.color, aColor);
    160 			assert.equal(o.position, 0);
    161 			
    162 			yield Zotero.Tags.setColor(libraryID, "A", false);
    163 			assert.equal(Zotero.Tags.getColors(libraryID).size, 0);
    164 			assert.isFalse(Zotero.Tags.getColor(libraryID, "A"));
    165 			
    166 			var o = Zotero.SyncedSettings.get(libraryID, 'tagColors');
    167 			assert.isNull(o);
    168 		});
    169 	});
    170 })