UNPKG

2.94 kBJavaScriptView Raw
1var assert = require('assert');
2var domify = require('domify');
3
4var serialize = require('../');
5
6var hash_check = function(form, exp) {
7 assert.deepEqual(serialize(form, { hash: true }), exp);
8};
9
10var str_check = function(form, exp) {
11 assert.equal(serialize(form), exp);
12};
13
14test('nothing', function() {
15 var form = domify('<form></form>');
16 hash_check(form, {});
17 str_check(form, '');
18});
19
20// basic form with single input
21test('single element', function() {
22 var form = domify('<form><input type="text" name="foo" value="bar"/></form>');
23 hash_check(form, {
24 'foo': 'bar'
25 });
26 str_check(form, 'foo=bar');
27});
28
29test('ignore no value', function() {
30 var form = domify('<form><input type="text" name="foo"/></form>');
31 hash_check(form, {});
32 str_check(form, '');
33});
34
35test('multi inputs', function() {
36 var form = domify('<form>' +
37 '<input type="text" name="foo" value="bar 1"/>' +
38 '<input type="text" name="foo.bar" value="bar 2"/>' +
39 '<input type="text" name="baz.foo" value="bar 3"/>' +
40 '</form>');
41 hash_check(form, {
42 'foo': 'bar 1',
43 'foo.bar': 'bar 2',
44 'baz.foo': 'bar 3'
45 });
46 str_check(form, 'foo=bar+1&foo.bar=bar+2&baz.foo=bar+3');
47});
48
49test('ignore disabled', function() {
50 var form = domify('<form>' +
51 '<input type="text" name="foo" value="bar 1"/>' +
52 '<input type="text" name="foo.bar" value="bar 2" disabled/>' +
53 '</form>');
54 hash_check(form, {
55 'foo': 'bar 1'
56 });
57 str_check(form, 'foo=bar+1');
58});
59
60test('ignore buttons', function() {
61 var form = domify('<form>' +
62 '<input type="submit" name="foo" value="submit"/>' +
63 '<input type="reset" name="foo.bar" value="reset"/>' +
64 '</form>');
65 hash_check(form, {});
66 str_check(form, '');
67});
68
69test('checkboxes', function() {
70 var form = domify('<form>' +
71 '<input type="checkbox" name="foo" checked/>' +
72 '<input type="checkbox" name="bar"/>' +
73 '<input type="checkbox" name="baz" checked/>' +
74 '</form>');
75 hash_check(form, {
76 'foo': "on",
77 'baz': "on"
78 });
79 str_check(form, 'foo=on&baz=on');
80});
81
82test('select - single', function() {
83 var form = domify('<form>' +
84 '<select name="foo">' +
85 '<option value="bar">bar</option>' +
86 '<option value="baz" selected>baz</option>' +
87 '</select>' +
88 '</form>');
89 hash_check(form, {
90 'foo': 'baz'
91 });
92 str_check(form, 'foo=baz');
93});
94
95test('select - multiple', function() {
96 var form = domify('<form>' +
97 '<select name="foo" multiple>' +
98 '<option value="bar" selected>bar</option>' +
99 '<option value="baz">baz</option>' +
100 '<option value="cat" selected>cat</option>' +
101 '</select>' +
102 '</form>');
103 hash_check(form, {
104 'foo': ['bar', 'cat']
105 });
106 str_check(form, 'foo=bar&foo=cat');
107});