| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Exception; |
| 5 |
|
| 6 |
{ |
| 7 |
package Test::Form; |
| 8 |
use HTML::FormHandler::Moose; |
| 9 |
extends 'HTML::FormHandler'; |
| 10 |
|
| 11 |
has_field 'bar'; |
| 12 |
has_field 'foo' => ( inactive => 1 ); |
| 13 |
has_field 'foo_checkbox' => ( type => 'Checkbox', inactive => 1 ); |
| 14 |
} |
| 15 |
|
| 16 |
my $form = Test::Form->new; |
| 17 |
ok( $form, 'form builds' ); |
| 18 |
$form->process; |
| 19 |
|
| 20 |
is( $form->num_fields, 3, 'right number of fields' ); |
| 21 |
is( scalar @{$form->sorted_fields}, 1, 'right number of sorted fields' ); |
| 22 |
|
| 23 |
throws_ok( sub { $form->field('foo')->render }, |
| 24 |
qr/No result for form field/, 'dies when rendering inactive field' ); |
| 25 |
|
| 26 |
$form->field('foo')->clear_inactive; |
| 27 |
is( scalar @{$form->sorted_fields}, 2, 'right number of sorted fields after clear_inactive' ); |
| 28 |
|
| 29 |
my $fif = { |
| 30 |
bar => 'bar_test', |
| 31 |
foo => 'foo_test', |
| 32 |
}; |
| 33 |
$form->process($fif); |
| 34 |
ok( $form->validated, 'form validated' ); |
| 35 |
is_deeply( $form->fif, $fif, 'fif is correct' ); |
| 36 |
is_deeply( $form->value, $fif, 'value is correct' ); |
| 37 |
|
| 38 |
$form = Test::Form->new; |
| 39 |
my $active = ['foo']; |
| 40 |
$form->process( active => [@{$active}], params => $fif ); |
| 41 |
is_deeply( $active, ['foo'], 'active hashref still there' ); |
| 42 |
ok( $form->validated, 'form validated' ); |
| 43 |
is_deeply( $form->fif, $fif, 'fif is correct' ); |
| 44 |
is_deeply( $form->value, $fif, 'value is correct' ); |
| 45 |
|
| 46 |
|
| 47 |
# tests for setting active fields inactive |
| 48 |
{ |
| 49 |
package Test::Another; |
| 50 |
use HTML::FormHandler::Moose; |
| 51 |
extends 'HTML::FormHandler'; |
| 52 |
|
| 53 |
has_field 'foo'; |
| 54 |
has_field 'bar'; |
| 55 |
has_field 'gaz'; |
| 56 |
} |
| 57 |
|
| 58 |
$form = Test::Another->new( inactive => ['bar'] ); |
| 59 |
ok( $form->field('bar')->is_inactive, 'field is inactive' ); |
| 60 |
ok( $form->field('foo')->is_active, 'field is active' ); |
| 61 |
|
| 62 |
my $html = $form->render; |
| 63 |
unlike($html, qr/input.*name="bar"/, 'inactive field is hidden'); |
| 64 |
like($html, qr/input.*name="foo"/, 'active field is shown'); |
| 65 |
|
| 66 |
$form = Test::Another->new; |
| 67 |
$form->process( inactive => ['gaz'], params => {} ); |
| 68 |
ok( $form->field('gaz')->is_inactive, 'field is inactive' ); |
| 69 |
ok( $form->field('foo')->is_active, 'field is active' ); |
| 70 |
|
| 71 |
$html = $form->render; |
| 72 |
unlike($html, qr/input.*name="gaz"/, 'inactive field is hidden'); |
| 73 |
like($html, qr/input.*name="foo"/, 'active field is shown'); |
| 74 |
|
| 75 |
done_testing; |