Commit 6b10b936 authored by Sean Hammond's avatar Sean Hammond

Remove some test code duplication

parent fcf7fa1b
...@@ -320,6 +320,22 @@ describe('annotation.js', function() { ...@@ -320,6 +320,22 @@ describe('annotation.js', function() {
var fakeUrlEncodeFilter; var fakeUrlEncodeFilter;
var sandbox; var sandbox;
function createDirective(annotation) {
annotation = annotation || defaultAnnotation();
$scope.annotation = annotation;
var element = angular.element('<div annotation="annotation">');
compileService()(element)($scope);
$scope.$digest();
var controller = element.controller('annotation');
var scope = element.isolateScope();
return {
annotation: annotation,
controller: controller,
element: element,
scope: scope
};
}
/** Return the default domain model object that createDirective() uses if /** Return the default domain model object that createDirective() uses if
* no custom one is passed to it. */ * no custom one is passed to it. */
function defaultAnnotation() { function defaultAnnotation() {
...@@ -334,19 +350,85 @@ describe('annotation.js', function() { ...@@ -334,19 +350,85 @@ describe('annotation.js', function() {
}; };
} }
function createDirective(annotation) { /** Return an annotation domain model object for a new annotation
annotation = annotation || defaultAnnotation(); * (newly-created client-side, not yet saved to the server).
$scope.annotation = annotation; */
var element = angular.element('<div annotation="annotation">'); function newAnnotation() {
compileService()(element)($scope); // A new annotation won't have any saved drafts yet.
$scope.$digest(); fakeDrafts.get.returns(null);
var controller = element.controller('annotation');
var scope = element.isolateScope();
return { return {
annotation: annotation, id: undefined,
controller: controller, $highlight: undefined,
element: element, target: ['foo', 'bar'],
scope: scope references: [],
text: 'Annotation text',
tags: ['tag_1', 'tag_2']
};
}
/** Return an annotation domain model object for a new highlight
* (newly-created client-side, not yet saved to the server).
*/
function newHighlight() {
// A new highlight won't have any saved drafts yet.
fakeDrafts.get.returns(null);
return {
id: undefined,
$highlight: true
};
}
/** Return an annotation domain model object for an existing annotation
* received from the server.
*/
function oldAnnotation() {
return {
id: 'annotation_id',
$highlight: undefined,
target: ['foo', 'bar'],
references: [],
text: 'This is my annotation',
tags: ['tag_1', 'tag_2']
};
}
/** Return an annotation domain model object for an existing highlight
* received from the server.
*/
function oldHighlight() {
return {
id: 'annotation_id',
$highlight: undefined,
target: ['foo', 'bar'],
references: [],
text: '',
tags: []
};
}
/** Return an annotation domain model object for an existing page note
* received from the server.
*/
function oldPageNote() {
return {
highlight: undefined,
target: [],
references: [],
text: '',
tags: []
};
}
/** Return an annotation domain model object for an existing reply
* received from the server.
*/
function oldReply() {
return {
highlight: undefined,
target: ['foo'],
references: ['parent_annotation_id'],
text: '',
tags: []
}; };
} }
...@@ -479,14 +561,9 @@ describe('annotation.js', function() { ...@@ -479,14 +561,9 @@ describe('annotation.js', function() {
describe('AnnotationController() initialization', function() { describe('AnnotationController() initialization', function() {
it('saves new highlights to the server on initialization', function() { it('saves new highlights to the server on initialization', function() {
var annotation = defaultAnnotation(); var annotation = newHighlight();
// New highlights have no id and have $highlight: true.
annotation.id = null;
annotation.$highlight = true;
// The user is logged-in. // The user is logged-in.
annotation.user = fakeSession.state.userid = 'acct:bill@localhost'; annotation.user = fakeSession.state.userid = 'acct:bill@localhost';
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
}); });
...@@ -497,14 +574,9 @@ describe('annotation.js', function() { ...@@ -497,14 +574,9 @@ describe('annotation.js', function() {
}); });
it('saves new highlights to drafts if not logged in', function() { it('saves new highlights to drafts if not logged in', function() {
var annotation = defaultAnnotation(); var annotation = newHighlight();
// New highlights have no id and have $highlight: true.
annotation.id = null;
annotation.$highlight = true;
// The user is not logged-in. // The user is not logged-in.
annotation.user = fakeSession.state.userid = undefined; annotation.user = fakeSession.state.userid = undefined;
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
}); });
...@@ -516,11 +588,7 @@ describe('annotation.js', function() { ...@@ -516,11 +588,7 @@ describe('annotation.js', function() {
}); });
it('does not save new annotations on initialization', function() { it('does not save new annotations on initialization', function() {
var annotation = defaultAnnotation(); var annotation = newAnnotation();
// New annotations have no id and no $highlight.
annotation.id = null;
annotation.$highlight = undefined;
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
}); });
...@@ -531,20 +599,7 @@ describe('annotation.js', function() { ...@@ -531,20 +599,7 @@ describe('annotation.js', function() {
}); });
it('does not save old highlights on initialization', function() { it('does not save old highlights on initialization', function() {
var annotation = defaultAnnotation(); var annotation = oldHighlight();
// Old highlights (ones that the client has received from the server,
// rather than created locally itself) have an id and do not have any
// $highlight.
annotation.id = 'annotation_id';
annotation.$highlight = undefined;
// If it's a highlight, then the annotation will have a target but
// no references, text or tags.
annotation.target = ['foo', 'bar'];
annotation.references = [];
annotation.text = '';
annotation.tags = [];
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
}); });
...@@ -555,21 +610,7 @@ describe('annotation.js', function() { ...@@ -555,21 +610,7 @@ describe('annotation.js', function() {
}); });
it('does not save old annotations on initialization', function() { it('does not save old annotations on initialization', function() {
var annotation = defaultAnnotation(); var annotation = oldAnnotation();
// Old annotations (ones that the client has received from the server,
// rather than created locally itself) have an id and do not have any
// $highlight.
annotation.id = 'annotation_id';
annotation.$highlight = undefined;
// If it's an annotation (rather than a highlight, reply or page note)
// then the annotation will have a target, no references and some
// text or tags.
annotation.target = ['foo', 'bar'];
annotation.references = [];
annotation.text = 'This is my annotation';
annotation.tags = ['tag_1', 'tag_2'];
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
}); });
...@@ -580,20 +621,7 @@ describe('annotation.js', function() { ...@@ -580,20 +621,7 @@ describe('annotation.js', function() {
}); });
it('edits new annotations on initialization', function() { it('edits new annotations on initialization', function() {
var annotation = defaultAnnotation(); var annotation = newAnnotation();
// When the user creates a new annotation and we create a new
// AnnotationController instance, we automatically open the
// annotation's editor.
// A new annotation will have no id or $highlight.
annotation.id = annotation.$highlight = undefined;
// A new annotation won't have any text or tags yet.
annotation.text = '';
annotation.tags = [];
// A new annotation won't have any saved drafts yet.
fakeDrafts.get.returns(null);
var controller = createDirective(annotation).controller; var controller = createDirective(annotation).controller;
...@@ -601,13 +629,7 @@ describe('annotation.js', function() { ...@@ -601,13 +629,7 @@ describe('annotation.js', function() {
}); });
it('edits annotations with drafts on initialization', function() { it('edits annotations with drafts on initialization', function() {
var annotation = defaultAnnotation(); var annotation = oldAnnotation();
// This is not a new annotation.
annotation.id = 'annotation_id';
// This is not a highlight.
annotation.$highlight = undefined;
annotation.text = 'Annotation text';
annotation.tags = ['tag_1', 'tag_2'];
// The drafts service has some draft changes for this annotation. // The drafts service has some draft changes for this annotation.
fakeDrafts.get.returns('foo'); fakeDrafts.get.returns('foo');
...@@ -617,14 +639,7 @@ describe('annotation.js', function() { ...@@ -617,14 +639,7 @@ describe('annotation.js', function() {
}); });
it('does not edit new highlights on initialization', function() { it('does not edit new highlights on initialization', function() {
var annotation = defaultAnnotation(); var annotation = newHighlight();
// This is a new annotation.
annotation.id = undefined;
fakeDrafts.get.returns(null);
// This is a highlight.
annotation.$highlight = true;
annotation.text = '';
annotation.tags = [];
// We have to set annotation.$create() because it'll try to call it. // We have to set annotation.$create() because it'll try to call it.
annotation.$create = sandbox.stub().returns({ annotation.$create = sandbox.stub().returns({
then: function() {} then: function() {}
...@@ -636,18 +651,11 @@ describe('annotation.js', function() { ...@@ -636,18 +651,11 @@ describe('annotation.js', function() {
}); });
it('edits highlights with drafts on initialization', function() { it('edits highlights with drafts on initialization', function() {
var annotation = defaultAnnotation(); var annotation = oldHighlight();
// You can edit a highlight, enter some text or tags, and save it (the // You can edit a highlight, enter some text or tags, and save it (the
// highlight then becomes an annotation). You can also edit a highlight // highlight then becomes an annotation). You can also edit a highlight
// and then change focus to another group and back without saving the // and then change focus to another group and back without saving the
// highlight, in which case the highlight will have draft edits. // highlight, in which case the highlight will have draft edits.
//
// This is not a new highlight.
annotation.id = 'annotation_id';
annotation.$highlight = undefined;
// This is a highlight.
annotation.text = '';
annotation.tags = [];
// This highlight has draft edits. // This highlight has draft edits.
fakeDrafts.get.returns('foo'); fakeDrafts.get.returns('foo');
...@@ -679,10 +687,7 @@ describe('annotation.js', function() { ...@@ -679,10 +687,7 @@ describe('annotation.js', function() {
describe('AnnotationController.isHighlight()', function() { describe('AnnotationController.isHighlight()', function() {
it('returns true for new highlights', function() { it('returns true for new highlights', function() {
var annotation = defaultAnnotation(); var annotation = newHighlight();
// New highlights have no id and have $highlight: true.
annotation.id = undefined;
annotation.$highlight = true;
// We need to define $create because it'll try to call it. // We need to define $create because it'll try to call it.
annotation.$create = function() {return {then: function() {}};}; annotation.$create = function() {return {then: function() {}};};
...@@ -692,9 +697,7 @@ describe('annotation.js', function() { ...@@ -692,9 +697,7 @@ describe('annotation.js', function() {
}); });
it('returns false for new annotations', function() { it('returns false for new annotations', function() {
var annotation = defaultAnnotation(); var annotation = newAnnotation();
// New annotations have no id and no $highlight.
annotation.id = annotation.$highlight = undefined;
var vm = createDirective(annotation).controller; var vm = createDirective(annotation).controller;
...@@ -702,16 +705,7 @@ describe('annotation.js', function() { ...@@ -702,16 +705,7 @@ describe('annotation.js', function() {
}); });
it('returns false for page notes', function() { it('returns false for page notes', function() {
var annotation = defaultAnnotation(); var annotation = oldPageNote();
annotation.$highlight = undefined;
// Page notes have no targets.
annotation.target = [];
// This is not a reply.
annotation.references = [];
// The annotation has no text or tags. If it weren't a page note, it'd
// be a highlight.
annotation.text = '';
annotation.tags = [];
var vm = createDirective(annotation).controller; var vm = createDirective(annotation).controller;
...@@ -719,30 +713,15 @@ describe('annotation.js', function() { ...@@ -719,30 +713,15 @@ describe('annotation.js', function() {
}); });
it('returns false for replies', function() { it('returns false for replies', function() {
var annotation = defaultAnnotation(); var annotation = oldReply();
annotation.$highlight = undefined;
// This is not a page note.
annotation.target = ['foo'];
// Replies have references.
annotation.references = ['parent_annotation_id'];
// The annotation has no text or tags. If it weren't a reply, it'd
// be a highlight.
annotation.text = '';
annotation.tags = [];
var vm = createDirective(annotation).controller; var vm = createDirective(annotation).controller;
assert.isFalse(vm.isHighlight()); assert.isFalse(vm.isHighlight());
}); });
it('returns false for annotations with text', function() { it('returns false for annotations with text but no tags', function() {
var annotation = defaultAnnotation(); var annotation = oldAnnotation();
// Not a highlight, reply or page note.
annotation.$highlight = undefined;
annotation.target = ['foo'];
annotation.references = ['parent_annotation_id'];
// Has some text but no tags.
annotation.text = 'This is my annotation'; annotation.text = 'This is my annotation';
annotation.tags = []; annotation.tags = [];
...@@ -751,14 +730,8 @@ describe('annotation.js', function() { ...@@ -751,14 +730,8 @@ describe('annotation.js', function() {
assert.isFalse(vm.isHighlight()); assert.isFalse(vm.isHighlight());
}); });
it('returns false for annotations with tags', function() { it('returns false for annotations with tags but no text', function() {
var annotation = defaultAnnotation(); var annotation = oldAnnotation();
// Not a highlight, reply or page note.
annotation.$highlight = undefined;
annotation.target = ['foo'];
annotation.references = ['parent_annotation_id'];
// Has some tags but no text.
annotation.text = ''; annotation.text = '';
annotation.tags = ['foo']; annotation.tags = ['foo'];
...@@ -768,13 +741,7 @@ describe('annotation.js', function() { ...@@ -768,13 +741,7 @@ describe('annotation.js', function() {
}); });
it('returns true for annotations with no text or tags', function() { it('returns true for annotations with no text or tags', function() {
var annotation = defaultAnnotation(); var annotation = oldAnnotation();
// Not a new highlight, reply or page note.
annotation.$highlight = undefined;
annotation.target = ['foo'];
annotation.references = [];
// Has no tags or text, i.e. it's a highlight.
annotation.text = ''; annotation.text = '';
annotation.tags = []; annotation.tags = [];
......
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