forked from bestpractical/jifty
/
06-forms.t
153 lines (126 loc) · 4.52 KB
/
06-forms.t
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use warnings;
use strict;
use Jifty::Test tests => 24;
use_ok ('Jifty::Web::Form::Field');
can_ok('Jifty::Web::Form::Field', 'new');
can_ok('Jifty::Web::Form::Field', 'name');
my $field = Jifty::Web::Form::Field->new();
# Form::Fields don't work without a framework
is($field->name, undef);
ok($field->name('Jesse'));
is($field->name, 'Jesse');
is($field->class, '');
is($field->class('basic'),'basic');
is($field->class(),'basic');
is($field->name, 'Jesse');
is ($field->type, 'text', "type defaults to text");
# Test render_XXX methods:
SKIP: {
eval {
require Test::MockObject;
require Test::MockModule;
};
skip "Test::MockObject and Test::MockModule required",
13 if $@;
my $out; # buf to store mason outputs
# mock up an instance of Jifty::Result
my $result = Test::MockObject->new;
$result->set_series('field_error', 'error: invalid email address', '');
$result->set_series('field_warning', 'warning: password too short', '');
$result->set_series('field_canonicalization_note', "I've changed it!", '');
# mock up an instance of Jifty::Action
my $action = Test::MockObject->new;
$action->set_always('result', $result);
$action->set_always('form_field_name', 'search_keys');
$action->set_series('error_div_id', 'FOO-error', 'no-error');
$action->set_series('warning_div_id', 'BAR-warning', 'no-warning');
$action->set_series('canonicalization_note_div_id', 'canonicalize', 'nothing');
# mock up an instance of Jifty::Web
my $web = Test::MockObject->new;
$web->mock('out', sub {
shift;
if (@_) { $out .= "@_" }
$out;
});
$web->set_always('serial', 32);
# mock up the Jifty package
my $module = new Test::MockModule('Jifty');
$module->mock('web', sub { $web });
# Test nonempty render_XXX:
$field = Jifty::Web::Form::Field->new();
$field->action($action);
$field->class('blah');
$field->name('agentz');
# Test render_lable:
$field->label('Audrey Tang');
$out = '';
$field->render_label;
is $out, qq{<label class="label text blah argument-agentz" for="search_keys-32">}.
qq{Audrey Tang</label>\n};
# Test render_hints:
$field->hints('She is here!');
$out = '';
$field->render_hints;
is $out, qq{<span class="hints text blah argument-agentz">She is here!</span>\n};
# Test render_errors:
$out = '';
$field->render_errors;
is $out, qq{<span class="error text blah argument-agentz" id="FOO-error">}.
qq{error: invalid email address</span>\n};
# Test render_warnings:
$out = '';
$field->render_warnings;
is $out, qq{<span class="warning text blah argument-agentz" id="BAR-warning">}.
qq{warning: password too short</span>\n};
# Test render_canonicalization_notes:
$out = '';
$field->render_canonicalization_notes;
is $out, qq{<span class="canonicalization_note text blah argument-agentz" }.
qq{id="canonicalize">I've changed it!</span>\n};
# Test render_preamble:
$out = '';
$field->preamble("preamble's here!");
$field->render_preamble;
is $out, qq{<span class="preamble text blah argument-agentz">}.
qq{preamble's here!</span>\n};
# Test empty labels are not output
$field->name('yichun');
$field->label('');
$field->class('');
$out = '';
$field->render_label;
is $out, qq{};
# Test default labels:
$field->name('yichun');
$field->label(undef);
$field->class('hey');
$out = '';
$field->render_label;
is $out, qq{<label class="label text hey argument-yichun" for="search_keys-32">}.
qq{yichun</label>\n};
# Test empty hints:
$field->name('xunxin');
$field->class('ujs');
$field->hints('');
$out = '';
$field->render_hints;
is $out, qq{<span class="hints text ujs argument-xunxin"></span>\n};
# Test empty errors:
$out = '';
$field->render_errors;
is $out, qq{<span class="error text ujs argument-xunxin" id="no-error"></span>\n};
# Test empty warnings:
$out = '';
$field->render_warnings;
is $out, qq{<span class="warning text ujs argument-xunxin" id="no-warning"></span>\n};
# Test empty canonicalization note:
$out = '';
$field->render_canonicalization_notes;
is $out, qq{<span class="canonicalization_note text ujs argument-xunxin" }.
qq{id="nothing"></span>\n};
# Test empty preambles:
$out = '';
$field->preamble("");
$field->render_preamble;
is $out, qq{<span class="preamble text ujs argument-xunxin"></span>\n};
}