Commit 0170e330 authored by Sean Hammond's avatar Sean Hammond

Put comments back into annotation-test.js

These were lost when translating annotation-test.coffee ->
annotation-test.js using the `coffee` command.
parent 509706da
...@@ -305,6 +305,8 @@ describe('annotation', function() { ...@@ -305,6 +305,8 @@ describe('annotation', function() {
controller.edit(); controller.edit();
controller.setPrivacy('private'); controller.setPrivacy('private');
return controller.save().then(function() { return controller.save().then(function() {
// Verify that the permissions are updated once the annotation
// is saved.
assert.deepEqual(annotation.permissions, { assert.deepEqual(annotation.permissions, {
read: ['justme'] read: ['justme']
}); });
...@@ -494,6 +496,8 @@ describe('annotation', function() { ...@@ -494,6 +496,8 @@ describe('annotation', function() {
}); });
it('is not updated for unsaved annotations', function() { it('is not updated for unsaved annotations', function() {
// Unsaved annotations don't have an updated time yet so a timestamp
// string can't be computed for them.
annotation.updated = null; annotation.updated = null;
$scope.$digest(); $scope.$digest();
assert.equal(controller.timestamp, null); assert.equal(controller.timestamp, null);
...@@ -767,6 +771,11 @@ describe('annotation', function() { ...@@ -767,6 +771,11 @@ describe('annotation', function() {
createDirective(); createDirective();
controller.edit(); controller.edit();
controller.save(); controller.save();
// The controller currently removes the draft whenever an annotation
// update is committed on the server. This can happen either when saving
// locally or when an update is committed in another instance of H
// which is then pushed to the current instance.
annotation.updated = (new Date()).toISOString(); annotation.updated = (new Date()).toISOString();
$scope.$digest(); $scope.$digest();
assert.calledWith(fakeDrafts.remove, annotation); assert.calledWith(fakeDrafts.remove, annotation);
...@@ -815,13 +824,17 @@ describe('annotation', function() { ...@@ -815,13 +824,17 @@ describe('annotation', function() {
it( it(
'updates perms when moving new annotations to the focused group', 'updates perms when moving new annotations to the focused group',
function() { function() {
// id must be null so that AnnotationController considers this a new
// annotation.
annotation.id = null; annotation.id = null;
annotation.group = 'old-group'; annotation.group = 'old-group';
annotation.permissions = { annotation.permissions = {
read: [annotation.group] read: [annotation.group]
}; };
// This is a shared annotation.
fakePermissions.isShared.returns(true); fakePermissions.isShared.returns(true);
createDirective(); createDirective();
// Make permissions.shared() behave like we expect it to.
fakePermissions.shared = function(groupId) { fakePermissions.shared = function(groupId) {
return { return {
read: [groupId] read: [groupId]
...@@ -836,19 +849,26 @@ describe('annotation', function() { ...@@ -836,19 +849,26 @@ describe('annotation', function() {
); );
it('saves shared permissions for the new group to drafts', function() { it('saves shared permissions for the new group to drafts', function() {
// id must be null so that AnnotationController considers this a new
// annotation.
annotation.id = null; annotation.id = null;
annotation.group = 'old-group'; annotation.group = 'old-group';
annotation.permissions = { annotation.permissions = {
read: [annotation.group] read: [annotation.group]
}; };
// This is a shared annotation.
fakePermissions.isShared.returns(true); fakePermissions.isShared.returns(true);
createDirective(); createDirective();
// drafts.get() needs to return something truthy, otherwise
// AnnotationController won't try to update the draft for the annotation.
fakeDrafts.get.returns(true); fakeDrafts.get.returns(true);
// Make permissions.shared() behave like we expect it to.
fakePermissions.shared = function(groupId) { fakePermissions.shared = function(groupId) {
return { return {
read: [groupId] read: [groupId]
}; };
}; };
// Change the focused group.
fakeGroups.focused = sinon.stub().returns({ fakeGroups.focused = sinon.stub().returns({
id: 'new-group' id: 'new-group'
}); });
...@@ -860,12 +880,15 @@ describe('annotation', function() { ...@@ -860,12 +880,15 @@ describe('annotation', function() {
}); });
it('does not change perms when moving new private annotations', function() { it('does not change perms when moving new private annotations', function() {
// id must be null so that AnnotationController considers this a new
// annotation.
annotation.id = null; annotation.id = null;
annotation.group = 'old-group'; annotation.group = 'old-group';
annotation.permissions = { annotation.permissions = {
read: ['acct:bill@localhost'] read: ['acct:bill@localhost']
}; };
createDirective(); createDirective();
// This is a private annotation.
fakePermissions.isShared.returns(false); fakePermissions.isShared.returns(false);
fakeGroups.focused = sinon.stub().returns({ fakeGroups.focused = sinon.stub().returns({
id: 'new-group' id: 'new-group'
...@@ -891,6 +914,7 @@ describe('AnnotationController', function() { ...@@ -891,6 +914,7 @@ describe('AnnotationController', function() {
beforeEach(module('h.templates')); beforeEach(module('h.templates'));
/** Return Angular's $compile service. */
getCompileService = function() { getCompileService = function() {
var $compile; var $compile;
$compile = null; $compile = null;
...@@ -900,6 +924,7 @@ describe('AnnotationController', function() { ...@@ -900,6 +924,7 @@ describe('AnnotationController', function() {
return $compile; return $compile;
}; };
/** Return Angular's $rootScope. */
getRootScope = function() { getRootScope = function() {
var $rootScope; var $rootScope;
$rootScope = null; $rootScope = null;
...@@ -1038,6 +1063,7 @@ describe('AnnotationController', function() { ...@@ -1038,6 +1063,7 @@ describe('AnnotationController', function() {
}); });
it('sets the permissions of new annotations', function() { it('sets the permissions of new annotations', function() {
// This is a new annotation, doesn't have any permissions yet.
var annotation = { var annotation = {
group: 'test-group' group: 'test-group'
}; };
...@@ -1114,32 +1140,43 @@ describe('AnnotationController', function() { ...@@ -1114,32 +1140,43 @@ describe('AnnotationController', function() {
describe('when the user signs in', function() { describe('when the user signs in', function() {
it('sets the user of unsaved annotations', function() { it('sets the user of unsaved annotations', function() {
// This annotation has no user yet, because that's what happens
// when you create a new annotation while not signed in.
var annotation = {}; var annotation = {};
var session = { var session = {
state: { state: {
userid: null userid: null // Not signed in.
} }
}; };
var $rootScope = createAnnotationDirective({ var $rootScope = createAnnotationDirective({
annotation: annotation, annotation: annotation,
session: session session: session
}).$rootScope; }).$rootScope;
// At this point we would not expect the user to have been set,
// even though the annotation has been created, because the user isn't
// signed in.
assert(!annotation.user); assert(!annotation.user);
// Sign the user in.
session.state.userid = 'acct:fred@hypothes.is'; session.state.userid = 'acct:fred@hypothes.is';
// The session service would broadcast USER_CHANGED after sign in.
$rootScope.$broadcast(events.USER_CHANGED, {}); $rootScope.$broadcast(events.USER_CHANGED, {});
assert.equal(annotation.user, session.state.userid); assert.equal(annotation.user, session.state.userid);
}); });
it('sets the permissions of unsaved annotations', function() { it('sets the permissions of unsaved annotations', function() {
// This annotation has no permissions yet, because that's what happens
// when you create a new annotation while not signed in.
var annotation = { var annotation = {
group: '__world__' group: '__world__'
}; };
var session = { var session = {
state: { state: {
userid: null userid: null // Not signed in.
} }
}; };
var permissions = { var permissions = {
// permissions.default() would return null, because the user isn't
// signed in.
'default': function() { 'default': function() {
return null; return null;
}, },
...@@ -1151,11 +1188,18 @@ describe('AnnotationController', function() { ...@@ -1151,11 +1188,18 @@ describe('AnnotationController', function() {
session: session, session: session,
permissions: permissions permissions: permissions
}).$rootScope; }).$rootScope;
// At this point we would not expect the permissions to have been set,
// even though the annotation has been created, because the user isn't
// signed in.
assert(!annotation.permissions); assert(!annotation.permissions);
// Sign the user in.
session.state.userid = 'acct:fred@hypothes.is'; session.state.userid = 'acct:fred@hypothes.is';
// permissions.default() would now return permissions, because the user
// is signed in.
permissions['default'] = function() { permissions['default'] = function() {
return '__default_permissions__'; return '__default_permissions__';
}; };
// The session service would broadcast USER_CHANGED after sign in.
$rootScope.$broadcast(events.USER_CHANGED, {}); $rootScope.$broadcast(events.USER_CHANGED, {});
assert.equal(annotation.permissions, '__default_permissions__'); assert.equal(annotation.permissions, '__default_permissions__');
}); });
...@@ -1173,9 +1217,11 @@ describe('AnnotationController', function() { ...@@ -1173,9 +1217,11 @@ describe('AnnotationController', function() {
id: 'test-annotation-id', id: 'test-annotation-id',
user: 'acct:bill@localhost', user: 'acct:bill@localhost',
text: 'Initial annotation body text', text: 'Initial annotation body text',
// Allow the initial save of the annotation to succeed.
$create: function() { $create: function() {
return Promise.resolve(); return Promise.resolve();
}, },
// Simulate saving the edit of the annotation to the server failing.
$update: function() { $update: function() {
return Promise.reject({ return Promise.reject({
status: 500, status: 500,
...@@ -1186,14 +1232,23 @@ describe('AnnotationController', function() { ...@@ -1186,14 +1232,23 @@ describe('AnnotationController', function() {
} }
}).controller; }).controller;
var originalText = controller.annotation.text; var originalText = controller.annotation.text;
// Simulate the user clicking the Edit button on the annotation.
controller.edit(); controller.edit();
// Simulate the user typing some text into the annotation editor textarea.
controller.annotation.text = 'changed by test code'; controller.annotation.text = 'changed by test code';
// Simulate the user hitting the Save button and wait for the
// (unsuccessful) response from the server.
controller.save(); controller.save();
// At this point the annotation editor controls are still open, and the
// annotation's text is still the modified (unsaved) text.
assert(controller.annotation.text === 'changed by test code'); assert(controller.annotation.text === 'changed by test code');
// Simulate the user clicking the Cancel button.
controller.revert(); controller.revert();
assert(controller.annotation.text === originalText); assert(controller.annotation.text === originalText);
}); });
// test that editing reverting changes to an annotation with
// no text resets the text to be empty.
it('clears the text when reverting changes to a highlight', function() { it('clears the text when reverting changes to a highlight', function() {
var controller = createAnnotationDirective({ var controller = createAnnotationDirective({
annotation: { annotation: {
......
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