Skip to content

Commit

Permalink
Organizing BaseModel specs according to module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gburghardt committed Jul 9, 2012
1 parent 2023b7e commit 88acc23
Show file tree
Hide file tree
Showing 9 changed files with 910 additions and 854 deletions.
57 changes: 57 additions & 0 deletions test/framework/models/BaseModel.basicValidation.spec.js
@@ -0,0 +1,57 @@
describe("BaseModel", function() {

describe("modules", function() {

describe("basicValidation", function() {

describe("validateRequiredAttributes", function() {
it("marks fields with 'empty'-like values as required", function() {
var o = new TestValidation({price: null, description: "", notes: " "});
o.errors = {};
o.validateRequiredAttributes();

expect(o.hasErrors()).toBeTrue();
expect(o.errors.price).toBeArray();
expect(o.errors.price[0]).toEqual("is required");

expect(o.errors.name).toBeArray();
expect(o.errors.name[0]).toEqual("is required");

expect(o.errors.notes).toBeArray();
expect(o.errors.notes[0]).toEqual("is required");

expect(o.errors.description).toBeArray();
expect(o.errors.description[0]).toEqual("is required");

expect(o.errors.phone).toBeArray();
expect(o.errors.phone[0]).toEqual("is required");
});

it("validates when no attributes are passed in the constructor", function() {
var o = new TestValidation();
o.errors = {};
o.validateRequiredAttributes();

expect(o.hasErrors()).toBeTrue();
expect(o.errors.price).toBeArray();
expect(o.errors.price[0]).toEqual("is required");

expect(o.errors.name).toBeArray();
expect(o.errors.name[0]).toEqual("is required");

expect(o.errors.notes).toBeArray();
expect(o.errors.notes[0]).toEqual("is required");

expect(o.errors.description).toBeArray();
expect(o.errors.description[0]).toEqual("is required");

expect(o.errors.phone).toBeArray();
expect(o.errors.phone[0]).toEqual("is required");
});
});

});

});

});
151 changes: 151 additions & 0 deletions test/framework/models/BaseModel.extendedValidation.spec.js
@@ -0,0 +1,151 @@
describe("BaseModel", function() {

describe("modules", function() {

describe("extendedValidation", function() {

describe("valueIsNumeric", function() {
beforeEach(function() {
this.model = new TestNumericValidation();
});

it("returns true for numbers", function() {
expect(this.model.valueIsNumeric(100)).toBeTrue();
expect(this.model.valueIsNumeric(-100)).toBeTrue();
expect(this.model.valueIsNumeric(0.3)).toBeTrue();
});

it("returns true for numeric strings", function() {
expect(this.model.valueIsNumeric("100")).toBeTrue();
expect(this.model.valueIsNumeric("-100")).toBeTrue();
expect(this.model.valueIsNumeric("0.3")).toBeTrue();
});

it("returns false for empty strings", function() {
expect(this.model.valueIsNumeric("")).toBeFalse();
});

it("returns false for null values", function() {
expect(this.model.valueIsNumeric(null)).toBeFalse();
});

it("returns false for NaN values", function() {
expect(this.model.valueIsNumeric(NaN)).toBeFalse();
});

it ("returns false for strings without numbers", function() {
expect(this.model.valueIsNumeric("abc")).toBeFalse();
});

it ("returns false for strings with numbers and non numeric characters", function() {
expect(this.model.valueIsNumeric("$10.05")).toBeFalse();
});
});

describe("numeric", function() {
beforeEach(function() {
this.model = new TestNumericValidation();
this.model.errors = {};
this.model.valid = true;
});

it("returns true for empty strings", function() {
this.model.price = "";
this.model.validateAttributeDataTypes();
expect(this.model.valid).toBeTrue();
});

it("returns true for null values", function() {
this.model.price = null;
this.model.validateAttributeDataTypes();
expect(this.model.valid).toBeTrue();
});

it("returns false for NaN values", function() {
this.model.price = NaN;
this.model.validateAttributeDataTypes();
expect(this.model.valid).toBeFalse();
});

it("returns false for non numeric strings", function() {
this.model.price = "acb";
this.model.validateAttributeDataTypes();
expect(this.model.valid).toBeFalse();
});

it("returns false for strings with numbers, but other non number characters", function() {
this.model.price = "$10.99";
this.model.validateAttributeDataTypes();
expect(this.model.valid).toBeFalse();
});
});

describe("max length", function() {
beforeEach(function() {
this.model = new TestMaxLengthValidation();
});

it("returns true for string lengths equal to or lesser than the max length", function() {
this.model.name = "123456789";
this.model.description = "12345678";
this.model.notes = "1234";
expect(this.model.validate()).toBeTrue();
});

it("returns false for values greater than the max length", function() {
this.model.name = "1234567890abc";
this.model.description = "123456789";
this.model.notes = "12345";
expect(this.model.validate()).toBeFalse();
expect(this.model.errors.name).toBeArray();
expect(this.model.errors.description).toBeArray();
expect(this.model.errors.notes).toBeArray();
});

it("returns true for empty values", function() {
this.model.name = undefined;
this.model.descrition = null;
this.model.notes = " ";
expect(this.model.validate()).toBeTrue();
});
});

describe("validateAttributeFormats", function() {
beforeEach(function() {
this.model = new TestFormatValidation();
this.model.valid = true;
this.model.errors = {};
});

it("is valid if one of many regular expressions are valid", function() {
this.model.phone = "123-555-1234";
this.model.validateAttributeFormats();
expect(this.model.valid).toBeTrue();
});

it("is valid only if a single regular expression is valid", function() {
this.model.address = "123 James St, Chicago, IL 12345";
this.model.validateAttributeFormats();
expect(this.model.valid).toBeTrue();
});

it("is invalid if all regular expressions fail", function() {
this.model.phone = "5555-5-5555";
this.model.validateAttributeFormats();
expect(this.model.valid).toBeFalse();
expect(this.model.errors.phone).toBeArray();
});

it("is invalid if the regular expression fails", function() {
this.model.address = "123 James St, Chicago, IL";
this.model.validateAttributeFormats();
expect(this.model.valid).toBeFalse();
expect(this.model.errors.address).toBeArray();
});
});

});

});

});

0 comments on commit 88acc23

Please sign in to comment.