Group strict-mode and non-strict-mode tests for Item::fromJSON()

This commit is contained in:
Dan Stillman 2020-02-09 12:44:40 -05:00
parent b41734924d
commit 11caa1b719

View file

@ -1754,18 +1754,6 @@ describe("Zotero.Item", function () {
assert.isFalse(item.inPublications); assert.isFalse(item.inPublications);
}); });
it("should handle Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
extra: "Here's some extra text"
};
var item = new Zotero.Item();
item.fromJSON(json);
assert.equal(item.getField('extra'), json.extra);
});
// Not currently following this behavior // Not currently following this behavior
/*it("should move valid field in Extra to field if not set", function () { /*it("should move valid field in Extra to field if not set", function () {
var doi = '10.1234/abcd'; var doi = '10.1234/abcd';
@ -1822,132 +1810,147 @@ describe("Zotero.Item", function () {
assert.equal(item.getField('extra'), json.extra); assert.equal(item.getField('extra'), json.extra);
}); });
it("should store unknown fields in Extra in non-strict mode", function () { describe("not-strict mode", function () {
var json = { it("should handle Extra in non-strict mode", function () {
itemType: "journalArticle", var json = {
title: "Test", itemType: "journalArticle",
fooBar: "123", title: "Test",
testField: "test value" extra: "Here's some extra text"
}; };
var item = new Zotero.Item; var item = new Zotero.Item();
item.fromJSON(json); item.fromJSON(json);
assert.equal(item.getField('title'), 'Test'); assert.equal(item.getField('extra'), json.extra);
assert.equal(item.getField('extra'), 'Foo Bar: 123\nTest Field: test value'); });
it("should store unknown fields in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
fooBar: "123",
testField: "test value"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo Bar: 123\nTest Field: test value');
});
it("should replace unknown field in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
foo: "BBB",
extra: "Foo: AAA\nBar: CCC"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo: BBB\nBar: CCC');
});
it("should store invalid-for-type field in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
medium: "123"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Medium: 123');
});
it("should ignore invalid-for-type base-mapped field if valid-for-type base field is set in Extra", function () {
var json = {
itemType: "document",
publisher: "Foo", // Valid for 'document'
company: "Bar" // Not valid for 'document', but mapped to base field 'publisher'
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('publisher'), 'Foo');
assert.equal(item.getField('extra'), '');
});
it("shouldn't include base field or invalid base-mapped field in Extra if valid base-mapped field is set", function () {
var json = {
itemType: "audioRecording",
publisher: "A", // Base field, which will be overwritten by the valid base-mapped field
label: "B", // Valid base-mapped field, which should be stored
company: "C", // Invalid base-mapped field, which should be ignored
foo: "D" // Invalid other field, which should be added to Extra
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('label'), 'B');
assert.equal(item.getField('extra'), 'Foo: D');
});
}); });
it("should replace unknown field in Extra in non-strict mode", function () { describe("strict mode", function () {
var json = { it("should throw on unknown field", function () {
itemType: "journalArticle", var json = {
title: "Test", itemType: "journalArticle",
foo: "BBB", title: "Test",
extra: "Foo: AAA\nBar: CCC" foo: "Bar"
}; };
var item = new Zotero.Item; var item = new Zotero.Item;
item.fromJSON(json); var f = () => {
assert.equal(item.getField('title'), 'Test'); item.fromJSON(json, { strict: true });
assert.equal(item.getField('extra'), 'Foo: BBB\nBar: CCC'); };
}); assert.throws(f, /^Unknown field/);
});
it("should store invalid-for-type field in Extra in non-strict mode", function () {
var json = { it("should throw on invalid field for a given item type", function () {
itemType: "journalArticle", var json = {
title: "Test", itemType: "journalArticle",
medium: "123" title: "Test",
}; numPages: "123"
var item = new Zotero.Item; };
item.fromJSON(json); var item = new Zotero.Item;
assert.equal(item.getField('title'), 'Test'); var f = () => {
assert.equal(item.getField('extra'), 'Medium: 123'); item.fromJSON(json, { strict: true });
}); };
assert.throws(f, /^Invalid field/);
it("should ignore invalid-for-type base-mapped field if valid-for-type base field is set in Extra in non-strict mode", function () { });
var json = {
itemType: "document", it("should throw on unknown creator type", function () {
publisher: "Foo", // Valid for 'document' var json = {
company: "Bar" // Not invalid for 'document', but mapped to base field 'publisher' itemType: "journalArticle",
}; title: "Test",
var item = new Zotero.Item; creators: [
item.fromJSON(json); {
assert.equal(item.getField('publisher'), 'Foo'); firstName: "First",
assert.equal(item.getField('extra'), ''); lastName: "Last",
}); creatorType: "unknown"
}
it("shouldn't include base field or invalid base-mapped field in Extra if valid base-mapped field is set in non-strict mode", function () { ]
var json = { };
itemType: "audioRecording", var item = new Zotero.Item;
publisher: "A", // Base field, which will be overwritten by the valid base-mapped field var f = () => {
label: "B", // Valid base-mapped field, which should be stored item.fromJSON(json, { strict: true });
company: "C", // Invalid base-mapped field, which should be ignored };
foo: "D" // Invalid other field, which should be added to Extra assert.throws(f, /^Unknown creator type/);
}; });
var item = new Zotero.Item;
item.fromJSON(json); it("should throw on invalid creator type for a given item type", function () {
assert.equal(item.getField('label'), 'B'); var json = {
assert.equal(item.getField('extra'), 'Foo: D'); itemType: "journalArticle",
}); title: "Test",
creators: [
it("should throw on unknown field in strict mode", function () { {
var json = { firstName: "First",
itemType: "journalArticle", lastName: "Last",
title: "Test", creatorType: "interviewee"
foo: "Bar" }
}; ]
var item = new Zotero.Item; };
var f = () => { var item = new Zotero.Item;
item.fromJSON(json, { strict: true }); var f = () => {
}; item.fromJSON(json, { strict: true });
assert.throws(f, /^Unknown field/); };
}); assert.throws(f, /^Invalid creator type/);
});
it("should throw on invalid field for a given item type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
numPages: "123"
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid field/);
});
it("should throw on unknown creator type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "unknown"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Unknown creator type/);
});
it("should throw on invalid creator type for a given item type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "interviewee"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid creator type/);
}); });
it("should accept ISO 8601 dates", function* () { it("should accept ISO 8601 dates", function* () {