Skip to content

Commit f6f799f

Browse files
committed
Merge pull request BaiduFE#270 from tianlili/dev
添加baidu.form的用例
2 parents 930d7d8 + 8f05534 commit f6f799f

File tree

3 files changed

+128
-23
lines changed

3 files changed

+128
-23
lines changed

test/baidu/form/json.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ function createForm() {
88

99
text1 = document.createElement('input');
1010
text2 = document.createElement('input');
11+
text3 = document.createElement('input');
12+
text4 = document.createElement('input');
1113
hid = document.createElement('input');
1214
rb1 = document.createElement('input');
1315
rb2 = document.createElement('input');
1416
cb1 = document.createElement('input');
1517
cb2 = document.createElement('input');
18+
cb3 = document.createElement('input');
1619
pass = document.createElement('input');
20+
bu = document.createElement('input');
1721
textArea = document.createElement('textarea');
1822
sel = document.createElement('select');
1923
selmul = document.createElement('select');
@@ -23,46 +27,63 @@ function createForm() {
2327
document.body.appendChild(div);
2428

2529
text1.type = "text";
26-
text1.disabled = "disabled";
30+
text1.name = "disable";
2731
text1.value = "disable";
32+
text1.disabled = "disabled";
2833

2934
text2.type = "text";
30-
text2.value = "param&1";
3135
text2.name = "param1";
36+
text2.value = "param&1 测试中文";
37+
text3.type = "text";
38+
text3.name = "param2";
39+
text3.value = "";
40+
text4.type = "text";
41+
text4.value = "param3";
3242

3343
hid.type = "hidden";
34-
hid.name = "param2";
35-
hid.value = "param2";
44+
hid.name = "hidden3";
45+
hid.value = "hidden3";
3646

3747
form.appendChild(text1);
3848
form.appendChild(text2);
49+
form.appendChild(text3);
50+
form.appendChild(text4);
3951
form.appendChild(hid);
4052

4153
rb1.type = "radio";
42-
rb1.value = "rb1";
4354
rb1.name = "rb";
55+
rb1.value = "rb1";
4456
rb2.type = "radio";
45-
rb2.value = "rb2";
4657
rb2.name = "rb";
47-
cb1.name = "cb";
58+
rb2.value = "rb2";
4859
cb1.type = "checkbox";
60+
cb1.name = "cb";
4961
cb1.value = "cb1";
50-
cb2.name = "cb";
5162
cb2.type = "checkbox";
63+
cb2.name = "cb";
5264
cb2.value = "cb2";
65+
cb3.type = "checkbox";
66+
cb3.name = "cb";
67+
cb3.value = "cb3";
5368

5469
form.appendChild(cb1);
5570
form.appendChild(cb2);
71+
form.appendChild(cb3);
5672
form.appendChild(rb1);
5773
form.appendChild(rb2);
5874

5975
rb2.checked = true;
6076
cb1.checked = true;
77+
cb3.checked = true;
6178

6279
pass.type = "password";
63-
pass.value = "pwd";
6480
pass.name = "pwd";
81+
pass.value = "pwd";
6582

83+
bu.type = "button";
84+
bu.name = "bu";
85+
bu.value = "bu1";
86+
6687
textArea.name = "ta";
6788
textArea.value = "textarea";
6889

@@ -86,9 +107,11 @@ function createForm() {
86107
selmul.options[2].selected = "selected";
87108

88109
button.id = "sub";
110+
button.name = "sub";
89111
button.value = "提交";
90112

91113
form.appendChild(pass);
114+
form.appendChild(bu);
92115
form.appendChild(textArea);
93116
form.appendChild(sel);
94117
form.appendChild(selmul);
@@ -97,6 +120,24 @@ function createForm() {
97120
}
98121

99122
test("传入form,返回结果", function() {
123+
expect(13);
124+
var count = 0;
100125
var f = createForm();
101-
baidu.form.json(f);
126+
var j = baidu.form.json(f);
127+
for(var i in j )
128+
count ++;
129+
equals(count, 9, "9 arears");
130+
equals(j.param1, "param%261%20测试中文", "The text input(escapeUrl) is right");
131+
equals(j.param2, "", "The text input(no value) is right");
132+
equals(j.hidden3, "hidden3", "The text input(hidden) is right");
133+
equals(j.cb[0], "cb1", "The combox is right");
134+
equals(j.cb[1], "cb3", "The combox is right");
135+
equals(j.rb, "rb2", "The radio is right");
136+
equals(j.pwd, "pwd", "The password is right");
137+
equals(j.ta, "textarea", "The textarea is right");
138+
equals(j.sel, "3", "The select is right");
139+
equals(j.selmul[0], "1", "The selmul is right");
140+
equals(j.selmul[1], "2", "The selmul is right");
141+
equals(j.selmul[2], "3", "The selmul is right");
142+
$(f).remove();
102143
});

test/baidu/form/manual.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>form</title>
6+
<!--qunit js -->
7+
<script type="text/javascript" src="../../../src/import.php?f=baidu.form,baidu.form.json,baidu.form.serialize"></script>
8+
<script type="text/javascript" >
9+
function json(){
10+
var j = baidu.form.json(document.getElementById('test'));
11+
alert(j.file1);
12+
};
13+
function serialize(){
14+
var j = baidu.form.serialize(document.getElementById('test'));
15+
alert(j[0]);
16+
}
17+
</script>
18+
</head>
19+
<body>
20+
<form id="test">
21+
<input type="file" name="file1">
22+
</form>
23+
<input type="button" name="json" value="查看json结果" onclick="json()">
24+
<input type="button" name="json" value="查看serialize结果"onclick="serialize()">
25+
</body>
26+
</html>

test/baidu/form/serialize.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module('baidu.form.serialize');
1+
module('baidu.form.json');
22
function createForm() {
33
var div, form, text1, text2, hid, cb1, cb2, rb1, rb2, pass, textArea, sel, selmul, button;
44
div = document.createElement('div');
@@ -8,12 +8,16 @@ function createForm() {
88

99
text1 = document.createElement('input');
1010
text2 = document.createElement('input');
11+
text3 = document.createElement('input');
12+
text4 = document.createElement('input');
1113
hid = document.createElement('input');
1214
rb1 = document.createElement('input');
1315
rb2 = document.createElement('input');
1416
cb1 = document.createElement('input');
1517
cb2 = document.createElement('input');
18+
cb3 = document.createElement('input');
1619
pass = document.createElement('input');
20+
bu = document.createElement('input');
1721
textArea = document.createElement('textarea');
1822
sel = document.createElement('select');
1923
selmul = document.createElement('select');
@@ -23,46 +27,63 @@ function createForm() {
2327
document.body.appendChild(div);
2428

2529
text1.type = "text";
26-
text1.disabled = "disabled";
30+
text1.name = "disable";
2731
text1.value = "disable";
32+
text1.disabled = "disabled";
2833

2934
text2.type = "text";
30-
text2.value = "param&1";
3135
text2.name = "param1";
36+
text2.value = "param&1 测试中文";
37+
text3.type = "text";
38+
text3.name = "param2";
39+
text3.value = "";
40+
text4.type = "text";
41+
text4.value = "param3";
3242

3343
hid.type = "hidden";
34-
hid.name = "param2";
35-
hid.value = "param2";
44+
hid.name = "hidden3";
45+
hid.value = "hidden3";
3646

3747
form.appendChild(text1);
3848
form.appendChild(text2);
49+
form.appendChild(text3);
50+
form.appendChild(text4);
3951
form.appendChild(hid);
4052

4153
rb1.type = "radio";
42-
rb1.value = "rb1";
4354
rb1.name = "rb";
55+
rb1.value = "rb1";
4456
rb2.type = "radio";
45-
rb2.value = "rb2";
4657
rb2.name = "rb";
47-
cb1.name = "cb";
58+
rb2.value = "rb2";
4859
cb1.type = "checkbox";
60+
cb1.name = "cb";
4961
cb1.value = "cb1";
50-
cb2.name = "cb";
5162
cb2.type = "checkbox";
63+
cb2.name = "cb";
5264
cb2.value = "cb2";
65+
cb3.type = "checkbox";
66+
cb3.name = "cb";
67+
cb3.value = "cb3";
5368

5469
form.appendChild(cb1);
5570
form.appendChild(cb2);
71+
form.appendChild(cb3);
5672
form.appendChild(rb1);
5773
form.appendChild(rb2);
5874

5975
rb2.checked = true;
6076
cb1.checked = true;
77+
cb3.checked = true;
6178

6279
pass.type = "password";
63-
pass.value = "pwd";
6480
pass.name = "pwd";
81+
pass.value = "pwd";
6582

83+
bu.type = "button";
84+
bu.name = "bu";
85+
bu.value = "bu1";
86+
6687
textArea.name = "ta";
6788
textArea.value = "textarea";
6889

@@ -72,8 +93,8 @@ function createForm() {
7293
sel.options[sel.options.length] = new Option('3', '3');
7394
sel.options[2].selected = "selected";
7495

75-
selmul.name = "selmul";
76-
selmul.multiple = "multiple";
96+
selmul.name = "selmul";
97+
selmul.multiple = "multiple";
7798
if(selmul.type != 'select-multiple'){
7899
selmul = document.createElement('<select name="selmul" multiple="true"></select>');
79100
}
@@ -86,9 +107,11 @@ function createForm() {
86107
selmul.options[2].selected = "selected";
87108

88109
button.id = "sub";
110+
button.name = "sub";
89111
button.value = "提交";
90112

91113
form.appendChild(pass);
114+
form.appendChild(bu);
92115
form.appendChild(textArea);
93116
form.appendChild(sel);
94117
form.appendChild(selmul);
@@ -97,6 +120,21 @@ function createForm() {
97120
}
98121

99122
test("传入form,返回结果", function() {
123+
expect(13);
100124
var f = createForm();
101-
baidu.form.serialize(f);
125+
var j = baidu.form.serialize(f);
126+
equals(j.length, 12, "12 arears");
127+
equals(j[0], "param1=param%261%20测试中文", "The text input(escapeUrl) is right");
128+
equals(j[1], "param2=", "The text input(no value) is right");
129+
equals(j[2], "hidden3=hidden3", "The text input(hidden) is right");
130+
equals(j[3], "cb=cb1", "The combox is right");
131+
equals(j[4], "cb=cb3", "The combox is right");
132+
equals(j[5], "rb=rb2", "The radio is right");
133+
equals(j[6], "pwd=pwd", "The password is right");
134+
equals(j[7], "ta=textarea", "The textarea is right");
135+
equals(j[8], "sel=3", "The select is right");
136+
equals(j[9], "selmul=1", "The selmul is right");
137+
equals(j[10], "selmul=2", "The selmul is right");
138+
equals(j[11], "selmul=3", "The selmul is right");
139+
$(f).remove();
102140
});

0 commit comments

Comments
 (0)