Commit cbc3b3e6 authored by Sean Hammond's avatar Sean Hammond

Don't save null permissions/tags/text to drafts store

This is a translation of commit 363134d28cb7ff8d2313278e1b4b6c09b5bf9bed
from CoffeeScript to JavaScript. Original commit message from
363134d28cb7ff8d2313278e1b4b6c09b5bf9bed:

When saving a draft to the drafts store, don't save changes for fields
which aren't actually set. This prevents us from accidentally later
restoring `null` to one of these fields.

Fixes an issue where creating an annotation when logged-out would result
in a null permissions field.
parent 0170e330
...@@ -328,11 +328,17 @@ function AnnotationController( ...@@ -328,11 +328,17 @@ function AnnotationController(
// Drafts only preserve the text, tags and permissions of the annotation // Drafts only preserve the text, tags and permissions of the annotation
// (i.e. only the bits that the user can edit), changes to other // (i.e. only the bits that the user can edit), changes to other
// properties are not preserved. // properties are not preserved.
drafts.update(model, { var changes = {};
text: draft.text, if (draft.text) {
tags: draft.tags, changes.text = draft.text;
permissions: draft.permissions }
}); if (draft.tags) {
changes.tags = draft.tags;
}
if (draft.permissions) {
changes.permissions = draft.permissions;
}
drafts.update(model, changes);
}; };
/** /**
......
...@@ -725,13 +725,27 @@ describe('annotation', function() { ...@@ -725,13 +725,27 @@ describe('annotation', function() {
it('creates a draft when editing an annotation', function() { it('creates a draft when editing an annotation', function() {
createDirective(); createDirective();
controller.edit(); controller.edit();
assert.calledWith(fakeDrafts.update, annotation, { assert.calledWith(fakeDrafts.update, annotation);
text: annotation.text,
tags: annotation.tags,
permissions: annotation.permissions
});
}); });
it(
'creates a draft with only editable fields which are non-null',
function() {
// When a draft is saved, we shouldn't save any fields to the draft
// "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();
annotation.permissions = null;
annotation.text = 'Hello!';
annotation.tags = null;
controller.edit();
assert.calledWith(fakeDrafts.update, annotation, {text: 'Hello!'});
}
);
it('starts editing immediately if there is a draft', function() { it('starts editing immediately if there is a draft', function() {
fakeDrafts.get.returns({ fakeDrafts.get.returns({
tags: [ 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