diff --git a/test/content/support.js b/test/content/support.js index e6c8ae75f3..728cbf1e9b 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -20,6 +20,42 @@ function waitForDOMEvent(target, event, capture) { return deferred.promise; } +/** + * Waits for a DOM element's attribute(s) to change. + * @param {HTMLElement} target DOM element + * @param {string | string[]} attributes array of attributes to watch + * @param {(newValue, oldValue) => boolean} callback called whenever attributes changes. return true to stop waiting + */ +async function waitForDOMAttributes(target, attributes, callback) { + if (typeof attributes === "string") { + attributes = [attributes]; + } + let deferred = Zotero.Promise.defer(); + function handleMutation(mutations) { + for (let mutation of mutations) { + if (mutation.type === 'attributes') { + let oldValue = mutation.oldValue; + let newValue = mutation.target.value; + if (callback(newValue, oldValue)) { + observer.disconnect(); + deferred.resolve(); + return; + } + } + } + } + + let observer = new MutationObserver(handleMutation); + observer.observe(target, { + attributes: true, + attributeFilter: attributes, + attributeOldValue: true, + }); + + await deferred.promise; + observer.disconnect(); +} + async function waitForRecognizer() { var win = await waitForWindow('chrome://zotero/content/progressQueueDialog.xhtml') // Wait for status to show as complete diff --git a/test/tests/itemPaneTest.js b/test/tests/itemPaneTest.js index 0e0d4b4017..bd13cfed4c 100644 --- a/test/tests/itemPaneTest.js +++ b/test/tests/itemPaneTest.js @@ -319,33 +319,45 @@ describe("Item pane", function () { describe("Attachment pane", function () { - it("should refresh on file rename", function* () { - var file = getTestDataDirectory(); + it("should refresh on file rename", async function () { + let file = getTestDataDirectory(); file.append('test.png'); - var item = yield Zotero.Attachments.importFromFile({ + let item = await Zotero.Attachments.importFromFile({ file: file }); - var newName = 'test2.png'; - yield item.renameAttachmentFile(newName); + let newName = 'test2.png'; + + let itemBox = doc.getElementById('zotero-attachment-box'); + let label = itemBox._id('fileName'); + let promise = waitForDOMAttributes(label, 'value', (newValue) => { + return newValue === newName; + }); + + await item.renameAttachmentFile(newName); - var itemBox = doc.getElementById('zotero-attachment-box'); - var label = itemBox._id('fileName'); + await promise; assert.equal(label.value, newName); - }) + }); it("should update on attachment title change", async function () { - var file = getTestDataDirectory(); + let file = getTestDataDirectory(); file.append('test.png'); - var item = await Zotero.Attachments.importFromFile({ file }); - var newTitle = 'New Title'; + let item = await Zotero.Attachments.importFromFile({ file }); + let newTitle = 'New Title'; + + let paneHeader = doc.getElementById('zotero-item-pane-header'); + let label = paneHeader.titleField; + let promise = waitForDOMAttributes(label, 'value', (newValue) => { + return newValue === newTitle; + }); + item.setField('title', newTitle); await item.saveTx(); - var itemBox = doc.getElementById('zotero-attachment-box'); - var label = itemBox._id('title'); - assert.equal(label.textContent, newTitle); - }) - }) + await promise; + assert.equal(label.value, newTitle); + }); + }); describe("Note editor", function () {