Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Mojo::DOM with Test::Mojo #1046

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/Test/Mojo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
use Test::More ();

has dom => sub { shift->tx->res->dom };
has [qw(message success tx)];
has ua => sub { Mojo::UserAgent->new->ioloop(Mojo::IOLoop->singleton) };

Expand Down Expand Up @@ -85,21 +86,21 @@ sub delete_ok { shift->_build_ok(DELETE => @_) }

sub element_count_is {
my ($self, $selector, $count, $desc) = @_;
my $size = $self->tx->res->dom->find($selector)->size;
my $size = $self->dom->find($selector)->size;
return $self->_test('is', $size, $count,
_desc($desc, qq{element count for selector "$selector"}));
}

sub element_exists {
my ($self, $selector, $desc) = @_;
$desc = _desc($desc, qq{element for selector "$selector" exists});
return $self->_test('ok', $self->tx->res->dom->at($selector), $desc);
return $self->_test('ok', $self->dom->at($selector), $desc);
}

sub element_exists_not {
my ($self, $selector, $desc) = @_;
$desc = _desc($desc, qq{no element for selector "$selector"});
return $self->_test('ok', !$self->tx->res->dom->at($selector), $desc);
return $self->_test('ok', !$self->dom->at($selector), $desc);
}

sub finish_ok {
Expand Down Expand Up @@ -371,6 +372,7 @@ sub _request_ok {
my $desc = _desc("WebSocket handshake with $url");
return $self->_test('ok', $self->tx->is_websocket, $desc);
}
delete $self->{dom};

# Perform request
$self->tx($self->ua->start($tx));
Expand All @@ -387,7 +389,7 @@ sub _test {
}

sub _text {
return '' unless my $e = shift->tx->res->dom->at(shift);
return '' unless my $e = shift->dom->at(shift);
return $e->text;
}

Expand Down Expand Up @@ -450,6 +452,13 @@ C<HARNESS_IS_VERBOSE> environment variable.

L<Test::Mojo> implements the following attributes.

=head2 dom

my $dom = $t->dom;
$t = $t->dom(Mojo::DOM->new);

Current L<Mojo::DOM> object. Defaults to DOM from the current transaction.

=head2 message

my $msg = $t->message;
Expand Down
21 changes: 21 additions & 0 deletions t/mojo/test.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

use Mojo::Base -strict;

BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}
use Mojolicious::Lite;

get('/' => sub { shift->render(text => '<b>wrong</b>') });

use Test::More;
use Test::Mojo;

my $t = Test::Mojo->new();
isa_ok($t, 'Test::Mojo', 'right class');
can_ok($t, qw/tx ua dom/);
$t->dom(Mojo::DOM->new('<b>right</b>'));
$t->text_is('b', 'right', 'dom can be set');
$t->get_ok('/')->text_is('b', 'wrong', 'dom reset on request');

done_testing;