NAME
Test::Mojo::Role::Moai - Test::Mojo role to test UI components
VERSION
version 0.013
SYNOPSIS
my $t = Test::Mojo->with_roles( '+Moai' )->new;
$t->get_ok( '/' )
->table_is(
'#mytable',
[
[ '1BDI' => 'Turanga Leela' ],
[ 'TJAM' => 'URL' ],
],
'NNY officers are listed',
);
DESCRIPTION
This module provides component tests for web pages: Instead of selecting individual elements and testing their parts, these methods test complete components in a way that allows for cosmetic, non-material changes to be made without editing the test.
These methods are designed for Mojolicious::Plugin::Moai components, but do not require Mojolicious::Plugin::Moai to function. Use them on any web site you want!
METHODS
table_is
# <table>
# <thead><tr><th>ID</th><th>Name</th></tr></thead>
# <tbody><tr><td>1</td><td>Doug</td></tr></tbody>
# </table>
$t = $t->table_is( '#mytable', [ [ 1, 'Doug' ] ] );
$t = $t->table_is( '#mytable', [ [ 1, 'Doug' ] ], 'user table' );
$t = $t->table_is( '#mytable', [ { ID => 1, Name => 'Doug' } ] );
Check data in a table is complete and correct. Data can be tested as arrays (ordered) or hashes (unordered).
If a table contains a <tbody>
element, this method will test the data inside. If not, it will test all rows in the table.
# <table><tr><td>1</td><td>Doug</td></tr></table>
$t = $t->table_is( '#mytable', [ [ 1, 'Doug' ] ] );
To test attributes and elements inside the table cells, values can be hashrefs with a text
attribute (for the cell text), an elem
attribute to test descendant elements, and other keys for the cell's attributes.
# <table><tr>
# <td class="center">1</td>
# <td><a href="/user/doug">Doug</a> <em>(admin)</em></td>
# </tr></table>
$t = $t->table_is( '#mytable', [
[
{ text => 1, class => 'center' },
{ elem => {
'a' => {
text => 'Doug',
href => '/user/doug',
},
'em' => '(admin)',
} },
],
] );
table_has
# <table>
# <thead><tr><th>ID</th><th>Name</th></tr></thead>
# <tbody><tr><td>1</td><td>Doug</td></tr></tbody>
# </table>
$t = $t->table_has( '#mytable', [ { ID => 1, Name => 'Doug' } ] );
$t = $t->table_has( '#mytable', [ { Name => 'Doug' } ] );
Check a subset of rows/columns of data in a table.
TODO
- qr// for text
-
Element text and attribute values should allow regex matching in addition to complete equality.
- list_is / list_has
-
Test a list
- dict_is / dict_has
-
Test a dictionary list
- elem_is / elem_has
-
Test an individual element (using "at" in Mojo::DOM).
- all_elem_is / all_elem_has
-
Test a collection of elements (using "find" in Mojo::DOM).
- link_to named route
-
Elements should be able to test whether they are a link to a named route with certain stash values set. This allows for the route's URL to change without needing to change the test.
- Methods for Moai components
-
Any Moai components that have special effects or contain multiple testable elements should be given their own method, with
_is
and_has
variants.
SEE ALSO
Mojolicious::Plugin::Moai, Mojolicious::Guides::Testing
AUTHOR
Doug Bell <preaction@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2019 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.