Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
d6903a9d
Commit
d6903a9d
authored
Dec 02, 2015
by
Sean Hammond
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for updateDomainModel()
parent
bd3b5cbb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
13 deletions
+83
-13
annotation.js
h/static/scripts/directive/annotation.js
+32
-13
annotation-test.js
h/static/scripts/directive/test/annotation-test.js
+51
-0
No files found.
h/static/scripts/directive/annotation.js
View file @
d6903a9d
...
...
@@ -3,6 +3,30 @@
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.
*
* A public annotation has to have some text and/or some tags to be valid,
...
...
@@ -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
* the text and tags from the domain model in `draft`.
...
...
@@ -695,7 +706,15 @@ function annotation($document, features) {
}
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
,
updateDomainModel
:
updateDomainModel
,
// These are meant to be the public API of this module.
directive
:
annotation
,
Controller
:
AnnotationController
};
h/static/scripts/directive/test/annotation-test.js
View file @
d6903a9d
...
...
@@ -184,6 +184,57 @@ describe('annotation', function() {
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
()
{
beforeEach
(
function
()
{
annotation
.
$highlight
=
true
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment