Commit d6903a9d authored by Sean Hammond's avatar Sean Hammond

Add tests for updateDomainModel()

parent bd3b5cbb
...@@ -3,6 +3,30 @@ ...@@ -3,6 +3,30 @@
var events = require('../events'); var events = require('../events');
/** Copy properties from viewModel into domainModel.
*
* All top-level properties in viewModel will be copied into domainModel,
* overwriting any existing properties with the same keys in domainModel.
*
* Additionally, the `tags` property of viewModel - an array of objects
* each with a `text` string - will become a simple array of strings in
* domainModel.
*
* @param {object} domainModel The object to copy properties to
* @param {object} viewModel The object to copy properties from
* @returns undefined
*
*/
function updateDomainModel(domainModel, viewModel) {
var i;
var tagTexts = [];
for (i = 0; i < viewModel.tags.length; i++) {
tagTexts[tagTexts.length] = viewModel.tags[i].text;
}
angular.extend(domainModel, viewModel, {tags: tagTexts});
}
/** Return truthy if the given annotation is valid, falsy otherwise. /** Return truthy if the given annotation is valid, falsy otherwise.
* *
* A public annotation has to have some text and/or some tags to be valid, * A public annotation has to have some text and/or some tags to be valid,
...@@ -294,19 +318,6 @@ function AnnotationController( ...@@ -294,19 +318,6 @@ function AnnotationController(
} }
}; };
/**
* Update the given annotation domain model object with the data from the
* given annotation view model object.
*/
function updateDomainModel(domainModel, viewModel) {
var i;
var tagTexts = [];
for (i = 0; i < viewModel.tags.length; i++) {
tagTexts[tagTexts.length] = viewModel.tags[i].text;
}
angular.extend(domainModel, viewModel, {tags: tagTexts});
}
/** /**
* Create or update the existing draft for this annotation using * Create or update the existing draft for this annotation using
* the text and tags from the domain model in `draft`. * the text and tags from the domain model in `draft`.
...@@ -695,7 +706,15 @@ function annotation($document, features) { ...@@ -695,7 +706,15 @@ function annotation($document, features) {
} }
module.exports = { module.exports = {
// These private helper functions aren't meant to be part of the public
// interface of this module. They've been exported temporarily to enable them
// to be unit tested.
// FIXME: The code should be refactored to enable unit testing without having
// to do this.
validate: validate, validate: validate,
updateDomainModel: updateDomainModel,
// These are meant to be the public API of this module.
directive: annotation, directive: annotation,
Controller: AnnotationController Controller: AnnotationController
}; };
...@@ -184,6 +184,57 @@ describe('annotation', function() { ...@@ -184,6 +184,57 @@ describe('annotation', function() {
sandbox.restore(); sandbox.restore();
}); });
describe('updateDomainModel()', function() {
var updateDomainModel = require('../annotation').updateDomainModel;
it('copies top-level keys form viewModel into domainModel', function() {
var domainModel = {};
var viewModel = {foo: 'bar', tags: []};
updateDomainModel(domainModel, viewModel);
assert.equal(domainModel.foo, viewModel.foo);
});
it('overwrites existing keys in domainModel', function() {
var domainModel = {foo: 'foo'};
var viewModel = {foo: 'bar', tags: []};
updateDomainModel(domainModel, viewModel);
assert.equal(domainModel.foo, viewModel.foo);
});
it('doesn\'t touch other properties in domainModel', function() {
var domainModel = {foo: 'foo', bar: 'bar'};
var viewModel = {foo: 'FOO', tags: []};
updateDomainModel(domainModel, viewModel);
assert.equal(
domainModel.bar, 'bar',
'updateDomainModel() should not touch properties of domainModel' +
'that don\'t exist in viewModel');
});
it('copies tag texts from viewModel into domainModel', function() {
var domainModel = {};
var viewModel = {
tags: [
{text: 'foo'},
{text: 'bar'}
]
};
updateDomainModel(domainModel, viewModel);
assert.deepEqual(
domainModel.tags, ['foo', 'bar'],
'The array of {tag: "text"} objects in viewModel becomes an array ' +
'of "text" strings in domainModel');
});
});
describe('when the annotation is a highlight', function() { describe('when the annotation is a highlight', function() {
beforeEach(function() { beforeEach(function() {
annotation.$highlight = true; annotation.$highlight = true;
......
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