Commit cbb1d56d authored by Sean Hammond's avatar Sean Hammond

Remove global variable from tests

Remove gloval `controller` variable from AnnotationController tests.
parent f7faa1c2
......@@ -289,7 +289,6 @@ describe('annotation.js', function() {
var $timeout;
var $window;
var annotation;
var controller;
var createDirective;
var fakeAnnotationMapper;
var fakeAnnotationUI;
......@@ -314,8 +313,9 @@ describe('annotation.js', function() {
$element = angular.element('<div annotation="annotation">');
$compile($element)($scope);
$scope.$digest();
controller = $element.controller('annotation');
var controller = $element.controller('annotation');
isolateScope = $element.isolateScope();
return controller;
};
before(function() {
......@@ -459,16 +459,19 @@ describe('annotation.js', function() {
describe('AnnotationController.editing()', function() {
it('returns true if action is "create"', function() {
var controller = createDirective();
controller.action = 'create';
assert(controller.editing());
});
it('returns true if action is "edit"', function() {
var controller = createDirective();
controller.action = 'edit';
assert(controller.editing());
});
it('returns false if action is "view"', function() {
var controller = createDirective();
controller.action = 'view';
assert(!controller.editing());
});
......@@ -509,7 +512,6 @@ describe('annotation.js', function() {
describe('#reply', function() {
beforeEach(function() {
createDirective();
annotation.permissions = {
read: ['acct:joe@localhost'],
update: ['acct:joe@localhost'],
......@@ -519,6 +521,7 @@ describe('annotation.js', function() {
});
it('creates a new reply with the proper uri and references', function() {
var controller = createDirective();
controller.reply();
var match = sinon.match({
references: [annotation.id],
......@@ -528,6 +531,7 @@ describe('annotation.js', function() {
});
it('makes the annotation shared if the parent is shared', function() {
var controller = createDirective();
var reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply);
fakePermissions.isShared.returns(true);
......@@ -538,6 +542,7 @@ describe('annotation.js', function() {
});
it('makes the annotation shared if the parent is shared', function() {
var controller = createDirective();
$scope.annotation.group = 'my group';
$scope.annotation.permissions = {
read: ['my group']
......@@ -559,6 +564,7 @@ describe('annotation.js', function() {
it(
'does not add the world readable principal if the parent is private',
function() {
var controller = createDirective();
var reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply);
fakePermissions.isShared.returns(false);
......@@ -570,6 +576,7 @@ describe('annotation.js', function() {
);
it('sets the reply\'s group to be the same as its parent\'s', function() {
var controller = createDirective();
$scope.annotation.group = 'my group';
var reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply);
......@@ -579,11 +586,8 @@ describe('annotation.js', function() {
});
describe('#setPrivacy', function() {
beforeEach(function() {
createDirective();
});
it('makes the annotation private when level is "private"', function() {
var controller = createDirective();
annotation.$update = sinon.stub().returns(Promise.resolve());
controller.edit();
controller.setPrivacy('private');
......@@ -597,6 +601,7 @@ describe('annotation.js', function() {
});
it('makes the annotation shared when level is "shared"', function() {
var controller = createDirective();
annotation.$update = sinon.stub().returns(Promise.resolve());
controller.edit();
controller.setPrivacy('shared');
......@@ -608,6 +613,7 @@ describe('annotation.js', function() {
});
it('saves the "shared" visibility level to localStorage', function() {
var controller = createDirective();
annotation.$update = sinon.stub().returns(Promise.resolve());
controller.edit();
controller.setPrivacy('shared');
......@@ -617,6 +623,7 @@ describe('annotation.js', function() {
});
it('saves the "private" visibility level to localStorage', function() {
var controller = createDirective();
annotation.$update = sinon.stub().returns(Promise.resolve());
controller.edit();
controller.setPrivacy('private');
......@@ -626,6 +633,7 @@ describe('annotation.js', function() {
});
it('doesn\'t save the visibility if the annotation is a reply', function() {
var controller = createDirective();
annotation.$update = sinon.stub().returns(Promise.resolve());
annotation.references = ['parent id'];
controller.edit();
......@@ -637,17 +645,15 @@ describe('annotation.js', function() {
});
describe('#hasContent', function() {
beforeEach(function() {
createDirective();
});
it('returns false if the annotation has no tags or text', function() {
var controller = createDirective();
controller.annotation.text = '';
controller.annotation.tags = [];
assert.ok(!controller.hasContent());
});
it('returns true if the annotation has tags or text', function() {
var controller = createDirective();
controller.annotation.text = 'bar';
assert.ok(controller.hasContent());
controller.annotation.text = '';
......@@ -661,16 +667,14 @@ describe('annotation.js', function() {
});
describe('#hasQuotes', function() {
beforeEach(function() {
createDirective();
});
it('returns false if the annotation has no quotes', function() {
var controller = createDirective();
controller.annotation.target = [{}];
assert.isFalse(controller.hasQuotes());
});
it('returns true if the annotation has quotes', function() {
var controller = createDirective();
controller.annotation.target = [
{
selector: [
......@@ -685,16 +689,13 @@ describe('annotation.js', function() {
});
describe('#render', function() {
beforeEach(function() {
createDirective();
sandbox.spy(controller, 'render');
});
afterEach(function() {
sandbox.restore();
});
it('is called exactly once on model changes', function() {
var controller = createDirective();
sandbox.spy(controller, 'render');
assert.notCalled(controller.render);
annotation['delete'] = true;
$scope.$digest();
......@@ -705,17 +706,20 @@ describe('annotation.js', function() {
});
it('provides a document title', function() {
var controller = createDirective();
controller.render();
assert.equal(controller.document.title, 'A special document');
});
it('uses the first title when there are more than one', function() {
var controller = createDirective();
annotation.document.title = ['first title', 'second title'];
controller.render();
assert.equal(controller.document.title, 'first title');
});
it('truncates long titles', function() {
var controller = createDirective();
annotation.document.title = 'A very very very long title that really\nshouldn\'t be found on a page on the internet.';
controller.render();
assert.equal(
......@@ -723,16 +727,19 @@ describe('annotation.js', function() {
});
it('provides a document uri', function() {
var controller = createDirective();
controller.render();
assert.equal(controller.document.uri, 'http://example.com');
});
it('provides an extracted domain from the uri', function() {
var controller = createDirective();
controller.render();
assert.equal(controller.document.domain, 'example.com');
});
it('uses the domain for the title if the title is not present', function() {
var controller = createDirective();
delete annotation.document.title;
controller.render();
assert.equal(controller.document.title, 'example.com');
......@@ -741,6 +748,7 @@ describe('annotation.js', function() {
it(
'still sets the uri correctly if the annotation has no document',
function() {
var controller = createDirective();
delete annotation.document;
controller.render();
assert(controller.document.uri === $scope.annotation.uri);
......@@ -750,6 +758,7 @@ describe('annotation.js', function() {
it(
'still sets the domain correctly if the annotation has no document',
function() {
var controller = createDirective();
delete annotation.document;
controller.render();
assert(controller.document.domain === 'example.com');
......@@ -759,6 +768,7 @@ describe('annotation.js', function() {
it(
'uses the domain for the title when the annotation has no document',
function() {
var controller = createDirective();
delete annotation.document;
controller.render();
assert(controller.document.title === 'example.com');
......@@ -779,19 +789,22 @@ describe('annotation.js', function() {
});
it('is not updated for unsaved annotations', function() {
annotation.updated = null;
var controller = createDirective();
// Unsaved annotations don't have an updated time yet so a timestamp
// string can't be computed for them.
annotation.updated = null;
$scope.$digest();
assert.equal(controller.timestamp, null);
});
it('is updated on first digest', function() {
var controller = createDirective();
$scope.$digest();
assert.equal(controller.timestamp, 'a while ago');
});
it('is updated after a timeout', function() {
var controller = createDirective();
fakeTime.nextFuzzyUpdate.returns(10);
fakeTime.toFuzzyString.returns('ages ago');
$scope.$digest();
......@@ -801,6 +814,7 @@ describe('annotation.js', function() {
});
it('is no longer updated after the scope is destroyed', function() {
var controller = createDirective();
$scope.$digest();
$scope.$destroy();
$timeout.flush();
......@@ -809,13 +823,9 @@ describe('annotation.js', function() {
});
describe('share', function() {
var dialog;
beforeEach(function() {
dialog = $element.find('.share-dialog-wrapper');
});
it('sets and unsets the open class on the share wrapper', function() {
var controller = createDirective();
var dialog = $element.find('.share-dialog-wrapper');
dialog.find('button').click();
isolateScope.$digest();
assert.ok(dialog.hasClass('open'));
......@@ -827,7 +837,6 @@ describe('annotation.js', function() {
describe('deleteAnnotation() method', function() {
beforeEach(function() {
createDirective();
fakeAnnotationMapper.deleteAnnotation = sandbox.stub();
fakeFlash.error = sandbox.stub();
});
......@@ -835,6 +844,7 @@ describe('annotation.js', function() {
it(
'calls annotationMapper.delete() if the delete is confirmed',
function(done) {
var controller = createDirective();
sandbox.stub($window, 'confirm').returns(true);
fakeAnnotationMapper.deleteAnnotation.returns($q.resolve());
controller['delete']().then(function() {
......@@ -848,6 +858,7 @@ describe('annotation.js', function() {
it(
'doesn\'t call annotationMapper.delete() if the delete is cancelled',
function() {
var controller = createDirective();
sandbox.stub($window, 'confirm').returns(false);
assert(fakeAnnotationMapper.deleteAnnotation.notCalled);
}
......@@ -856,6 +867,7 @@ describe('annotation.js', function() {
it(
'flashes a generic error if the server cannot be reached',
function(done) {
var controller = createDirective();
sandbox.stub($window, 'confirm').returns(true);
fakeAnnotationMapper.deleteAnnotation.returns($q.reject({
status: 0
......@@ -870,6 +882,7 @@ describe('annotation.js', function() {
);
it('flashes an error if the delete fails on the server', function(done) {
var controller = createDirective();
sandbox.stub($window, 'confirm').returns(true);
fakeAnnotationMapper.deleteAnnotation.returns($q.reject({
status: 500,
......@@ -885,6 +898,7 @@ describe('annotation.js', function() {
});
it('doesn\'t flash an error if the delete succeeds', function(done) {
var controller = createDirective();
sandbox.stub($window, 'confirm').returns(true);
fakeAnnotationMapper.deleteAnnotation.returns($q.resolve());
controller['delete']().then(function() {
......@@ -897,15 +911,20 @@ describe('annotation.js', function() {
describe('saving a new annotation', function() {
beforeEach(function() {
createDirective();
fakeFlash.error = sandbox.stub();
controller.action = 'create';
annotation.$create = sandbox.stub();
});
function controllerWithActionCreate() {
var controller = createDirective();
controller.action = 'create';
return controller;
}
it(
'emits annotationCreated when saving an annotation succeeds',
function(done) {
var controller = controllerWithActionCreate();
sandbox.spy($rootScope, '$emit');
annotation.$create.returns(Promise.resolve());
controller.save().then(function() {
......@@ -918,6 +937,7 @@ describe('annotation.js', function() {
it(
'flashes a generic error if the server can\'t be reached',
function(done) {
var controller = controllerWithActionCreate();
annotation.$create.returns(Promise.reject({
status: 0
}));
......@@ -932,6 +952,7 @@ describe('annotation.js', function() {
it(
'flashes an error if saving the annotation fails on the server',
function(done) {
var controller = controllerWithActionCreate();
annotation.$create.returns(Promise.reject({
status: 500,
statusText: 'Server Error',
......@@ -948,6 +969,7 @@ describe('annotation.js', function() {
it(
'doesn\'t flash an error when saving an annotation succeeds',
function() {
var controller = controllerWithActionCreate();
annotation.$create.returns(Promise.resolve());
controller.save();
assert(fakeFlash.error.notCalled);
......@@ -957,15 +979,20 @@ describe('annotation.js', function() {
describe('saving an edited an annotation', function() {
beforeEach(function() {
createDirective();
fakeFlash.error = sandbox.stub();
controller.action = 'edit';
annotation.$update = sandbox.stub();
});
function controllerWithActionEdit() {
var controller = createDirective();
controller.action = 'edit';
return controller;
}
it(
'flashes a generic error if the server cannot be reached',
function(done) {
var controller = controllerWithActionEdit();
annotation.$update.returns(Promise.reject({
status: 0
}));
......@@ -980,6 +1007,7 @@ describe('annotation.js', function() {
it(
'flashes an error if saving the annotation fails on the server',
function(done) {
var controller = controllerWithActionEdit();
annotation.$update.returns(Promise.reject({
status: 500,
statusText: 'Server Error',
......@@ -996,6 +1024,7 @@ describe('annotation.js', function() {
it(
'doesn\'t flash an error if saving the annotation succeeds',
function() {
var controller = controllerWithActionEdit();
annotation.$update.returns(Promise.resolve());
controller.save();
assert(fakeFlash.error.notCalled);
......@@ -1005,7 +1034,7 @@ describe('annotation.js', function() {
describe('drafts', function() {
it('creates a draft when editing an annotation', function() {
createDirective();
var controller = createDirective();
controller.edit();
assert.calledWith(fakeDrafts.update, annotation);
});
......@@ -1017,7 +1046,7 @@ describe('annotation.js', function() {
// "changes" object that aren't actually set on the annotation. In this
// case, both permissions and tags are null so shouldn't be saved in
// the draft.
createDirective();
var controller = createDirective();
annotation.permissions = null;
annotation.text = 'Hello!';
annotation.tags = null;
......@@ -1037,7 +1066,7 @@ describe('annotation.js', function() {
],
text: 'unsaved-text'
});
createDirective();
var controller = createDirective();
assert.isTrue(controller.editing());
});
......@@ -1046,7 +1075,7 @@ describe('annotation.js', function() {
tags: ['unsaved-tag'],
text: 'unsaved-text'
});
createDirective();
var controller = createDirective();
assert.deepEqual(controller.annotation.tags, [
{
text: 'unsaved-tag'
......@@ -1056,7 +1085,7 @@ describe('annotation.js', function() {
});
it('removes the draft when changes are discarded', function() {
createDirective();
var controller = createDirective();
controller.edit();
controller.revert();
assert.calledWith(fakeDrafts.remove, annotation);
......@@ -1064,7 +1093,7 @@ describe('annotation.js', function() {
it('removes the draft when changes are saved', function() {
annotation.$update = sandbox.stub().returns(Promise.resolve());
createDirective();
var controller = createDirective();
controller.edit();
controller.save();
......@@ -1080,7 +1109,7 @@ describe('annotation.js', function() {
describe('when the focused group changes', function() {
it('updates the current draft', function() {
createDirective();
var controller = createDirective();
controller.edit();
controller.annotation.text = 'unsaved-text';
controller.annotation.tags = [];
......@@ -1098,7 +1127,7 @@ describe('annotation.js', function() {
});
it('should not create a new draft', function() {
createDirective();
var controller = createDirective();
controller.edit();
fakeDrafts.update = sinon.stub();
fakeDrafts.get = sinon.stub().returns(null);
......
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