Commit f6779e35 authored by Robert Knight's avatar Robert Knight

Cleanup DocumentMeta tests

Clean up output from `decaffeinate`.

 - Remove unnecessary `return` statements
 - Rename `Document` to `DocumentMeta` to avoid conflict with global
   `Document` and match the name used in `document.js`
 - Disable ESLint bind warnings. The use here is appropriate.
parent d3e77e53
'use strict'; 'use strict';
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS201: Simplify complex destructure assignments
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const $ = require('jquery'); const $ = require('jquery');
const Document = require('../document'); const DocumentMeta = require('../document');
/* /*
** Adapted from: ** Adapted from:
...@@ -22,12 +15,12 @@ const Document = require('../document'); ...@@ -22,12 +15,12 @@ const Document = require('../document');
** https://github.com/openannotation/annotator/blob/master/LICENSE ** https://github.com/openannotation/annotator/blob/master/LICENSE
*/ */
describe('Document', function() { describe('DocumentMeta', function() {
let testDocument = null; let testDocument = null;
beforeEach(function() { beforeEach(function() {
testDocument = new Document($('<div></div>')[0], {}); testDocument = new DocumentMeta($('<div></div>')[0], {});
return testDocument.pluginInit(); testDocument.pluginInit();
}); });
afterEach(() => $(document).unbind()); afterEach(() => $(document).unbind());
...@@ -86,7 +79,7 @@ describe('Document', function() { ...@@ -86,7 +79,7 @@ describe('Document', function() {
// Link derived from dc resource identifiers in the form of urn:x-dc:<container>/<identifier> // Link derived from dc resource identifiers in the form of urn:x-dc:<container>/<identifier>
// Where <container> is the percent-encoded value of the last dc.relation.ispartof meta element // Where <container> is the percent-encoded value of the last dc.relation.ispartof meta element
// and <identifier> is the percent-encoded value of the last dc.identifier meta element. // and <identifier> is the percent-encoded value of the last dc.identifier meta element.
return assert.equal( assert.equal(
metadata.link[9].href, metadata.link[9].href,
'urn:x-dc:isbn%3A123456789/foobar-abcxyz' 'urn:x-dc:isbn%3A123456789/foobar-abcxyz'
); );
...@@ -98,44 +91,44 @@ describe('Document', function() { ...@@ -98,44 +91,44 @@ describe('Document', function() {
assert.ok(metadata.highwire); assert.ok(metadata.highwire);
assert.deepEqual(metadata.highwire.pdf_url, ['foo.pdf']); assert.deepEqual(metadata.highwire.pdf_url, ['foo.pdf']);
assert.deepEqual(metadata.highwire.doi, ['10.1175/JCLI-D-11-00015.1']); assert.deepEqual(metadata.highwire.doi, ['10.1175/JCLI-D-11-00015.1']);
return assert.deepEqual(metadata.highwire.title, ['Foo']); assert.deepEqual(metadata.highwire.title, ['Foo']);
}); });
it('should have dublincore metadata', function() { it('should have dublincore metadata', function() {
assert.ok(metadata.dc); assert.ok(metadata.dc);
assert.deepEqual(metadata.dc.identifier, ['doi:10.1175/JCLI-D-11-00015.1', 'foobar-abcxyz']); assert.deepEqual(metadata.dc.identifier, ['doi:10.1175/JCLI-D-11-00015.1', 'foobar-abcxyz']);
assert.deepEqual(metadata.dc['relation.ispartof'], ['isbn:123456789']); assert.deepEqual(metadata.dc['relation.ispartof'], ['isbn:123456789']);
return assert.deepEqual(metadata.dc.type, ['Article']); assert.deepEqual(metadata.dc.type, ['Article']);
}); });
it('should have facebook metadata', function() { it('should have facebook metadata', function() {
assert.ok(metadata.facebook); assert.ok(metadata.facebook);
return assert.deepEqual(metadata.facebook.url, ['http://example.com']); assert.deepEqual(metadata.facebook.url, ['http://example.com']);
}); });
it('should have eprints metadata', function() { it('should have eprints metadata', function() {
assert.ok(metadata.eprints); assert.ok(metadata.eprints);
return assert.deepEqual(metadata.eprints.title, ['Computer Lib / Dream Machines']); assert.deepEqual(metadata.eprints.title, ['Computer Lib / Dream Machines']);
}); });
it('should have prism metadata', function() { it('should have prism metadata', function() {
assert.ok(metadata.prism); assert.ok(metadata.prism);
assert.deepEqual(metadata.prism.title, ['Literary Machines']); assert.deepEqual(metadata.prism.title, ['Literary Machines']);
return it('should have twitter card metadata', function() { it('should have twitter card metadata', function() {
assert.ok(metadata.twitter); assert.ok(metadata.twitter);
return assert.deepEqual(metadata.twitter.site, ['@okfn']); assert.deepEqual(metadata.twitter.site, ['@okfn']);
}); });
}); });
it('should have unique uris', function() { it('should have unique uris', function() {
const uris = testDocument.uris(); const uris = testDocument.uris();
return assert.equal(uris.length, 8); assert.equal(uris.length, 8);
}); });
it('uri() returns the canonical uri', function() { it('uri() returns the canonical uri', function() {
const uri = testDocument.uri(); const uri = testDocument.uri();
return assert.equal(uri, metadata.link[5].href); assert.equal(uri, metadata.link[5].href);
}); });
it('should have a favicon', () => it('should have a favicon', () =>
...@@ -145,7 +138,7 @@ describe('Document', function() { ...@@ -145,7 +138,7 @@ describe('Document', function() {
) )
); );
return it('should have a documentFingerprint as the dc resource identifiers URN href', () => assert.equal(metadata.documentFingerprint, metadata.link[9].href)); it('should have a documentFingerprint as the dc resource identifiers URN href', () => assert.equal(metadata.documentFingerprint, metadata.link[9].href));
}); });
...@@ -154,12 +147,12 @@ describe('Document', function() { ...@@ -154,12 +147,12 @@ describe('Document', function() {
it('should add the protocol when the url starts with two slashes', function() { it('should add the protocol when the url starts with two slashes', function() {
const result = testDocument._absoluteUrl('//example.com/'); const result = testDocument._absoluteUrl('//example.com/');
const expected = `${document.location.protocol}//example.com/`; const expected = `${document.location.protocol}//example.com/`;
return assert.equal(result, expected); assert.equal(result, expected);
}); });
it('should add a trailing slash when given an empty path', function() { it('should add a trailing slash when given an empty path', function() {
const result = testDocument._absoluteUrl('http://example.com'); const result = testDocument._absoluteUrl('http://example.com');
return assert.equal(result, 'http://example.com/'); assert.equal(result, 'http://example.com/');
}); });
it('should make a relative path into an absolute url', function() { it('should make a relative path into an absolute url', function() {
...@@ -170,50 +163,50 @@ describe('Document', function() { ...@@ -170,50 +163,50 @@ describe('Document', function() {
document.location.pathname.replace(/[^\/]+$/, '') + document.location.pathname.replace(/[^\/]+$/, '') +
'path' 'path'
); );
return assert.equal(result, expected); assert.equal(result, expected);
}); });
return it('should make an absolute path into an absolute url', function() { it('should make an absolute path into an absolute url', function() {
const result = testDocument._absoluteUrl('/path'); const result = testDocument._absoluteUrl('/path');
const expected = ( const expected = (
document.location.protocol + '//' + document.location.protocol + '//' +
document.location.host + document.location.host +
'/path' '/path'
); );
return assert.equal(result, expected); assert.equal(result, expected);
}); });
}); });
return describe('#uri', function() { describe('#uri', function() {
beforeEach(function() { beforeEach(function() {
// Remove any existing canonical links which would otherwise override the // Remove any existing canonical links which would otherwise override the
// document's own location. // document's own location.
const canonicalLink = document.querySelector('link[rel="canonical"]'); const canonicalLink = document.querySelector('link[rel="canonical"]');
if (canonicalLink) { if (canonicalLink) {
return canonicalLink.remove(); canonicalLink.remove();
} }
}); });
// Create a blank HTML document with a faked `href` and `baseURI` and // Create a blank HTML document with a faked `href` and `baseURI` and
// return a `Document` instance which reads metadata from it. // return a `DocumentMeta` instance which reads metadata from it.
const createDoc = function(href, baseURI, htmlDoc) { const createDoc = function(href, baseURI, htmlDoc) {
if (!htmlDoc) { if (!htmlDoc) {
// Create a blank DOM Document // Create a blank DOM DocumentMeta
htmlDoc = document.implementation.createHTMLDocument(); htmlDoc = document.implementation.createHTMLDocument();
} }
// `Document.location` is not overridable. In order to fake the // `DocumentMeta.location` is not overridable. In order to fake the
// location in tests, create a proxy object in front of our blank HTML // location in tests, create a proxy object in front of our blank HTML
// document. // document.
const fakeDocument = { const fakeDocument = {
createElement: htmlDoc.createElement.bind(htmlDoc), createElement: htmlDoc.createElement.bind(htmlDoc), // eslint-disable-line no-restricted-properties
querySelectorAll: htmlDoc.querySelectorAll.bind(htmlDoc), querySelectorAll: htmlDoc.querySelectorAll.bind(htmlDoc), // eslint-disable-line no-restricted-properties
location: { location: {
href, href,
}, },
}; };
const doc = new Document($('<div></div>')[0], { const doc = new DocumentMeta($('<div></div>')[0], {
document: fakeDocument, document: fakeDocument,
baseURI, baseURI,
}); });
...@@ -229,7 +222,7 @@ describe('Document', function() { ...@@ -229,7 +222,7 @@ describe('Document', function() {
it("should return the document's URL if it has an allowed scheme", function() { it("should return the document's URL if it has an allowed scheme", function() {
const baseURI = 'https://publisher.org/'; const baseURI = 'https://publisher.org/';
const doc = createDoc(href, baseURI); const doc = createDoc(href, baseURI);
return assert.equal(doc.uri(), href); assert.equal(doc.uri(), href);
}) })
); );
...@@ -237,7 +230,7 @@ describe('Document', function() { ...@@ -237,7 +230,7 @@ describe('Document', function() {
const href = 'blob:1234-5678'; const href = 'blob:1234-5678';
const baseURI = 'https://publisher.org/book'; const baseURI = 'https://publisher.org/book';
const doc = createDoc(href, baseURI); const doc = createDoc(href, baseURI);
return assert.equal(doc.uri(), baseURI); assert.equal(doc.uri(), baseURI);
}); });
[ [
...@@ -250,13 +243,13 @@ describe('Document', function() { ...@@ -250,13 +243,13 @@ describe('Document', function() {
['chrome://foo', 'chrome://blah'], ['chrome://foo', 'chrome://blah'],
].forEach(function(...args) { ].forEach(function(...args) {
const [href, baseURI] = Array.from(args[0]); const [href, baseURI] = Array.from(args[0]);
return it("should return the document's URL if it and the baseURI do not have an allowed scheme", function() { it("should return the document's URL if it and the baseURI do not have an allowed scheme", function() {
const doc = createDoc(href, baseURI); const doc = createDoc(href, baseURI);
return assert.equal(doc.uri(), href); assert.equal(doc.uri(), href);
}); });
}); });
return it('returns the canonical URI if present', function() { it('returns the canonical URI if present', function() {
const htmlDoc = document.implementation.createHTMLDocument(); const htmlDoc = document.implementation.createHTMLDocument();
const canonicalLink = htmlDoc.createElement('link'); const canonicalLink = htmlDoc.createElement('link');
canonicalLink.rel = 'canonical'; canonicalLink.rel = 'canonical';
...@@ -265,7 +258,7 @@ describe('Document', function() { ...@@ -265,7 +258,7 @@ describe('Document', function() {
const doc = createDoc('https://publisher.org/not-canonical', null, htmlDoc); const doc = createDoc('https://publisher.org/not-canonical', null, htmlDoc);
return assert.equal(doc.uri(), canonicalLink.href); assert.equal(doc.uri(), canonicalLink.href);
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment