itemPaneTest.js (9596B)
1 describe("Item pane", function () { 2 var win, doc, itemsView; 3 4 before(function* () { 5 win = yield loadZoteroPane(); 6 doc = win.document; 7 itemsView = win.ZoteroPane.itemsView; 8 }); 9 after(function () { 10 win.close(); 11 }); 12 13 describe("Info pane", function () { 14 it("should refresh on item update", function* () { 15 var item = new Zotero.Item('book'); 16 var id = yield item.saveTx(); 17 18 var itemBox = doc.getElementById('zotero-editpane-item-box'); 19 var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'title')[1]; 20 assert.equal(label.textContent, ''); 21 22 item.setField('title', 'Test'); 23 yield item.saveTx(); 24 25 var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'title')[1]; 26 assert.equal(label.textContent, 'Test'); 27 28 yield Zotero.Items.erase(id); 29 }) 30 31 32 it.skip("should swap creator names", function* () { 33 var item = new Zotero.Item('book'); 34 item.setCreators([ 35 { 36 firstName: "First", 37 lastName: "Last", 38 creatorType: "author" 39 } 40 ]); 41 yield item.saveTx(); 42 43 var itemBox = doc.getElementById('zotero-editpane-item-box'); 44 var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-lastName')[0]; 45 var parent = label.parentNode; 46 assert.isTrue(parent.hasAttribute('contextmenu')); 47 48 var menupopup = doc.getAnonymousNodes(itemBox)[0] 49 .getElementsByAttribute('id', 'zotero-creator-transform-menu')[0]; 50 // Fake a right-click 51 doc.popupNode = parent; 52 menupopup.openPopup( 53 parent, "after_start", 0, 0, true, false, new MouseEvent('click', { button: 2 }) 54 ); 55 var menuitem = menupopup.getElementsByTagName('menuitem')[0]; 56 menuitem.click(); 57 yield waitForItemEvent('modify'); 58 59 var creator = item.getCreators()[0]; 60 assert.propertyVal(creator, 'firstName', 'Last'); 61 assert.propertyVal(creator, 'lastName', 'First'); 62 }); 63 64 65 it("shouldn't show Swap Names menu for single-field mode", function* () { 66 var item = new Zotero.Item('book'); 67 item.setCreators([ 68 { 69 name: "Name", 70 creatorType: "author" 71 } 72 ]); 73 yield item.saveTx(); 74 75 var itemBox = doc.getElementById('zotero-editpane-item-box'); 76 var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-lastName')[0]; 77 assert.isFalse(label.parentNode.hasAttribute('contextmenu')); 78 }); 79 80 81 // Note: This issue applies to all context menus in the item box (text transform, name swap), 82 // though the others aren't tested. This might go away with the XUL->HTML transition. 83 it.skip("should save open field after changing creator type", function* () { 84 var item = new Zotero.Item('book'); 85 item.setCreators([ 86 { 87 firstName: "First", 88 lastName: "Last", 89 creatorType: "author" 90 } 91 ]); 92 var id = yield item.saveTx(); 93 94 var itemBox = doc.getElementById('zotero-editpane-item-box'); 95 var label = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'place')[1]; 96 label.click(); 97 var textbox = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'place')[1]; 98 textbox.value = "Place"; 99 100 var menuLabel = doc.getAnonymousNodes(itemBox)[0].getElementsByAttribute('fieldname', 'creator-0-typeID')[0]; 101 menuLabel.click(); 102 var menupopup = itemBox._creatorTypeMenu; 103 var menuItems = menupopup.getElementsByTagName('menuitem'); 104 menuItems[1].click(); 105 yield waitForItemEvent('modify'); 106 107 assert.equal(item.getField('place'), 'Place'); 108 assert.equal(Zotero.CreatorTypes.getName(item.getCreators()[0].creatorTypeID), 'contributor'); 109 110 // Wait for no-op saveTx() 111 yield Zotero.Promise.delay(1); 112 }); 113 114 it("should accept 'now' for Accessed", async function () { 115 var item = await createDataObject('item'); 116 117 var itemBox = doc.getElementById('zotero-editpane-item-box'); 118 var box = doc.getAnonymousNodes(itemBox)[0]; 119 var label = box.querySelector('label[fieldname="accessDate"][class="zotero-clicky"]'); 120 label.click(); 121 var textbox = box.querySelector('textbox[fieldname="accessDate"]'); 122 textbox.value = 'now'; 123 // Blur events don't necessarily trigger if window doesn't have focus 124 itemBox.hideEditor(textbox); 125 126 await waitForItemEvent('modify'); 127 128 assert.approximately( 129 Zotero.Date.sqlToDate(item.getField('accessDate'), true).getTime(), 130 Date.now(), 131 5000 132 ); 133 }); 134 }) 135 136 137 describe("Notes pane", function () { 138 it("should refresh on child note change", function* () { 139 var item; 140 var note1; 141 var note2; 142 yield Zotero.DB.executeTransaction(function* () { 143 item = createUnsavedDataObject('item'); 144 yield item.save(); 145 146 note1 = new Zotero.Item('note'); 147 note1.parentID = item.id; 148 note1.setNote('A'); 149 yield note1.save(); 150 151 note2 = new Zotero.Item('note'); 152 note2.parentID = item.id; 153 note2.setNote('B'); 154 yield note2.save(); 155 }); 156 157 var tabs = doc.getElementById('zotero-editpane-tabs'); 158 var notesTab = doc.getElementById('zotero-editpane-notes-tab'); 159 var noteRows = doc.getElementById('zotero-editpane-dynamic-notes'); 160 tabs.selectedItem = notesTab; 161 // Wait for note list to update 162 do { 163 yield Zotero.Promise.delay(1); 164 } 165 while (noteRows.childNodes.length !== 2); 166 167 // Update note text 168 note2.setNote('C'); 169 yield note2.saveTx(); 170 171 // Wait for note list to update 172 do { 173 yield Zotero.Promise.delay(1); 174 } 175 while (Array.from(noteRows.querySelectorAll('label.zotero-box-label')).every(label => label.value != 'C')); 176 }); 177 178 it("should refresh on child note trash", function* () { 179 var item; 180 var note1; 181 var note2; 182 yield Zotero.DB.executeTransaction(function* () { 183 item = createUnsavedDataObject('item'); 184 yield item.save(); 185 186 note1 = new Zotero.Item('note'); 187 note1.parentID = item.id; 188 note1.setNote('A'); 189 yield note1.save(); 190 191 note2 = new Zotero.Item('note'); 192 note2.parentID = item.id; 193 note2.setNote('B'); 194 yield note2.save(); 195 }); 196 197 var tabs = doc.getElementById('zotero-editpane-tabs'); 198 var notesTab = doc.getElementById('zotero-editpane-notes-tab'); 199 var noteRows = doc.getElementById('zotero-editpane-dynamic-notes'); 200 tabs.selectedItem = notesTab; 201 // Wait for note list to update 202 do { 203 yield Zotero.Promise.delay(1); 204 } 205 while (noteRows.childNodes.length !== 2); 206 207 // Click "-" in first note 208 var promise = waitForDialog(); 209 noteRows.childNodes[0].lastChild.click(); 210 yield promise; 211 212 // Wait for note list to update 213 do { 214 yield Zotero.Promise.delay(1); 215 } 216 while (noteRows.childNodes.length !== 1); 217 }); 218 219 it("should refresh on child note delete", function* () { 220 var item; 221 var note1; 222 var note2; 223 yield Zotero.DB.executeTransaction(function* () { 224 item = createUnsavedDataObject('item'); 225 yield item.save(); 226 227 note1 = new Zotero.Item('note'); 228 note1.parentID = item.id; 229 note1.setNote('A'); 230 yield note1.save(); 231 232 note2 = new Zotero.Item('note'); 233 note2.parentID = item.id; 234 note2.setNote('B'); 235 yield note2.save(); 236 }); 237 238 var tabs = doc.getElementById('zotero-editpane-tabs'); 239 var notesTab = doc.getElementById('zotero-editpane-notes-tab'); 240 var noteRows = doc.getElementById('zotero-editpane-dynamic-notes'); 241 tabs.selectedItem = notesTab; 242 // Wait for note list to update 243 do { 244 yield Zotero.Promise.delay(1); 245 } 246 while (noteRows.childNodes.length !== 2); 247 248 yield note2.eraseTx(); 249 250 // Wait for note list to update 251 do { 252 yield Zotero.Promise.delay(1); 253 } 254 while (noteRows.childNodes.length !== 1); 255 }); 256 }); 257 258 259 describe("Attachment pane", function () { 260 it("should refresh on file rename", function* () { 261 var file = getTestDataDirectory(); 262 file.append('test.png'); 263 var item = yield Zotero.Attachments.importFromFile({ 264 file: file 265 }); 266 var newName = 'test2.png'; 267 yield item.renameAttachmentFile(newName); 268 269 var itemBox = doc.getElementById('zotero-attachment-box'); 270 var label = itemBox._id('fileName'); 271 assert.equal(label.value, newName); 272 }) 273 }) 274 275 276 describe("Note editor", function () { 277 it("should refresh on note update", function* () { 278 var item = new Zotero.Item('note'); 279 var id = yield item.saveTx(); 280 281 var noteEditor = doc.getElementById('zotero-note-editor'); 282 283 // Wait for the editor 284 yield new Zotero.Promise((resolve, reject) => { 285 noteEditor.noteField.onInit(() => resolve()); 286 }) 287 assert.equal(noteEditor.noteField.value, ''); 288 289 item.setNote('<p>Test</p>'); 290 yield item.saveTx(); 291 292 assert.equal(noteEditor.noteField.value, '<p>Test</p>'); 293 }) 294 }) 295 296 describe("Feed buttons", function() { 297 describe("Mark as Read/Unread", function() { 298 it("should update label when state of an item changes", function* () { 299 let feed = yield createFeed(); 300 yield selectLibrary(win, feed.libraryID); 301 yield waitForItemsLoad(win); 302 303 var stub = sinon.stub(win.ZoteroPane, 'startItemReadTimeout'); 304 var item = yield createDataObject('feedItem', { libraryID: feed.libraryID }); 305 // Skip timed mark-as-read 306 assert.ok(stub.called); 307 stub.restore(); 308 item.isRead = true; 309 yield item.saveTx(); 310 311 let button = doc.getElementById('zotero-feed-item-toggleRead-button'); 312 313 assert.equal(button.getAttribute('label'), Zotero.getString('pane.item.markAsUnread')); 314 yield item.toggleRead(false); 315 assert.equal(button.getAttribute('label'), Zotero.getString('pane.item.markAsRead')); 316 }); 317 }); 318 }); 319 })