Skip to content

Commit

Permalink
Fixed bug where overrides did not receive correct values for multiple…
Browse files Browse the repository at this point in the history
… controls with same name (#5)
  • Loading branch information
zoffixznet committed Sep 23, 2015
1 parent 1c7b264 commit 601afea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/Test/Mojo/Role/SubmitForm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sub click_ok {
next unless ref $extra_params->{$_} eq 'CODE';
( my $name = $_ ) =~ s/"/\\"/g;
$extra_params->{$_} = $extra_params->{$_}->(
$el->at(qq{[name="$name"]})->val
$self->_gather_vals( $el->find(qq{[name="$name"]})->each )
);
}

Expand All @@ -52,20 +52,30 @@ sub click_ok {
$self->request_ok( $tx );
}

sub _gather_vals {
my ( $self, @els ) = @_;
my @vals;
for ( @els ) {
next if $_
->matches('[type=radio]:not(:checked), [type=checkbox]:not(:checked)');

defined( my $val = $_->val ) or next;
push @vals, ref $val ? @$val : $val;
}
return @vals == 1 ? $vals[0] : @vals ? \@vals : undef;
}

sub _get_controls {
my ( $self, $form ) = @_;

my @els = $form->find(
'input:not([type=button]):not([type=submit]):not([type=image])'
. ':not([type=checkbox]):not([type=radio]),'
. '[type=checkbox]:checked, [type=radio]:checked,'
. 'select, textarea'
'input:not([type=button]):not([type=submit]):not([type=image]),' . 'select, textarea'
)->each;

my %controls;
for ( @els ) {
defined( my $val = $_->val ) or next;
push @{ $controls{$_->{name}} }, ref $val ? @$val : $val;
my $vals = $self->_gather_vals( $_ ) or next;
push @{ $controls{$_->{name}} }, ref $vals ? @$vals : $vals;
}
$#$_ or $_= $_->[0] for values %controls; # chage 1-el arrayrefs to strings

Expand Down
2 changes: 2 additions & 0 deletions t/01-tester.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ my %form_one = (
'$"bar' => sub { 5 },
'©☺♥' => sub { 55 },
mult_m => [qw/FOO BAR/],
mult_a => sub { my $r = shift; [ 1, 2, 3, @$r ] },
})->status_is(200)->json_is({
%form_one,
a => '42',
Expand All @@ -80,6 +81,7 @@ my %form_one = (
'$"bar' => 5,
'©☺♥' => 55,
mult_m => [qw/FOO BAR/],
mult_a => [1, 2, 3, qw/A B/],
})
}

Expand Down

0 comments on commit 601afea

Please sign in to comment.