Add an integration test for citation merging

This commit is contained in:
Adomas Venčkauskas 2022-10-25 14:50:22 +03:00
parent 19558a425b
commit 0d1b194b6b

View file

@ -25,6 +25,7 @@ describe("Zotero.Integration", function () {
this.supportsImportExport = true; this.supportsImportExport = true;
// Will allow inserting notes // Will allow inserting notes
this.supportsTextInsertion = true; this.supportsTextInsertion = true;
this.supportsCitationMerging = false;
this.fields = []; this.fields = [];
}; };
DocumentPluginDummy.Application.prototype = { DocumentPluginDummy.Application.prototype = {
@ -186,6 +187,7 @@ describe("Zotero.Integration", function () {
// insertBibliography will fail if there is no placeholder text. // insertBibliography will fail if there is no placeholder text.
this.text = '{Placeholder}'; this.text = '{Placeholder}';
this.idx = idx; this.idx = idx;
this.adjacent = false;
this.wrappedJSObject = this; this.wrappedJSObject = this;
}; };
DocumentPluginDummy.Field.noteIndex = 0; DocumentPluginDummy.Field.noteIndex = 0;
@ -235,6 +237,12 @@ describe("Zotero.Integration", function () {
* @returns {Number} * @returns {Number}
*/ */
getNoteIndex: () => 0, getNoteIndex: () => 0,
/**
* Whether this field is adjacent to the next field (meaning they can be merged).
* @returns {boolean}
*/
isAdjacentToNextField: function() { return this.adjacent },
}; };
// Processing functions for logging and promisification // Processing functions for logging and promisification
@ -336,6 +344,28 @@ describe("Zotero.Integration", function () {
}; };
} }
async function insertMultipleCitations() {
var docID = this.test.fullTitle();
if (!(docID in applications)) await initDoc(docID);
var doc = applications[docID].doc;
setAddEditItems(testItems[0]);
await execCommand('addEditCitation', docID);
assert.equal(doc.fields.length, 1);
var citation = await (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
assert.equal(citation.citationItems.length, 1);
assert.equal(citation.citationItems[0].id, testItems[0].id);
setAddEditItems(testItems.slice(1, 3));
await execCommand('addEditCitation', docID);
assert.equal(doc.fields.length, 2);
citation = await (new Zotero.Integration.CitationField(doc.fields[1], doc.fields[1].code)).unserialize();
assert.equal(citation.citationItems.length, 2);
for (let i = 1; i < 3; i++) {
assert.equal(citation.citationItems[i-1].id, testItems[i].id);
}
};
before(function* () { before(function* () {
yield Zotero.Styles.init(); yield Zotero.Styles.init();
yield Zotero.Styles.install({file: stylePath}, styleID, true); yield Zotero.Styles.install({file: stylePath}, styleID, true);
@ -489,27 +519,6 @@ describe("Zotero.Integration", function () {
}); });
describe('#addEditCitation', function() { describe('#addEditCitation', function() {
var insertMultipleCitations = Zotero.Promise.coroutine(function *() {
var docID = this.test.fullTitle();
if (!(docID in applications)) yield initDoc(docID);
var doc = applications[docID].doc;
setAddEditItems(testItems[0]);
yield execCommand('addEditCitation', docID);
assert.equal(doc.fields.length, 1);
var citation = yield (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
assert.equal(citation.citationItems.length, 1);
assert.equal(citation.citationItems[0].id, testItems[0].id);
setAddEditItems(testItems.slice(1, 3));
yield execCommand('addEditCitation', docID);
assert.equal(doc.fields.length, 2);
citation = yield (new Zotero.Integration.CitationField(doc.fields[1], doc.fields[1].code)).unserialize();
assert.equal(citation.citationItems.length, 2);
for (let i = 1; i < 3; i++) {
assert.equal(citation.citationItems[i-1].id, testItems[i].id);
}
});
it('should insert citation if not in field', insertMultipleCitations); it('should insert citation if not in field', insertMultipleCitations);
it('should edit citation if in citation field', function* () { it('should edit citation if in citation field', function* () {
@ -940,8 +949,8 @@ describe("Zotero.Integration", function () {
}); });
describe('#refresh', function() { describe('#refresh', function() {
var docID = this.fullTitle();
it ('should properly disambiguate author after editing in the database', async function () { it ('should properly disambiguate author after editing in the database', async function () {
var docID = this.test.fullTitle();
let testItem1 = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID}); let testItem1 = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});
testItem1.setField('title', `title1`); testItem1.setField('title', `title1`);
testItem1.setCreator(0, {creatorType: 'author', firstName: "Foo", lastName: "Bar"}); testItem1.setCreator(0, {creatorType: 'author', firstName: "Foo", lastName: "Bar"});
@ -954,9 +963,24 @@ describe("Zotero.Integration", function () {
await initDoc(docID); await initDoc(docID);
await execCommand('addEditCitation', docID); await execCommand('addEditCitation', docID);
assert.equal(applications[docID].doc.fields[0].text, '(Bar, 2022a, 2022b)'); assert.equal(applications[docID].doc.fields[0].text, '(Bar, 2022a, 2022b)');
testItem2.setCreator(0, {creatorType: 'author', firstName: "Foo F", lastName: "Bar"}); testItem2.setCreator(0, {creatorType: 'author', firstName: "Foo F", lastName: "Bar"});
await execCommand('refresh', docID); await execCommand('refresh', docID);
assert.equal(applications[docID].doc.fields[0].text, '(F. Bar, 2022; F. F. Bar, 2022)'); assert.equal(applications[docID].doc.fields[0].text, '(F. Bar, 2022; F. F. Bar, 2022)');
testItem2.setCreator(0, {creatorType: 'author', firstName: "Foo", lastName: "Bar"});
await execCommand('refresh', docID);
assert.equal(applications[docID].doc.fields[0].text, '(Bar, 2022a, 2022b)');
});
it ('should merge adjacent fields', async function () {
await insertMultipleCitations.call(this);
var docID = this.test.fullTitle();
assert.equal(applications[docID].doc.fields.length, 2);
applications[docID].doc.fields[0].adjacent = true;
await execCommand('refresh', docID);
assert.equal(applications[docID].doc.fields.length, 1);
}); });
}); });
}); });