describe('serializing a form', function() { describe('when no elements are found', function() { var View = Backbone.View.extend({ render: function() { this.$el.html('When I am alone I pretend to be a carrot'); } }); var view; beforeEach(function() { view = new View(); view.render(); }); it('should not throw an exception', function() { expect(function() { Backbone.Syphon.serialize(view); }).to.not.throw(Error); }); }); describe('when serializing a text input', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should return an object with a key from the text input name', function() { expect(this.result).to.have.have.ownProperty('foo'); }); it('should have the input\'s value', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when serializing a input with no name', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should not serialize the value to the target object', function() { expect(this.result).to.be.ok; }); }); describe('when serializing a textarea', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should have the textarea\'s value', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when serializing a select box', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should have the textarea\'s value', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when serializing a checkbox', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); }); describe('and the checkbox is checked', function() { beforeEach(function() { this.view = new this.View(); this.view.render(); this.view.$('#the-checkbox').prop('checked', true); this.result = Backbone.Syphon.serialize(this.view); }); it('should return an object with a value of true', function() { expect(this.result.chk).to.be.true; }); }); describe('and the checkbox is not checked', function() { beforeEach(function() { this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should return an object with a value of false', function() { expect(this.result.chk).to.be.false; }); }); describe('and the checkbox is indeterminate', function() { beforeEach(function() { this.view = new this.View(); this.view.render(); this.view.$('#the-checkbox').prop('indeterminate', true); this.result = Backbone.Syphon.serialize(this.view); }); it('should return an object with a value of null', function() { expect(this.result.chk).to.be.null; }); }); }); describe('when serializing a button', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should not have the button\'s value', function() { expect(this.result.hasOwnProperty('btn')).to.be.false; }); }); describe('when serializing an input with type of "submit"', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should not have the button\'s value', function() { expect(this.result.hasOwnProperty('btn')).to.be.false; }); }); describe('when serializing an input with type of "reset"', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should not have the button\'s value', function() { expect(this.result.hasOwnProperty('btn')).to.be.false; }); }); describe('when serializing a radio button group', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should only return the value of the selected radio button', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when serializing contenteditable elements', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '
bar
' + 'baz' + '
bar
' + 'baz' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('should return the value of a contenteditable "div" element', function() { expect(this.result.foo).to.equal('bar'); }); it('should return the value of a contenteditable "span" element', function() { expect(this.result.fu).to.equal('baz'); }); it('should ignore elements that are not "contenteditable"', function() { expect(this.result.trap).to.be.undefined; expect(this.result.trapAgain).to.be.undefined; }); }); describe('when the view is actually a form', function() { beforeEach(function() { this.View = Backbone.View.extend({ tagName: 'form', render: function() { this.$el.html( '' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('retrieves the inputs\' values', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when given a form element instead of a view', function() { beforeEach(function() { this.form = $( '
' + '' + '
' )[0]; this.result = Backbone.Syphon.serialize(this.form); }); it('retrieves the inputs\' values', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when given a form element with nested inputs', function() { beforeEach(function() { this.form = $( '
' + '
' + '' + '
' + '
' )[0]; this.result = Backbone.Syphon.serialize(this.form); }); it('retrieves the inputs\' values', function() { expect(this.result.foo).to.equal('bar'); }); }); describe('when given more than 1 form', function() { beforeEach(function() { this.View = Backbone.View.extend({ tagName: 'form', render: function() { this.$el.html( '
' + '' + '
' + '
' + '' + '
' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('retrieves all values', function() { expect(_.keys(this.result).length).to.equal(2); expect(this.result.foo).to.equal('bar'); expect(this.result.bar).to.equal('foo'); }); }); describe('when a form tag does not exist', function() { beforeEach(function() { this.View = Backbone.View.extend({ tagName: 'form', render: function() { this.$el.html( '' + '' ); } }); this.view = new this.View(); this.view.render(); this.result = Backbone.Syphon.serialize(this.view); }); it('retrieves all values', function() { expect(_.keys(this.result).length).to.equal(2); expect(this.result.foo).to.equal('bar'); expect(this.result.bar).to.equal('foo'); }); }); describe('when ignoring a field by selector', function() { beforeEach(function() { this.View = Backbone.View.extend({ render: function() { this.$el.html( '
' + '' + '' + '' + '' + '