Commit | Line | Data |
7adfd53f |
1 | package RTest::UI::Window; |
2 | |
3 | use aliased 'Reaction::UI::ViewPort'; |
4 | |
5 | use base qw/Reaction::Test/; |
6 | use Reaction::Class; |
7 | |
8 | BEGIN { |
9 | |
10 | package RTest::UI::Window::_::view; |
11 | |
12 | use base qw/Reaction::UI::Renderer::XHTML/; |
13 | |
14 | sub render { |
15 | return $_[0]->{render}->(@_); |
16 | } |
17 | |
18 | package RTest::UI::Window::_::TestViewPort; |
19 | |
20 | use Reaction::Class; |
21 | |
22 | extends 'Reaction::UI::ViewPort'; |
23 | |
24 | register_inc_entry; |
25 | |
26 | sub handle_events { |
27 | $_[0]->{handle_events}->(@_); |
28 | } |
29 | |
30 | }; |
31 | |
32 | use Test::More (); |
33 | use Reaction::UI::Window; |
34 | use aliased 'RTest::UI::Window::_::TestViewPort'; |
35 | |
c4481e73 |
36 | has 'window' => (isa => 'Reaction::UI::Window', is => 'rw', lazy_build => 1); |
7adfd53f |
37 | |
c4481e73 |
38 | sub _build_window { |
7adfd53f |
39 | my $self = shift; |
40 | return Reaction::UI::Window->new( |
41 | ctx => bless({}, 'Reaction::Test::Mock::Context'), |
42 | view_name => 'Test', |
43 | content_type => 'text/html', |
44 | ); |
45 | } |
46 | |
47 | sub test_window :Tests { |
48 | my $self = shift; |
49 | my $window = $self->build_window; |
50 | my $view = bless({}, 'RTest::UI::Window::_::view'); |
51 | $window->ctx->{view} = sub { |
52 | Test::More::is($_[1], 'Test', 'View name ok'); |
53 | return $view; |
54 | }; |
55 | Test::More::is($window->view, $view, 'View retrieved from context'); |
56 | my %param; |
57 | $window->ctx->{req} = sub { |
58 | return bless({ |
59 | query_parameters => sub { \%param }, |
60 | body_parameters => sub { {} }, |
61 | }, 'Reaction::Test::Mock::Request'); |
62 | }; |
63 | $window->ctx->{res} = sub { |
64 | return bless({ |
65 | status => sub { 200 }, |
66 | body => sub { '' }, |
67 | }, 'Reaction::Test::Mock::Response'); |
68 | }; |
69 | eval { $window->flush }; |
70 | Test::More::like($@, qr/empty focus stack/, 'Error thrown without viewports'); |
71 | my @vp; |
72 | push(@vp, $window->focus_stack |
73 | ->push_viewport(ViewPort, ctx => $window->ctx)); |
74 | push(@vp, $window->focus_stack |
75 | ->push_viewport(ViewPort, ctx => $window->ctx)); |
76 | my $i; |
77 | $view->{render} = sub { |
78 | my $expect_vp = $vp[$i++]; |
79 | Test::More::is($_[1], $window->ctx, 'Context ok'); |
80 | Test::More::is($_[2], 'component', 'Component template'); |
81 | Test::More::is($_[3]->{self}, $expect_vp, 'Viewport'); |
82 | $_[3]->{window}->render_viewport($expect_vp->inner); |
83 | return "foo"; |
84 | }; |
85 | my $body; |
86 | $window->ctx->{res} = sub { |
87 | return bless({ |
88 | body => sub { shift; return '' unless @_; $body = shift; }, |
89 | content_type => sub { }, |
90 | status => sub { 200 }, |
91 | }, 'Reaction::Test::Mock::Response'); |
92 | }; |
93 | $window->flush; |
94 | Test::More::is($body, 'foo', 'body set ok'); |
95 | my $test_vp = $vp[1]->create_tangent('foo') |
96 | ->push_viewport(TestViewPort, |
97 | ctx => bless({}, 'Catalyst')); |
98 | my $param_name = '1.foo.0:name'; |
99 | Test::More::is($test_vp->event_id_for('name'), $param_name, 'Event id ok'); |
100 | $param{$param_name} = 'blah'; |
101 | $test_vp->{handle_events} = sub { |
102 | Test::More::is($_[1]->{name}, 'blah', 'Event delivered ok'); |
103 | }; |
104 | $window->flush_events; |
105 | } |
106 | |
107 | 1; |