jam-cloud/spec/javascripts/formToObject.spec.js

158 lines
6.0 KiB
JavaScript

(function(global) {
describe("jquery.formToObject tests", function() {
beforeEach(function() {
jasmine.getFixtures().fixturesPath = '/spec/javascripts/fixtures';
});
describe("Top level", function() {
describe("text input named foo, val bar", function() {
it("should have variable named foo, val bar", function() {
loadFixtures('formToObject.htm');
var o = $('#test1').formToObject();
expect(o.foo).toEqual("bar");
});
});
describe("text area named foo, contents bar", function() {
it("should have variable named foo, val bar", function() {
loadFixtures('formToObject.htm');
var o = $('#test1a').formToObject();
expect(o.foo).toEqual("bar");
});
});
describe("single checkbox named foo, val bar", function() {
it("should have variable named foo, val bar", function() {
loadFixtures('formToObject.htm');
var o = $('#test2').formToObject();
expect(o.foo).toEqual("bar");
});
});
describe("multi checkboxes named foo, vals 'a' and 'b'", function() {
it("should have variable named foo, val ['a','b']", function() {
loadFixtures('formToObject.htm');
var o = $('#test3').formToObject();
expect(o.foo).toEqual(['a','b']);
});
});
describe("select named foo, selected op val 'a'", function() {
it("should have variable named foo, val 'a'", function() {
loadFixtures('formToObject.htm');
var o = $('#test4').formToObject();
expect(o.foo).toEqual('a');
});
});
describe("multiselect named foo, selected opts vals 'a', 'c'", function() {
it("should have variable named foo, val ['a','c']", function() {
loadFixtures('formToObject.htm');
var o = $('#test5').formToObject();
expect(o.foo).toEqual(['a','c']);
});
});
describe("single radio named foo, val bar", function() {
it("should have variable named foo, val bar", function() {
loadFixtures('formToObject.htm');
var o = $('#test6').formToObject();
expect(o.foo).toEqual("bar");
});
});
});
describe("Second level", function() {
describe("text input named foo.bar, val 'x'", function() {
it("should have object foo with prop bar, value 'x'", function() {
loadFixtures('formToObject.htm');
var o = $('#test7').formToObject();
expect(o.foo.bar).toEqual("x");
});
});
describe("multi checkboxes named foo.bar, vals 'a' and 'b'", function() {
it("should have object named foo, prop bar, val ['a','b']", function() {
loadFixtures('formToObject.htm');
var o = $('#test9').formToObject();
expect(o.foo.bar).toEqual(['a','b']);
});
});
});
describe("Third level", function() {
describe("text input named foo.bar.spam, val 'x'", function() {
it("should have object foo with obj bar, with prop spam, value 'x'", function() {
loadFixtures('formToObject.htm');
var o = $('#test8').formToObject();
expect(o.foo.bar.spam).toEqual("x");
});
});
});
describe("Mixed cases", function() {
describe("flipping in and out of hierarchy", function() {
it("should properly capture all properties", function() {
loadFixtures('formToObject.htm');
var o = $('#test10').formToObject();
expect(o.foo.sponge).toEqual("99");
expect(o.foo.bar.potato).toEqual("mashed");
expect(o.apple).toEqual("sweet");
expect(o.foo.bar.strawberry).toEqual("ripe");
expect(o.foo.bar.kentucky.bluegrass.yards).toEqual("north");
});
});
});
describe("Bad cases", function() {
describe("Overriding object with property", function() {
it("should raise an error", function() {
loadFixtures('formToObject.htm');
expect( function() {
$('#error1').formToObject();
}).toThrow("Can't overwrite named structure");
});
});
});
describe("__OMIT__ special value", function() {
describe("When value for key is __OMIT__", function() {
it("should remove key from object", function() {
loadFixtures('formToObject.htm');
var o = $('#OMIT').formToObject();
expect(o.foo.sponge).toEqual("99");
expect(o.foo.bar.potato).toEqual("mashed");
expect("apple" in o).toBeFalsy('apple not in o');
// would be foo.bar.kentucky.bluegrass.yard - all empty should be gone:
expect("kentucky" in o.foo.bar).toBeFalsy('kentucky not in bar');
});
});
describe("When resulting formToObject is {}", function() {
it("should return null instead of {}", function() {
loadFixtures('formToObject.htm');
var o = $('#OMIT_ALL').formToObject();
expect(o).toBeNull();
});
});
});
});
})(window);