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
83911254
Commit
83911254
authored
Dec 02, 2015
by
Sean Hammond
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for "extract document metadata" code
parent
e15163ee
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
174 additions
and
36 deletions
+174
-36
annotation.js
h/static/scripts/directive/annotation.js
+51
-36
annotation-test.js
h/static/scripts/directive/test/annotation-test.js
+123
-0
No files found.
h/static/scripts/directive/annotation.js
View file @
83911254
...
@@ -3,6 +3,54 @@
...
@@ -3,6 +3,54 @@
var
events
=
require
(
'../events'
);
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.
/** Copy properties from viewModel into domainModel.
*
*
* All top-level properties in viewModel will be copied into domainModel,
* All top-level properties in viewModel will be copied into domainModel,
...
@@ -443,41 +491,7 @@ function AnnotationController(
...
@@ -443,41 +491,7 @@ function AnnotationController(
vm
.
annotationURI
=
new
URL
(
vm
.
annotationURI
=
new
URL
(
'/a/'
+
vm
.
annotation
.
id
,
vm
.
baseURI
).
href
;
'/a/'
+
vm
.
annotation
.
id
,
vm
.
baseURI
).
href
;
// Extract the document metadata.
vm
.
document
=
extractDocumentMetadata
(
model
);
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
)
+
'…'
;
}
// Form the tags for ngTagsInput.
// Form the tags for ngTagsInput.
vm
.
annotation
.
tags
=
(
vm
.
annotation
.
tags
||
[]).
map
(
function
(
tag
)
{
vm
.
annotation
.
tags
=
(
vm
.
annotation
.
tags
||
[]).
map
(
function
(
tag
)
{
...
@@ -709,8 +723,9 @@ module.exports = {
...
@@ -709,8 +723,9 @@ module.exports = {
// to be unit tested.
// to be unit tested.
// FIXME: The code should be refactored to enable unit testing without having
// FIXME: The code should be refactored to enable unit testing without having
// to do this.
// to do this.
validate
:
validate
,
extractDocumentMetadata
:
extractDocumentMetadata
,
updateDomainModel
:
updateDomainModel
,
updateDomainModel
:
updateDomainModel
,
validate
:
validate
,
// These are meant to be the public API of this module.
// These are meant to be the public API of this module.
directive
:
annotation
,
directive
:
annotation
,
...
...
h/static/scripts/directive/test/annotation-test.js
View file @
83911254
...
@@ -184,6 +184,129 @@ describe('annotation', function() {
...
@@ -184,6 +184,129 @@ describe('annotation', function() {
sandbox
.
restore
();
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
()
{
describe
(
'updateDomainModel()'
,
function
()
{
var
updateDomainModel
=
require
(
'../annotation'
).
updateDomainModel
;
var
updateDomainModel
=
require
(
'../annotation'
).
updateDomainModel
;
...
...
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