Fix Item pane "should refresh on file rename" test
This commit is contained in:
parent
91876e4185
commit
6f466b25bd
2 changed files with 64 additions and 16 deletions
|
@ -20,6 +20,42 @@ function waitForDOMEvent(target, event, capture) {
|
||||||
return deferred.promise;
|
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() {
|
async function waitForRecognizer() {
|
||||||
var win = await waitForWindow('chrome://zotero/content/progressQueueDialog.xhtml')
|
var win = await waitForWindow('chrome://zotero/content/progressQueueDialog.xhtml')
|
||||||
// Wait for status to show as complete
|
// Wait for status to show as complete
|
||||||
|
|
|
@ -319,33 +319,45 @@ describe("Item pane", function () {
|
||||||
|
|
||||||
|
|
||||||
describe("Attachment pane", function () {
|
describe("Attachment pane", function () {
|
||||||
it("should refresh on file rename", function* () {
|
it("should refresh on file rename", async function () {
|
||||||
var file = getTestDataDirectory();
|
let file = getTestDataDirectory();
|
||||||
file.append('test.png');
|
file.append('test.png');
|
||||||
var item = yield Zotero.Attachments.importFromFile({
|
let item = await Zotero.Attachments.importFromFile({
|
||||||
file: file
|
file: file
|
||||||
});
|
});
|
||||||
var newName = 'test2.png';
|
let newName = 'test2.png';
|
||||||
yield item.renameAttachmentFile(newName);
|
|
||||||
|
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');
|
await promise;
|
||||||
var label = itemBox._id('fileName');
|
|
||||||
assert.equal(label.value, newName);
|
assert.equal(label.value, newName);
|
||||||
})
|
});
|
||||||
|
|
||||||
it("should update on attachment title change", async function () {
|
it("should update on attachment title change", async function () {
|
||||||
var file = getTestDataDirectory();
|
let file = getTestDataDirectory();
|
||||||
file.append('test.png');
|
file.append('test.png');
|
||||||
var item = await Zotero.Attachments.importFromFile({ file });
|
let item = await Zotero.Attachments.importFromFile({ file });
|
||||||
var newTitle = 'New Title';
|
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);
|
item.setField('title', newTitle);
|
||||||
await item.saveTx();
|
await item.saveTx();
|
||||||
|
|
||||||
var itemBox = doc.getElementById('zotero-attachment-box');
|
await promise;
|
||||||
var label = itemBox._id('title');
|
assert.equal(label.value, newTitle);
|
||||||
assert.equal(label.textContent, newTitle);
|
});
|
||||||
})
|
});
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
describe("Note editor", function () {
|
describe("Note editor", function () {
|
||||||
|
|
Loading…
Reference in a new issue