hoarder-js 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/CHANGELOG +2 -0
  2. data/Gemfile.lock +2 -2
  3. data/Manifest +14 -3
  4. data/assets/scripts/coffee/hoarder/form/form.coffee +47 -44
  5. data/assets/scripts/coffee/hoarder/form/form_serializer.coffee +23 -0
  6. data/assets/scripts/coffee/hoarder/form_manager.coffee +35 -19
  7. data/assets/scripts/coffee/hoarder/submitter/form_submitter.coffee +10 -15
  8. data/assets/scripts/coffee/hoarder/submitter/submitters/base_submitter.coffee +11 -0
  9. data/assets/scripts/coffee/hoarder/submitter/submitters/polling_submitter.coffee +30 -27
  10. data/assets/scripts/coffee/hoarder/submitter/submitters/simple_submitter.coffee +11 -21
  11. data/assets/scripts/coffee/hoarder/validator/constraints/alpha_constraint.coffee +8 -14
  12. data/assets/scripts/coffee/hoarder/validator/constraints/alphanumeric_constraint.coffee +8 -14
  13. data/assets/scripts/coffee/hoarder/validator/constraints/base_constraint.coffee +10 -0
  14. data/assets/scripts/coffee/hoarder/validator/constraints/credit_card_constraint.coffee +8 -14
  15. data/assets/scripts/coffee/hoarder/validator/constraints/email_constraint.coffee +8 -14
  16. data/assets/scripts/coffee/hoarder/validator/constraints/max_length_constraint.coffee +8 -14
  17. data/assets/scripts/coffee/hoarder/validator/constraints/min_length_constraint.coffee +8 -14
  18. data/assets/scripts/coffee/hoarder/validator/constraints/numeric_constraint.coffee +8 -14
  19. data/assets/scripts/coffee/hoarder/validator/constraints/phone_constraint.coffee +8 -14
  20. data/assets/scripts/coffee/hoarder/validator/constraints/required_constraint.coffee +8 -14
  21. data/assets/scripts/coffee/hoarder/validator/form_validator.coffee +30 -25
  22. data/assets/scripts/coffee/hoarder/validator/rules/validation_rule.coffee +10 -0
  23. data/assets/scripts/js/lib/bonzo.js +1151 -0
  24. data/assets/scripts/js/lib/qwery.js +369 -0
  25. data/assets/scripts/js/lib/reqwest.js +565 -0
  26. data/assets/scripts/js/patches/event_listeners.js +73 -0
  27. data/bin/hoarder.js +2807 -369
  28. data/hoarder-js.gemspec +3 -3
  29. data/spec/jasmine.yml +3 -0
  30. data/spec/runner.html +44 -34
  31. data/spec/support/fixtures.coffee +19 -0
  32. data/spec/support/lib/jasmine-fixture.js +506 -0
  33. data/spec/support/lib/jquery-1.7.1.min.js +4 -0
  34. data/spec/support/mocks.coffee +12 -37
  35. data/spec/support/requirements.coffee +0 -1
  36. data/spec/tests/form/form_serializer_spec.coffee +78 -0
  37. data/spec/tests/form/form_spec.coffee +127 -0
  38. data/spec/tests/form_manager_spec.coffee +128 -40
  39. data/spec/tests/submitter/form_submitter_spec.coffee +59 -42
  40. data/spec/tests/submitter/submitters/polling_submitter_spec.coffee +102 -57
  41. data/spec/tests/submitter/submitters/simple_submitter_spec.coffee +42 -16
  42. data/spec/tests/validator/constraints_spec.coffee +65 -57
  43. data/spec/tests/validator/form_validator_spec.coffee +39 -23
  44. data/spec/tests/validator/validation_rule_spec.coffee +14 -0
  45. metadata +16 -5
  46. data/assets/scripts/coffee/hoarder/form/form_element.coffee +0 -22
  47. data/assets/scripts/coffee/hoarder/validator/error/validation_error.coffee +0 -9
  48. data/assets/scripts/js/lib/jquery.js +0 -5
@@ -1,60 +1,105 @@
1
1
  PollingSubmitter = require "hoarder/submitter/submitters/polling_submitter"
2
+ Form = require 'hoarder/form/form'
2
3
 
3
4
  describe "PollingSubmitter", ->
4
- submitter = null
5
-
6
- beforeEach ->
7
- submitter = new PollingSubmitter("/poll-check-form-submit", 500)
8
-
9
- it "can initiate a poll upon successful submission of a form", ->
10
- spyOn($, "ajax").andCallFake( (params)-> params.success(mocks.submitPollingFormResponse))
11
- spyOn(submitter, "queryPoll")
12
- submitter.submitForm(mocks.pollingForm)
13
- expect(submitter.queryPoll).toHaveBeenCalledWith(mocks.pollingForm, "1234")
14
-
15
- it "can respond to a successful poll check", ->
16
- spyOn($, "ajax").andCallFake( (params)-> params.success(mocks.submitFormPollingCheckResponse))
17
- spyOn(submitter.submittedWithSuccess, "dispatch")
18
- submitter.queryPoll(mocks.pollingForm, "1234")
19
- expect(submitter.submittedWithSuccess.dispatch).toHaveBeenCalledWith(mocks.pollingForm, mocks.submitFormPollingCheckResponse.pollData)
20
-
21
- it "can poll with the given frequency", ->
22
- jasmine.Clock.useMock()
23
- spyOn($, "ajax").andCallFake( (params)=> if $.ajax.calls.length < 3 then params.success(mocks.submitFormPollingCheckFailedResponse) else params.success(mocks.submitFormPollingCheckResponse))
24
- spyOn(submitter, "queryPoll").andCallThrough()
25
- spyOn(submitter.submittedWithSuccess, "dispatch")
26
- spyOn(submitter.submittedWithError, "dispatch")
27
- submitter.submitSuccess(mocks.pollingForm, { pollId:"1234" })
28
- expect(submitter.submittedWithSuccess.dispatch).not.toHaveBeenCalled()
29
- expect(submitter.submittedWithError.dispatch).not.toHaveBeenCalled()
30
- expect(submitter.queryPoll.calls.length).toEqual 1
31
- jasmine.Clock.tick 501
32
- expect(submitter.submittedWithSuccess.dispatch).not.toHaveBeenCalled()
33
- expect(submitter.submittedWithError.dispatch).not.toHaveBeenCalled()
34
- expect(submitter.queryPoll.calls.length).toEqual 2
35
- jasmine.Clock.tick 501
36
- expect(submitter.submittedWithSuccess.dispatch).toHaveBeenCalledWith(mocks.pollingForm, mocks.submitFormPollingCheckResponse.pollData)
37
-
38
- it "will stop polling after receiving a pollCompleted message", ->
39
- jasmine.Clock.useMock()
40
- spyOn($, "ajax").andCallFake( (params)-> params.success(mocks.submitFormPollingCheckResponse))
41
- spyOn(submitter, "queryPoll").andCallThrough()
42
- spyOn(submitter.submittedWithSuccess, "dispatch")
43
- spyOn(submitter.submittedWithError, "dispatch")
44
- submitter.submitSuccess(mocks.pollingForm, { pollId:"1234" })
45
- expect(submitter.submittedWithSuccess.dispatch).toHaveBeenCalledWith(mocks.pollingForm, mocks.submitFormPollingCheckResponse.pollData)
46
- expect(submitter.submittedWithSuccess.dispatch.calls.length).toEqual 1
47
- jasmine.Clock.tick 501
48
- expect(submitter.submittedWithSuccess.dispatch.calls.length).toEqual 1
49
-
50
- it "can respond to an error-ridden polling form submission", ->
51
- spyOn($, "ajax").andCallFake( (params)-> params.error({}, "Error!"))
52
- spyOn(submitter.submittedWithError, "dispatch")
53
- submitter.submitForm(mocks.pollingForm)
54
- expect(submitter.submittedWithError.dispatch).toHaveBeenCalledWith(mocks.pollingForm, "Error!" )
55
-
56
- it "can respond to an error-ridden poll check", ->
57
- spyOn($, "ajax").andCallFake( (params)-> params.error({}, "Error!"))
58
- spyOn(submitter.submittedWithError, "dispatch")
59
- submitter.queryPoll(mocks.pollingForm, "1234")
60
- expect(submitter.submittedWithError.dispatch).toHaveBeenCalledWith(mocks.pollingForm, "Error!" )
5
+ submitter = form = null
6
+
7
+ reqwestCallback = null
8
+ reqwestResponse = null
9
+ reqwestSpy = (params)-> params[reqwestCallback].apply null, reqwestResponse
10
+
11
+ # these are a result of using Function::apply above
12
+ successResponse = -> reqwestResponse[0]
13
+ errorResponse = -> reqwestResponse[1]
14
+
15
+ callbacks =
16
+ successHappened: (form, data)->
17
+ errorHappened: (form, errorMessage)->
18
+
19
+ beforeEach ->
20
+ createTestFormFixture()
21
+
22
+ form = new Form(document.getElementById('test-form'))
23
+ submitter = new PollingSubmitter("/poll-url", 500)
24
+
25
+ reqwestCallback = "success"
26
+ spyOn(callbacks, 'successHappened').andCallThrough()
27
+ spyOn(callbacks, 'errorHappened').andCallThrough()
28
+ submitter.submittedWithSuccess.add callbacks.successHappened
29
+ submitter.submittedWithError.add callbacks.errorHappened
30
+ spyOn(window, 'reqwest').andCallFake(reqwestSpy)
31
+
32
+ describe '#submit', ->
33
+
34
+ describe "when the submission is successful", ->
35
+
36
+ it "will initiate a poll", ->
37
+ spyOn(submitter, 'poll')
38
+ reqwestResponse = mocks.pollingSubmitSuccessResponse
39
+ submitter.submit(form)
40
+ expect(submitter.poll).toHaveBeenCalledWith(form, successResponse().processId)
41
+
42
+ describe "when an error occurs in the submission", ->
43
+
44
+ it "will call callbacks added to the submittedWithError signal", ->
45
+ reqwestCallback = "error"
46
+ reqwestResponse = mocks.errorResponse
47
+ submitter.submit(form)
48
+ expect(callbacks.errorHappened).toHaveBeenCalledWith(form, errorResponse())
49
+
50
+ describe '#poll', ->
51
+
52
+ beforeEach ->
53
+ spyOn(submitter, "poll").andCallThrough()
54
+
55
+ it "will poll with the given frequency", ->
56
+ reqwestResponse = mocks.pollingProcessNotCompletedResponse
57
+ jasmine.Clock.useMock()
58
+ submitter.poll(form, "1234")
59
+ expect(submitter.poll.calls.length).toEqual 1
60
+ jasmine.Clock.tick 501
61
+ expect(submitter.poll.calls.length).toEqual 2
62
+ jasmine.Clock.tick 501
63
+ expect(submitter.poll.calls.length).toEqual 3
64
+
65
+ describe "when the poll is successful", ->
66
+
67
+ describe "and the process has completed", ->
68
+
69
+ it "will stop polling", ->
70
+ reqwestResponse = mocks.pollingProcessNotCompletedResponse
71
+ jasmine.Clock.useMock()
72
+ submitter.poll(form, "1234")
73
+ expect(submitter.poll.calls.length).toEqual 1
74
+ reqwestResponse = mocks.pollingProcessCompletedResponse
75
+ jasmine.Clock.tick 501
76
+ expect(submitter.poll.calls.length).toEqual 2
77
+ jasmine.Clock.tick 501
78
+ expect(submitter.poll.calls.length).toEqual 2
79
+
80
+ it "will call callbacks added to the submittedWithSuccess signal", ->
81
+ reqwestResponse = mocks.pollingProcessCompletedResponse
82
+ submitter.poll(form, "1234")
83
+ expect(callbacks.successHappened).toHaveBeenCalledWith(form, successResponse().processData)
84
+
85
+ describe "and the process has not yet completed", ->
86
+
87
+ it "will initiate another poll", ->
88
+ reqwestResponse = mocks.pollingProcessNotCompletedResponse
89
+ jasmine.Clock.useMock()
90
+ submitter.poll(form, "1234")
91
+ jasmine.Clock.tick 501
92
+ expect(submitter.poll.calls.length).toBeGreaterThan 1
93
+
94
+ it "will not call callbacks added to the submittedWithSuccess signal", ->
95
+ reqwestResponse = mocks.pollingProcessNotCompletedResponse
96
+ submitter.poll(form, "1234")
97
+ expect(callbacks.successHappened).not.toHaveBeenCalled()
98
+
99
+ describe "when an error occurs in the polling", ->
100
+
101
+ it "will call callbacks added to the submittedWithError signal", ->
102
+ reqwestCallback = "error"
103
+ reqwestResponse = mocks.errorResponse
104
+ submitter.poll(form, "1234")
105
+ expect(callbacks.errorHappened).toHaveBeenCalledWith(form, errorResponse())
@@ -1,19 +1,45 @@
1
1
  SimpleSubmitter = require "hoarder/submitter/submitters/simple_submitter"
2
+ Form = require 'hoarder/form/form'
2
3
 
3
4
  describe "SimpleSubmitter", ->
4
- submitter = null
5
-
6
- beforeEach ->
7
- submitter = new SimpleSubmitter()
8
-
9
- it "can submit a form, and respond to a successful submit", ->
10
- spyOn(submitter.submittedWithSuccess, "dispatch")
11
- spyOn($,"ajax").andCallFake( (params)-> params.success(mocks.submitSimpleFormResponse))
12
- submitter.submitForm(mocks.simpleForm)
13
- expect(submitter.submittedWithSuccess.dispatch).toHaveBeenCalledWith(mocks.simpleForm, mocks.submitSimpleFormResponse)
14
-
15
- it "can respond to an error-ridden submission", ->
16
- spyOn($, "ajax").andCallFake( (params)-> params.error({}, "Error!"))
17
- spyOn(submitter.submittedWithError, "dispatch")
18
- submitter.submitForm(mocks.simpleForm)
19
- expect(submitter.submittedWithError.dispatch).toHaveBeenCalledWith(mocks.simpleForm, "Error!" )
5
+ submitter = form = null
6
+
7
+ reqwestCallback = "success"
8
+ reqwestResponse = null
9
+ reqwestSpy = (params)-> params[reqwestCallback].apply null, reqwestResponse
10
+
11
+ # these are a result of using Function::apply above
12
+ successResponse = -> reqwestResponse[0]
13
+ errorResponse = -> reqwestResponse[1]
14
+
15
+ callbacks =
16
+ successHappened: (form, data)->
17
+ errorHappened: (form, errorMessage)->
18
+
19
+ beforeEach ->
20
+ createTestFormFixture()
21
+
22
+ form = new Form(document.getElementById('test-form'))
23
+ submitter = new SimpleSubmitter()
24
+ spyOn(callbacks, 'successHappened').andCallThrough()
25
+ spyOn(callbacks, 'errorHappened').andCallThrough()
26
+ submitter.submittedWithSuccess.add callbacks.successHappened
27
+ submitter.submittedWithError.add callbacks.errorHappened
28
+ spyOn(window, 'reqwest').andCallFake(reqwestSpy)
29
+
30
+ describe '#submit', ->
31
+
32
+ describe "when the submission is successful", ->
33
+
34
+ it "will call callbacks added to the submittedWithSuccess signal", ->
35
+ reqwestResponse = mocks.simpleSuccessResponse
36
+ submitter.submit(form)
37
+ expect(callbacks.successHappened).toHaveBeenCalledWith(form, successResponse())
38
+
39
+ describe "when an error occurs in the submission", ->
40
+
41
+ it "will call callbacks added to the submittedWithError signal", ->
42
+ reqwestCallback = 'error'
43
+ reqwestResponse = mocks.errorResponse
44
+ submitter.submit(form)
45
+ expect(callbacks.errorHappened).toHaveBeenCalledWith(form, errorResponse())
@@ -1,6 +1,4 @@
1
- FormElement = require 'hoarder/form/form_element'
2
-
3
- #AlphaConstraint = require "/hoarder/validator/constraints/alpha_constraint"
1
+ AlphaConstraint = require "hoarder/validator/constraints/alpha_constraint"
4
2
  AlphanumericConstraint = require "hoarder/validator/constraints/alphanumeric_constraint"
5
3
  CreditCardConstraint = require "hoarder/validator/constraints/credit_card_constraint"
6
4
  EmailConstraint = require "hoarder/validator/constraints/email_constraint"
@@ -11,64 +9,74 @@ PhoneConstraint = require "hoarder/validator/constraints/phone_constraint"
11
9
  RequiredConstraint = require "hoarder/validator/constraints/required_constraint"
12
10
 
13
11
  describe "Validator Constraints", ->
14
- it "can require that a value be alphanumeric only", ->
15
- constraint = new AlphanumericConstraint()
16
- validElement = new FormElement("fullName", "71m5h3lburn3")
17
- invalidElement = new FormElement("fullName", "T!m $h3lbur*3")
18
- expect(constraint.handle(validElement)).toEqual []
19
- expect(constraint.handle(invalidElement)[0].message).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
20
12
 
21
- it "can require that a value be a valid credit card number", ->
22
- constraint = new CreditCardConstraint()
23
- validElement = new FormElement("number", "4111111111111111")
24
- invalidElement = new FormElement("number", "12345")
25
- expect(constraint.handle(validElement)).toEqual []
26
- expect(constraint.handle(invalidElement)[0].message).toEqual "Please enter a valid credit card number."
13
+ newElement = (name, value)-> { name: name, value: value }
14
+
15
+ it "can require that a value be alpha only", ->
16
+ constraint = new AlphaConstraint()
17
+ validElement = newElement("fullName", "ABCabc")
18
+ invalidElement = newElement("fullName", "a1b2c3")
19
+ expect(-> constraint.handle(validElement)).not.toThrow()
20
+ expect(-> constraint.handle(invalidElement)).toThrow()
21
+
22
+ it "can require that a value be alphanumeric only", ->
23
+ constraint = new AlphanumericConstraint()
24
+ validElement = newElement("fullName", "71m5h3lburn3")
25
+ invalidElement = newElement("fullName", "T!m $h3lbur*3")
26
+ expect(-> constraint.handle(validElement)).not.toThrow()
27
+ expect(-> constraint.handle(invalidElement)).toThrow()
28
+
29
+ it "can require that a value be a valid credit card number", ->
30
+ constraint = new CreditCardConstraint()
31
+ validElement = newElement("number", "4111111111111111")
32
+ invalidElement = newElement("number", "12345")
33
+ expect(-> constraint.handle(validElement)).not.toThrow()
34
+ expect(-> constraint.handle(invalidElement)).toThrow()
27
35
 
28
- it "can require that a value be a valid email address", ->
29
- constraint = new EmailConstraint()
30
- validElement = new FormElement("email", "tim@musiconelive.com")
31
- invalidElement = new FormElement("email", "tim she@d")
32
- expect(constraint.handle(validElement)).toEqual []
33
- expect(constraint.handle(invalidElement)[0].message).toEqual "Please enter a valid email address."
36
+ it "can require that a value be a valid email address", ->
37
+ constraint = new EmailConstraint()
38
+ validElement = newElement("email", "tim@musiconelive.com")
39
+ invalidElement = newElement("email", "tim she@d")
40
+ expect(-> constraint.handle(validElement)).not.toThrow()
41
+ expect(-> constraint.handle(invalidElement)).toThrow()
34
42
 
35
- it "can require that a value have a maximum length", ->
36
- constraint = new MaxLengthConstraint()
37
- validElement = new FormElement("zipCode", "78765")
38
- validElementTwo = new FormElement("zipCode", "787")
39
- invalidElement = new FormElement("zipCode", "787657")
40
- expect(constraint.handle(validElement, { value: 5 })).toEqual []
41
- expect(constraint.handle(validElementTwo, { value: 5 })).toEqual []
42
- expect(constraint.handle(invalidElement, { value: 5 })[0].message).toEqual "The maximum length of this field is 5."
43
+ it "can require that a value have a maximum length", ->
44
+ constraint = new MaxLengthConstraint()
45
+ validElement = newElement("zipCode", "78765")
46
+ validElementTwo = newElement("zipCode", "787")
47
+ invalidElement = newElement("zipCode", "787657")
48
+ expect(-> constraint.handle(validElement, { context: 5 })).not.toThrow()
49
+ expect(-> constraint.handle(validElementTwo, { context: 5 })).not.toThrow()
50
+ expect(-> constraint.handle(invalidElement, { context: 5 })).toThrow()
43
51
 
44
- it "can require that a value have a minimum length", ->
45
- constraint = new MinLengthConstraint()
46
- validElement = new FormElement("zipCode", "78765")
47
- validElementTwo = new FormElement("zipCode", "787643")
48
- invalidElement = new FormElement("zipCode", "787")
49
- expect(constraint.handle(validElement, { value: 5 })).toEqual []
50
- expect(constraint.handle(validElementTwo, { value: 5 })).toEqual []
51
- expect(constraint.handle(invalidElement, { value: 5 })[0].message).toEqual "The minimum length of this field is 5."
52
+ it "can require that a value have a minimum length", ->
53
+ constraint = new MinLengthConstraint()
54
+ validElement = newElement("zipCode", "78765")
55
+ validElementTwo = newElement("zipCode", "787643")
56
+ invalidElement = newElement("zipCode", "787")
57
+ expect(-> constraint.handle(validElement, { context: 5 })).not.toThrow()
58
+ expect(-> constraint.handle(validElementTwo, { context: 5 })).not.toThrow()
59
+ expect(-> constraint.handle(invalidElement, { context: 5 })).toThrow()
52
60
 
53
- it "can require that a value be numeric only", ->
54
- constraint = new NumericConstraint()
55
- validElement = new FormElement("zipCode", "78765")
56
- invalidElement = new FormElement("zipCode", "Seven8765")
57
- expect(constraint.handle(validElement)).toEqual []
58
- expect(constraint.handle(invalidElement)[0].message).toEqual "This field only accepts numbers (0-9)."
61
+ it "can require that a value be numeric only", ->
62
+ constraint = new NumericConstraint()
63
+ validElement = newElement("zipCode", "78765")
64
+ invalidElement = newElement("zipCode", "Seven8765")
65
+ expect(-> constraint.handle(validElement)).not.toThrow()
66
+ expect(-> constraint.handle(invalidElement)).toThrow()
59
67
 
60
- it "can require that a value be a valid phone number", ->
61
- constraint = new PhoneConstraint()
62
- validElement = new FormElement("phone", "(555)555-5555")
63
- invalidElement = new FormElement("phone", "12341")
64
- expect(constraint.handle(validElement)).toEqual []
65
- expect(constraint.handle(invalidElement)[0].message).toEqual "Please enter a valid phone number."
68
+ it "can require that a value be a valid phone number", ->
69
+ constraint = new PhoneConstraint()
70
+ validElement = newElement("phone", "(555)555-5555")
71
+ invalidElement = newElement("phone", "12341")
72
+ expect(-> constraint.handle(validElement)).not.toThrow()
73
+ expect(-> constraint.handle(invalidElement)).toThrow()
66
74
 
67
- it "can require that a value be required", ->
68
- constraint = new RequiredConstraint()
69
- validElement = new FormElement("fullName", "Tim Shelburne")
70
- invalidElement = new FormElement("fullName", "")
71
- invalidElementTwo = new FormElement("fullName", null)
72
- expect(constraint.handle(validElement)).toEqual []
73
- expect(constraint.handle(invalidElement)[0].message).toEqual "This field is required."
74
- expect(constraint.handle(invalidElementTwo)[0].message).toEqual "This field is required."
75
+ it "can require that a value be required", ->
76
+ constraint = new RequiredConstraint()
77
+ validElement = newElement("fullName", "Tim Shelburne")
78
+ invalidElement = newElement("fullName", "")
79
+ invalidElementTwo = newElement("fullName", null)
80
+ expect(-> constraint.handle(validElement)).not.toThrow()
81
+ expect(-> constraint.handle(invalidElement)).toThrow()
82
+ expect(-> constraint.handle(invalidElementTwo)).toThrow()
@@ -1,29 +1,45 @@
1
- FormElement = require "hoarder/form/form_element"
1
+ Form = require 'hoarder/form/form'
2
2
  FormValidator = require "hoarder/validator/form_validator"
3
3
 
4
4
  describe "FormValidator", ->
5
5
  validator = null
6
- element = null
6
+ form = cityElement = null
7
7
 
8
8
  beforeEach ->
9
- validator = FormValidator.default()
10
- element = new FormElement("element", "$", "test-element", [ "alphanumeric", "creditCard", "email", "maxLength=5", "minLength=5", "numeric", "phone", "required" ])
11
-
12
- it "can validate an element", ->
13
- errors = validator.validateElement(element)
14
- jasmine.log errors
15
- expect(errors[0].message).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
16
- element.value = "4111111111111111"
17
- errors = validator.validateElement(element)
18
- jasmine.log errors
19
- expect(errors[0].message).toEqual "Please enter a valid email address."
20
- element.value = "6854291"
21
- errors = validator.validateElement(element)
22
- jasmine.log errors
23
- expect(errors[0].message).toEqual "Please enter a valid credit card number."
24
-
25
- it "can validate a form", ->
26
- errors = validator.validateForm(mocks.simpleForm)
27
- expect(errors.length).toEqual 0
28
- errors = validator.validateForm(mocks.invalidForm)
29
- expect(errors.length).toEqual 4
9
+ createTestFormFixture()
10
+
11
+ formElement = document.getElementById 'test-form'
12
+ cityElement = formElement['city']
13
+
14
+ form = new Form(formElement)
15
+ validator = FormValidator.create()
16
+
17
+ describe '#validateForm', ->
18
+
19
+ it "will return true if the form is valid", ->
20
+ expect(validator.validateForm form).toBeTruthy()
21
+
22
+ it "will return false if any part of the form is invalid", ->
23
+ cityElement.value = 5
24
+ expect(validator.validateForm form).toBeFalsy()
25
+
26
+ it "will mark the invalid elements with a validity message", ->
27
+ cityElement.value = 5
28
+ validator.validateForm form
29
+ expect(cityElement.validationMessage).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
30
+
31
+ describe '#validateElement', ->
32
+
33
+ it "will return true if the element is valid", ->
34
+ expect(validator.validateElement cityElement).toBeTruthy()
35
+
36
+ describe "when the element is invalid", ->
37
+
38
+ it "will return false", ->
39
+ cityElement.value = 5
40
+ expect(validator.validateElement cityElement).toBeFalsy()
41
+
42
+ it "will mark the element with a validity message", ->
43
+ cityElement.value = 5
44
+ validator.validateElement cityElement
45
+ expect(cityElement.validationMessage).toEqual "This field only accepts numbers and characters (0-9, A-Z, a-z)."
@@ -0,0 +1,14 @@
1
+ ValidationRule = require 'hoarder/validator/rules/validation_rule'
2
+
3
+ describe 'ValidationRule', ->
4
+
5
+ describe '::fromString', ->
6
+
7
+ it "will build a rule with only a type", ->
8
+ rule = ValidationRule.fromString("email")
9
+ expect(rule.constructor).toEqual ValidationRule
10
+
11
+ it "will build a rule with context", ->
12
+ rule = ValidationRule.fromString("maxLength=5")
13
+ expect(rule.constructor).toEqual ValidationRule
14
+ expect(rule.context).toEqual "5"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoarder-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
12
+ date: 2013-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jasmine
@@ -64,13 +64,15 @@ files:
64
64
  - README.rdoc
65
65
  - Rakefile
66
66
  - assets/scripts/coffee/hoarder/form/form.coffee
67
- - assets/scripts/coffee/hoarder/form/form_element.coffee
67
+ - assets/scripts/coffee/hoarder/form/form_serializer.coffee
68
68
  - assets/scripts/coffee/hoarder/form_manager.coffee
69
69
  - assets/scripts/coffee/hoarder/submitter/form_submitter.coffee
70
+ - assets/scripts/coffee/hoarder/submitter/submitters/base_submitter.coffee
70
71
  - assets/scripts/coffee/hoarder/submitter/submitters/polling_submitter.coffee
71
72
  - assets/scripts/coffee/hoarder/submitter/submitters/simple_submitter.coffee
72
73
  - assets/scripts/coffee/hoarder/validator/constraints/alpha_constraint.coffee
73
74
  - assets/scripts/coffee/hoarder/validator/constraints/alphanumeric_constraint.coffee
75
+ - assets/scripts/coffee/hoarder/validator/constraints/base_constraint.coffee
74
76
  - assets/scripts/coffee/hoarder/validator/constraints/credit_card_constraint.coffee
75
77
  - assets/scripts/coffee/hoarder/validator/constraints/email_constraint.coffee
76
78
  - assets/scripts/coffee/hoarder/validator/constraints/max_length_constraint.coffee
@@ -78,9 +80,12 @@ files:
78
80
  - assets/scripts/coffee/hoarder/validator/constraints/numeric_constraint.coffee
79
81
  - assets/scripts/coffee/hoarder/validator/constraints/phone_constraint.coffee
80
82
  - assets/scripts/coffee/hoarder/validator/constraints/required_constraint.coffee
81
- - assets/scripts/coffee/hoarder/validator/error/validation_error.coffee
82
83
  - assets/scripts/coffee/hoarder/validator/form_validator.coffee
83
- - assets/scripts/js/lib/jquery.js
84
+ - assets/scripts/coffee/hoarder/validator/rules/validation_rule.coffee
85
+ - assets/scripts/js/lib/bonzo.js
86
+ - assets/scripts/js/lib/qwery.js
87
+ - assets/scripts/js/lib/reqwest.js
88
+ - assets/scripts/js/patches/event_listeners.js
84
89
  - bin/hoarder.js
85
90
  - config/assets.rb
86
91
  - lib/hoarder.rb
@@ -88,16 +93,22 @@ files:
88
93
  - spec/jasmine.yml
89
94
  - spec/runner.html
90
95
  - spec/support/classes.coffee
96
+ - spec/support/fixtures.coffee
91
97
  - spec/support/helpers.coffee
98
+ - spec/support/lib/jasmine-fixture.js
99
+ - spec/support/lib/jquery-1.7.1.min.js
92
100
  - spec/support/mocks.coffee
93
101
  - spec/support/objects.coffee
94
102
  - spec/support/requirements.coffee
103
+ - spec/tests/form/form_serializer_spec.coffee
104
+ - spec/tests/form/form_spec.coffee
95
105
  - spec/tests/form_manager_spec.coffee
96
106
  - spec/tests/submitter/form_submitter_spec.coffee
97
107
  - spec/tests/submitter/submitters/polling_submitter_spec.coffee
98
108
  - spec/tests/submitter/submitters/simple_submitter_spec.coffee
99
109
  - spec/tests/validator/constraints_spec.coffee
100
110
  - spec/tests/validator/form_validator_spec.coffee
111
+ - spec/tests/validator/validation_rule_spec.coffee
101
112
  - hoarder-js.gemspec
102
113
  homepage: https://github.com/tshelburne/hoarder-js
103
114
  licenses: []