Fix Item pane "should refresh on file rename" test

This commit is contained in:
windingwind 2024-01-18 12:44:29 +08:00 committed by Dan Stillman
parent 91876e4185
commit 6f466b25bd
2 changed files with 64 additions and 16 deletions

View file

@ -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

View file

@ -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 () {