Skip to content

Commit

Permalink
Add unit tests for EMLDistribution model
Browse files Browse the repository at this point in the history
Issue #1380
  • Loading branch information
robyngit committed Jul 31, 2023
1 parent 9409832 commit 7262f66
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/js/models/metadata/eml211/EMLDistribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
}
});

// If there is no distribution location, return the DOM
if (!distLocation) return objectDOM;

// Add the distribution location if it doesn't exist
if (!$objectDOM.find(distLocation).length) {
$objectDOM.append(`<${distLocation}></${distLocation}>`);
Expand Down Expand Up @@ -257,7 +260,7 @@ define(["jquery", "underscore", "backbone", "models/DataONEObject"], function (
},

trickleUpChange: function () {
MetacatUI.rootDataPackage.packageModel.set("changed", true);
MetacatUI.rootDataPackage?.packageModel?.set("changed", true);
},

formatXML: function (xmlString) {
Expand Down
1 change: 1 addition & 0 deletions test/config/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"./js/specs/unit/models/metadata/eml211/EMLOtherEntity.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLParty.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLTemporalCoverage.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLDistribution.spec.js",
"./js/specs/unit/models/maps/assets/CesiumImagery.spec.js",
"./js/specs/unit/collections/maps/Geohashes.spec.js",
"./js/specs/unit/models/connectors/Filters-Map.spec.js",
Expand Down
189 changes: 189 additions & 0 deletions test/js/specs/unit/models/metadata/eml211/EMLDistribution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
define([
"../../../../../../../../src/js/models/metadata/eml211/EMLDistribution",
], function (EMLDistribution) {
// Configure the Chai assertion library
var should = chai.should();
var expect = chai.expect;

describe("EMLDistribution Test Suite", function () {
/* Set up */
beforeEach(function () {
this.emlDistribution = new EMLDistribution();
});

/* Tear down */
afterEach(function () {
delete this.emlDistribution;
});

describe("Initialization", function () {
it("should create a EMLDistribution instance", function () {
new EMLDistribution().should.be.instanceof(EMLDistribution);
});
});

describe("Parse", function () {
it("should parse online URL from EMLDistribution model", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution>" +
" <online>" +
" <url>http://www.dataone.org</url>" +
" </online>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.get("url").should.equal("http://www.dataone.org");
});

it("should parse offline elements from EMLDistribution model", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution>" +
" <offline>" +
" <mediumName>CD-ROM</mediumName>" +
" <mediumVolume>1</mediumVolume>" +
" <mediumFormat>ISO9660</mediumFormat>" +
" <mediumNote>Some notes</mediumNote>" +
" </offline>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.get("mediumName").should.equal("CD-ROM");
emlDistribution.get("mediumVolume").should.equal("1");
emlDistribution.get("mediumFormat").should.equal("ISO9660");
emlDistribution.get("mediumNote").should.equal("Some notes");
});
});

describe("Update DOM", function () {
it("should update the DOM with the new values", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution>" +
" <online>" +
" <url>http://www.dataone.org</url>" +
" </online>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.set("url", "http://www.dataone.org/updated");

var updatedDOM = emlDistribution.updateDOM();

updatedDOM
.querySelector("url")
.textContent.should.equal("http://www.dataone.org/updated");
});

it("should create a new node if one does not exist", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution>" +
" <offline>" +
" <mediumName>CD-ROM</mediumName>" +
" </offline>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.set("mediumName", "CD-ROM");

var updatedDOM = emlDistribution.updateDOM();

updatedDOM
.querySelector("mediumName")
.textContent.should.equal("CD-ROM");
});

it("should create a DOM if one doesn't exist", function () {
var emlDistribution = new EMLDistribution({
mediumName: "CD-ROM",
});

var updatedDOM = emlDistribution.updateDOM();

updatedDOM
.querySelector("mediumName")
.textContent.should.equal("CD-ROM");
// check that mediumName is within the offline node
updatedDOM.querySelector("offline > mediumName").should.not.equal(null);
});

it("should remove nodes if the value is empty", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution>" +
" <online>" +
" <url>http://www.dataone.org</url>" +
" </online>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.set("url", "");

var updatedDOM = emlDistribution.updateDOM();
expect(updatedDOM.querySelector("url")).to.equal(null);
});

it("should not remove id, system, nor scope attributes from the distribution node", function () {
var objectDOM = new DOMParser().parseFromString(
"<distribution id='123' system='eml' scope='system'>" +
" <online>" +
" <url>http://www.dataone.org</url>" +
" </online>" +
"</distribution>",
"text/xml"
).documentElement;

var emlDistribution = new EMLDistribution(
{
objectDOM: objectDOM,
},
{ parse: true }
);

emlDistribution.set("url", "");

var updatedDOM = emlDistribution.updateDOM();
expect(updatedDOM.getAttribute("id")).to.equal("123");
expect(updatedDOM.getAttribute("system")).to.equal("eml");
expect(updatedDOM.getAttribute("scope")).to.equal("system");
});
});
});
});

0 comments on commit 7262f66

Please sign in to comment.