Commit 83911254 authored by Sean Hammond's avatar Sean Hammond

Add tests for "extract document metadata" code

parent e15163ee
......@@ -3,6 +3,54 @@
var events = require('../events');
/** Extract a URI, domain and title from the given domain model object.
*
* @param {object} model An annotation domain model object as received from the
* server-side API.
* @returns {object} An object with three properties extracted from the model:
* uri, domain and title.
*
*/
function extractDocumentMetadata(model) {
var document_;
var uri = model.uri;
var domain = new URL(uri).hostname;
if (model.document) {
if (uri.indexOf('urn') === 0) {
var i;
for (i = 0; i < model.document.link.length; i++) {
var link = model.document.link[i];
if (!(link.href.indexOf('urn'))) {
continue;
}
uri = link.href;
break;
}
}
var documentTitle = Array.isArray(
model.document.title) ? model.document.title[0] : model.document.title;
document_ = {
uri: uri,
domain: domain,
title: documentTitle || domain
};
} else {
document_ = {
uri: uri,
domain: domain,
title: domain
};
}
if (document_.title.length > 30) {
document_.title = document_.title.slice(0, 30) + '…';
}
return document_;
}
/** Copy properties from viewModel into domainModel.
*
* All top-level properties in viewModel will be copied into domainModel,
......@@ -443,41 +491,7 @@ function AnnotationController(
vm.annotationURI = new URL(
'/a/' + vm.annotation.id, vm.baseURI).href;
// Extract the document metadata.
var uri = model.uri;
var domain = new URL(uri).hostname;
if (model.document) {
if (uri.indexOf('urn') === 0) {
var i;
for (i = 0; i < model.document.link.length; i++) {
var link = model.document.link[i];
if (!(link.href.indexOf('urn'))) {
continue;
}
uri = link.href;
break;
}
}
var documentTitle = Array.isArray(
model.document.title) ? model.document.title[0] : model.document.title;
vm.document = {
uri: uri,
domain: domain,
title: documentTitle || domain
};
} else {
vm.document = {
uri: uri,
domain: domain,
title: domain
};
}
if (vm.document.title.length > 30) {
vm.document.title = vm.document.title.slice(0, 30) + '…';
}
vm.document = extractDocumentMetadata(model);
// Form the tags for ngTagsInput.
vm.annotation.tags = (vm.annotation.tags || []).map(function(tag) {
......@@ -709,8 +723,9 @@ module.exports = {
// to be unit tested.
// FIXME: The code should be refactored to enable unit testing without having
// to do this.
validate: validate,
extractDocumentMetadata: extractDocumentMetadata,
updateDomainModel: updateDomainModel,
validate: validate,
// These are meant to be the public API of this module.
directive: annotation,
......
......@@ -184,6 +184,129 @@ describe('annotation', function() {
sandbox.restore();
});
describe('extractDocumentMetadata()', function() {
var extractDocumentMetadata = require('../annotation')
.extractDocumentMetadata;
context('when the model has a document property', function() {
it('returns the hostname from model.uri as the domain', function() {
var model = {
document: {},
uri: 'http://example.com/'
};
assert.equal(extractDocumentMetadata(model).domain, 'example.com');
});
context('when model.uri starts with "urn"', function() {
it(
'uses the first document.link uri that doesn\'t start with "urn"',
function() {
var model = {
uri: 'urn:isbn:0451450523',
document: {
link: [
{href: 'urn:isan:0000-0000-9E59-0000-O-0000-0000-2'},
{href: 'http://example.com/'}
]
}
};
assert.equal(
extractDocumentMetadata(model).uri, 'http://example.com/');
}
);
});
context('when model.uri does not start with "urn"', function() {
it('uses model.uri as the uri', function() {
var model = {
document: {},
uri: 'http://example.com/'
};
assert.equal(
extractDocumentMetadata(model).uri, 'http://example.com/');
});
});
context('when document.title is a string', function() {
it('returns document.title as title', function() {
var model = {
uri: 'http://example.com/',
document: {
title: 'My Document'
}
};
assert.equal(
extractDocumentMetadata(model).title, model.document.title);
});
});
context('when document.title is an array', function() {
it('returns document.title[0] as title', function() {
var model = {
uri: 'http://example.com/',
document: {
title: ['My Document', 'My Other Document']
}
};
assert.equal(
extractDocumentMetadata(model).title, model.document.title[0]);
});
});
context('when there is no document.title', function() {
it('returns the domain as the title', function() {
var model = {
document: {},
uri: 'http://example.com/',
};
assert.equal(extractDocumentMetadata(model).title, 'example.com');
});
});
});
context('when the model does not have a document property', function() {
it('returns model.uri for the uri', function() {
var model = {uri: 'http://example.com/'};
assert.equal(extractDocumentMetadata(model).uri, model.uri);
});
it('returns the hostname of model.uri for the domain', function() {
var model = {uri: 'http://example.com/'};
assert.equal(extractDocumentMetadata(model).domain, 'example.com');
});
it('returns the hostname of model.uri for the title', function() {
var model = {uri: 'http://example.com/'};
assert.equal(extractDocumentMetadata(model).title, 'example.com');
});
});
context('when the title is longer than 30 characters', function() {
it('truncates the title with "…"', function() {
var model = {
uri: 'http://example.com/',
document: {
title: 'My Really Really Long Document Title'
}
};
assert.equal(
extractDocumentMetadata(model).title,
'My Really Really Long Document…'
);
});
});
});
describe('updateDomainModel()', function() {
var updateDomainModel = require('../annotation').updateDomainModel;
......
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