forked from retro/FormBinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_binder_test.js
135 lines (115 loc) · 4.52 KB
/
form_binder_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
steal(
"jquery/class",
'jquery/controller',
'jquery/model',
'jquery/model/validations',
'funcunit/qunit',
'jquery/view',
'form_binder')
.then(function(){
$.Controller('Cntrlr', {
init : function(){
this.element.html($.View('form', {m: model}))
}
});
$.Model("BlogPost",{
attributes : {
title : 'string',
lead : 'text',
body : 'text',
is_published : 'boolean',
status : 'string',
tags : 'array',
author : 'string'
},
valuesForTags : ['test', 'tag'],
valuesForStatus : [['published', 'Published'], ['draft', 'Draft']],
valuesForAuthor : ['Mihael', 'Justin', 'Brian', 'Austin'],
init : function(){
this.validatePresenceOf('title');
this.validatePresenceOf('lead');
},
}, {});
model = new BlogPost();
module("form_binder test", {
setup: function(){
$('<div id="content"></div>').appendTo($("#qunit-test-area"));
$('#content').cntrlr();
}
});
test("Changing a value in form changes a value in model", function(){
stop();
$('#blog_post_title').val('Title').trigger('blur');
equals('Title', model.attr('title'));
$('#blog_post_lead').val('Lead').trigger('blur');
equals('Lead', model.attr('lead'));
start();
});
test("Changing an attribute in model reflects in form", function(){
model.attr('title', 'Title2');
equals($('#blog_post_title').val(), 'Title2');
model.attr('body', 'Body');
equals($('#blog_post_body').val(), 'Body');
})
test("Changing an attribute in model reflects in password field", function(){
model.attr('password', 'pass');
equals($('#blog_post_password').val(), 'pass');
})
test("Setting an array attribute should check checkboxes in checkbox group", function(){
model.attr('tags', ['test', 'tag'])
equals($('[name="blog_post[tags][]"]:checked').length, 2)
model.attr('tags', ['tag'])
equals($('[name="blog_post[tags][]"]:checked').length, 1)
model.attr('tags', [])
equals($('[name="blog_post[tags][]"]:checked').length, 0)
})
test("Setting a value should reflect in select box", function(){
model.attr('status', 'published')
equals($('#blog_post_status').val(), 'published')
model.attr('status', 'draft')
equals($('#blog_post_status').val(), 'draft')
})
test("Validation should work on blur", function(){
$('#blog_post_title').val('').trigger('blur')
equals($('#blog_post_title').parents('.input-wrapper').find('.errors').text(), "can't be empty")
})
test("Errors should be removed after valid data is entered", function(){
$('#blog_post_title').val('').trigger('blur')
equals($('#blog_post_title').parents('.input-wrapper').find('.errors').text(), "can't be empty")
$('#blog_post_title').val('Title').trigger('blur')
equals($('#blog_post_title').parents('.input-wrapper').find('.errors').text(), "")
})
test("Errors should be removed after valid data is entered", function(){
$('#blog_post_title').val('').trigger('blur')
equals($('#blog_post_title').parents('.input-wrapper').find('.errors').text(), "can't be empty")
$('#blog_post_title').val('Title').trigger('blur')
equals($('#blog_post_title').parents('.input-wrapper').find('.errors').text(), "")
})
test("Setting an boolean value should reflect in checkbox being checked", function(){
model.attr('is_published', true)
equals($('#blog_post_is_published:checked').length, 1)
})
test("Setting an value in model should select this value in a radio group", function(){
model.attr('author', 'Mihael')
equals($('[name="blog_post[author]"]:checked').val(), 'Mihael')
model.attr('author', 'Justin')
equals($('[name="blog_post[author]"]:checked').val(), 'Justin')
})
test("Clicking an radio button should set value in model", function(){
$('[name="blog_post[author]"]:eq(3)').trigger('click').trigger('change');
equals(model.attr('author'), 'Austin')
})
test("Setting a value of form field that isn't set in attributes should work as expected", function(){
$('#blog_post_non_existing_attribute').val('Test value').trigger('change');
equals(model.attr('non_existing_attribute'), 'Test value')
})
test("Setting an non_existing_attribute should set value in form field", function(){
model.attr('non_existing_attribute2', 'Test value');
equals($('#blog_post_non_existing_attribute2').val(), 'Test value')
})
test("providing inline values to selects, checkbox groups and radio groups", function(){
equals($('#blog_post_select_field option').length, 2)
equals($('[name="blog_post[radio_group_field]"]').length, 2)
equals($('[name="blog_post[checkbox_group_field][]"]').length, 2)
})
})