Last active
August 29, 2016 17:51
-
-
Save jberger/b0132b3d7641c3d7c6e4 to your computer and use it in GitHub Desktop.
Tests for the websocket.pl application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test::More; | |
use Test::Mojo; | |
use Mojo::DOM; | |
use Mojo::JSON 'j'; | |
# point to a test db and load the app | |
$ENV{MOJO_DBNAME} = 'test.db'; | |
require './websocket.pl'; | |
my $t = Test::Mojo->new; | |
$t->app->empty_table; | |
subtest 'Test initial state' => sub { | |
$t->get_ok('/') | |
->status_is(200) | |
->text_is('table thead th:nth-child(1)' => 'Name') | |
->text_is('table thead th:nth-child(2)' => 'Age') | |
->element_exists('#table', 'table exists') | |
->element_exists_not('#table tr', 'table has no rows'); | |
}; | |
subtest 'Test adding a row' => sub { | |
$t->websocket_ok('/insert') | |
->send_ok({ json => [ Joel => 30 ] }) | |
->message_ok | |
->json_message_has('/row') | |
->finish_ok; | |
my $html = j($t->message->[1])->{row}; | |
my $row = Mojo::DOM->new($html)->find('tr td')->text; | |
is $row->[0], 'Joel', 'get td name via websocket'; | |
is $row->[1], 30, 'get td age via websocket'; | |
$t->get_ok('/') | |
->status_is(200) | |
->text_is('#table tr td:nth-child(1)' => 'Joel') | |
->text_is('#table tr td:nth-child(2)' => '30'); | |
}; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment