spec/tests/validator/form_validator_spec.coffee in hoarder-js-0.0.1 vs spec/tests/validator/form_validator_spec.coffee in hoarder-js-0.0.2
- old
+ new
@@ -1,29 +1,45 @@
-FormElement = require "hoarder/form/form_element"
+Form = require 'hoarder/form/form'
FormValidator = require "hoarder/validator/form_validator"
describe "FormValidator", ->
validator = null
- element = null
+ form = cityElement = null
beforeEach ->
- validator = FormValidator.default()
- element = new FormElement("element", "$", "test-element", [ "alphanumeric", "creditCard", "email", "maxLength=5", "minLength=5", "numeric", "phone", "required" ])
+ createTestFormFixture()
- it "can validate an element", ->
- errors = validator.validateElement(element)
- jasmine.log errors
- expect(errors[0].message).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
- element.value = "4111111111111111"
- errors = validator.validateElement(element)
- jasmine.log errors
- expect(errors[0].message).toEqual "Please enter a valid email address."
- element.value = "6854291"
- errors = validator.validateElement(element)
- jasmine.log errors
- expect(errors[0].message).toEqual "Please enter a valid credit card number."
+ formElement = document.getElementById 'test-form'
+ cityElement = formElement['city']
- it "can validate a form", ->
- errors = validator.validateForm(mocks.simpleForm)
- expect(errors.length).toEqual 0
- errors = validator.validateForm(mocks.invalidForm)
- expect(errors.length).toEqual 4
+ form = new Form(formElement)
+ validator = FormValidator.create()
+
+ describe '#validateForm', ->
+
+ it "will return true if the form is valid", ->
+ expect(validator.validateForm form).toBeTruthy()
+
+ it "will return false if any part of the form is invalid", ->
+ cityElement.value = 5
+ expect(validator.validateForm form).toBeFalsy()
+
+ it "will mark the invalid elements with a validity message", ->
+ cityElement.value = 5
+ validator.validateForm form
+ expect(cityElement.validationMessage).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
+
+ describe '#validateElement', ->
+
+ it "will return true if the element is valid", ->
+ expect(validator.validateElement cityElement).toBeTruthy()
+
+ describe "when the element is invalid", ->
+
+ it "will return false", ->
+ cityElement.value = 5
+ expect(validator.validateElement cityElement).toBeFalsy()
+
+ it "will mark the element with a validity message", ->
+ cityElement.value = 5
+ validator.validateElement cityElement
+ expect(cityElement.validationMessage).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
\ No newline at end of file