PageRenderTime 47ms CodeModel.GetById 15ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/t/wizard.t

http://github.com/gshank/html-formhandler
text | 70 lines | 55 code | 15 blank | 0 comment | 0 complexity | 6ecd4ca973924c2cd6fdd93fc65dbee0 MD5 | raw file
 1use strict;
 2use warnings;
 3use Test::More;
 4
 5use_ok( 'HTML::FormHandler::Wizard' );
 6
 7{
 8    package Test::Wizard;
 9    use HTML::FormHandler::Moose;
10    extends 'HTML::FormHandler::Wizard';
11
12    has_field 'foo';
13    has_field 'bar';
14    has_field 'zed';
15
16    has_page 'one' => ( fields => ['foo'] );
17    has_page 'two' => ( fields => ['bar'] );
18    has_page 'three' => ( fields => ['zed'] );
19}
20
21my $wizard = Test::Wizard->new;
22ok( $wizard, 'wizard built ok' );
23is( $wizard->num_pages, 3, 'right number of pages' );
24ok( $wizard->page('one')->has_fields, 'first page has a field' );
25is( $wizard->page('one')->field('foo')->name, 'foo', 'field object from page' );
26
27{
28    package Test::Wizard::List;
29    use HTML::FormHandler::Moose;
30    extends 'HTML::FormHandler::Wizard';
31
32    has_field 'foo';
33    has_field 'bar';
34    has_field 'zed';
35
36    sub page_list { [
37        one => { fields => ['foo'] },
38        two => { fields => ['bar'] },
39        three => { fields => ['zed'] }
40    ]}
41
42}
43
44my $stash = {};
45$wizard = Test::Wizard::List->new( stash => $stash );
46ok( $wizard, 'wizard built ok' );
47is( $wizard->num_pages, 3, 'right number of pages' );
48ok( $wizard->page('one')->has_fields, 'first page has a field' );
49is( $wizard->page('one')->field('foo')->name, 'foo', 'field object from page' );
50
51$wizard->process( params => {} );
52like( $wizard->render, qr/\<input type="hidden" name="page_num" id="page_num" value="1" \/\>/, 'renders ok' );
53is( $wizard->field('page_num')->value, 1, 'wizard is on first page' );
54
55$wizard->process( params => { foo => 'test123', page_num => 1 } );
56is( $wizard->field('page_num')->value, 2, 'wizard is on second page' );
57like( $wizard->render, qr/\<input type="hidden" name="page_num" id="page_num" value="2" \/\>/, 'renders ok' );
58is_deeply( $stash, { foo => 'test123', page_num => 1 }, 'values saved' );
59
60$wizard->process( params => { bar => 'xxxxx', page_num => 2 } );
61is( $wizard->field('page_num')->value, 3, 'wizard is on third page' );
62like( $wizard->render, qr/\<input type="hidden" name="page_num" id="page_num" value="3" \/\>/, 'renders ok' );
63is_deeply( $stash, { foo => 'test123', page_num => 2, bar => 'xxxxx' }, 'values saved' );
64
65$wizard->process( params => { zed => 'omega', page_num => 3 } );
66ok( $wizard->validated, 'wizard validated on last page' );
67is_deeply( $stash, { foo => 'test123', page_num => 3, bar => 'xxxxx', zed => 'omega' }, 'values saved' );
68is_deeply( $wizard->value, { foo => 'test123', page_num => 3, bar => 'xxxxx', zed => 'omega' }, 'value is correct' );
69
70done_testing;