Commit 4facd262 by cahrens

Jasmine test for metadata model.

parent 4d7c7f86
describe "CMS.Models.Metadata", -> describe "CMS.Models.Metadata", ->
it "has no url", -> it "knows when the value has not been modified", ->
expect(new CMS.Models.Metadata().url).toEqual("/save_item") model = new CMS.Models.Metadata(
\ No newline at end of file {'value': 'original', 'explicitly_set': false})
expect(model.isModified()).toBeFalsy()
model = new CMS.Models.Metadata(
{'value': 'original', 'explicitly_set': true})
model.setValue('original')
expect(model.isModified()).toBeFalsy()
it "knows when the value has been modified", ->
model = new CMS.Models.Metadata(
{'value': 'original', 'explicitly_set': false})
model.setValue('original')
expect(model.isModified()).toBeTruthy()
model = new CMS.Models.Metadata(
{'value': 'original', 'explicitly_set': true})
model.setValue('modified')
expect(model.isModified()).toBeTruthy()
it "tracks when values have been explicitly set", ->
model = new CMS.Models.Metadata(
{'value': 'original', 'explicitly_set': false})
expect(model.isExplicitlySet()).toBeFalsy()
model.setValue('original')
expect(model.isExplicitlySet()).toBeTruthy()
it "has both 'display value' and a 'value' methods", ->
model = new CMS.Models.Metadata(
{'value': 'default', 'explicitly_set': false})
expect(model.getValue()).toBeNull
expect(model.getDisplayValue()).toBe('default')
model.setValue('modified')
expect(model.getValue()).toBe('modified')
expect(model.getDisplayValue()).toBe('modified')
it "has a clear method for reverting to the default", ->
model = new CMS.Models.Metadata(
{'value': 'original', 'default_value' : 'default', 'explicitly_set': true})
model.clear()
expect(model.getValue()).toBeNull
expect(model.getDisplayValue()).toBe('default')
expect(model.isExplicitlySet()).toBeFalsy()
it "has a getter for field name", ->
model = new CMS.Models.Metadata({'field_name': 'foo'})
expect(model.getFieldName()).toBe('foo')
it "has a getter for options", ->
model = new CMS.Models.Metadata({'options': ['foo', 'bar']})
expect(model.getOptions()).toEqual(['foo', 'bar'])
it "has a getter for type", ->
model = new CMS.Models.Metadata({'type': 'Integer'})
expect(model.getType()).toBe(CMS.Models.Metadata.INTEGER_TYPE)
...@@ -73,13 +73,3 @@ describe "CMS.Views.ModuleEdit", -> ...@@ -73,13 +73,3 @@ describe "CMS.Views.ModuleEdit", ->
expect(XModule.loadModule).toHaveBeenCalled() expect(XModule.loadModule).toHaveBeenCalled()
expect(XModule.loadModule.mostRecentCall.args[0]).toBe($('.xmodule_display')) expect(XModule.loadModule.mostRecentCall.args[0]).toBe($('.xmodule_display'))
describe "changedMetadata", ->
it "returns empty if no metadata loaded", ->
expect(@moduleEdit.changedMetadata()).toEqual({})
it "returns only changed values", ->
@moduleEdit.originalMetadata = {'foo', 'bar'}
spyOn(@moduleEdit, 'metadata').andReturn({'a': '', 'b': 'before', 'c': ''})
@moduleEdit.loadEdit()
@moduleEdit.metadata.andReturn({'a': '', 'b': 'after', 'd': 'only_after'})
expect(@moduleEdit.changedMetadata()).toEqual({'b' : 'after', 'd' : 'only_after'})
...@@ -19,10 +19,10 @@ CMS.Models.Metadata = Backbone.Model.extend({ ...@@ -19,10 +19,10 @@ CMS.Models.Metadata = Backbone.Model.extend({
this.original_explicitly_set = this.get('explicitly_set'); this.original_explicitly_set = this.get('explicitly_set');
}, },
getOriginalValue: function() { /**
return this.originalValue; * Returns true if the stored value is different, or if the "explicitly_set"
}, * property has changed.
*/
isModified : function() { isModified : function() {
if (!this.get('explicitly_set') && !this.original_explicitly_set) { if (!this.get('explicitly_set') && !this.original_explicitly_set) {
return false; return false;
...@@ -33,37 +33,72 @@ CMS.Models.Metadata = Backbone.Model.extend({ ...@@ -33,37 +33,72 @@ CMS.Models.Metadata = Backbone.Model.extend({
return true; return true;
}, },
/**
* Returns true if a non-default/non-inherited value has been set.
*/
isExplicitlySet: function() { isExplicitlySet: function() {
return this.get('explicitly_set'); return this.get('explicitly_set');
}, },
/**
* The value, as shown in the UI. This may be an inherited or default value.
*/
getDisplayValue : function () { getDisplayValue : function () {
return this.get('value'); return this.get('value');
}, },
/**
* The value, as should be returned to the server. if 'isExplicitlySet'
* returns false, this method returns null to indicate that the value
* is not set at this level.
*/
getValue: function() { getValue: function() {
return this.get('explicitly_set') ? this.get('value') : null; return this.get('explicitly_set') ? this.get('value') : null;
}, },
/**
* Sets the displayed value.
*/
setValue: function (value) { setValue: function (value) {
this.set('explicitly_set', true); this.set('explicitly_set', true);
this.set('value', value); this.set('value', value);
}, },
/**
* Returns the field name, which should be used for persisting the metadata
* field to the server.
*/
getFieldName: function () { getFieldName: function () {
return this.get('field_name'); return this.get('field_name');
}, },
/**
* Returns the options. This may be a array of possible values, or an object
* with properties like "max", "min" and "step".
*/
getOptions: function () { getOptions: function () {
return this.get('options'); return this.get('options');
}, },
/**
* Returns the type of this metadata field. Possible values are SELECT_TYPE,
* INTEGER_TYPE, and FLOAT_TYPE, GENERIC_TYPE.
*/
getType: function() { getType: function() {
return this.get('type'); return this.get('type');
}, },
/**
* Reverts the value to the default_value specified at construction, and updates the
* explicitly_set property.
*/
clear: function() { clear: function() {
this.set('explicitly_set', false); this.set('explicitly_set', false);
this.set('value', this.get('default_value')); this.set('value', this.get('default_value'));
} }
}); });
CMS.Models.Metadata.SELECT_TYPE = "Select";
CMS.Models.Metadata.INTEGER_TYPE = "Integer";
CMS.Models.Metadata.FLOAT_TYPE = "Float";
CMS.Models.Metadata.GENERIC_TYPE = "Generic";
...@@ -2,9 +2,7 @@ if (!CMS.Views['Metadata']) CMS.Views.Metadata = {}; ...@@ -2,9 +2,7 @@ if (!CMS.Views['Metadata']) CMS.Views.Metadata = {};
CMS.Views.Metadata.Editor = Backbone.View.extend({ CMS.Views.Metadata.Editor = Backbone.View.extend({
// Model class is ... // Model is simply a Backbone.Model instance.
events : {
},
initialize : function() { initialize : function() {
var self = this; var self = this;
...@@ -32,13 +30,15 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({ ...@@ -32,13 +30,15 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
el: self.$el.find('.metadata_entry')[counter++], el: self.$el.find('.metadata_entry')[counter++],
model: model model: model
}; };
if (item.type === 'Select') { if (item.type === CMS.Models.Metadata.SELECT_TYPE) {
new CMS.Views.Metadata.Option(data); new CMS.Views.Metadata.Option(data);
} }
else if (item.type === 'Integer' || item.type === 'Float') { else if (item.type === CMS.Models.Metadata.INTEGER_TYPE ||
item.type === CMS.Models.Metadata.FLOAT_TYPE) {
new CMS.Views.Metadata.Number(data); new CMS.Views.Metadata.Number(data);
} }
else { else {
// Everything else is treated as GENERIC_TYPE, which uses String editor.
new CMS.Views.Metadata.String(data); new CMS.Views.Metadata.String(data);
} }
}); });
...@@ -67,6 +67,8 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({ ...@@ -67,6 +67,8 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({
CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({
// Model is CMS.Models.Metadata.
initialize : function() { initialize : function() {
var self = this; var self = this;
var templateName = this.getTemplateName(); var templateName = this.getTemplateName();
......
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