From ac80e166a4c3f76a6a9c4370b2762898c9560193 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Wed, 29 Oct 2008 21:28:45 +0000 Subject: [PATCH] added testing for DataTable form and asset --- t/Asset/Wobject/DataTable.t | 146 ++++++++++++++++++++++++++++++++++++ t/Form/DataTable.t | 128 +++++++++++++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 t/Asset/Wobject/DataTable.t create mode 100644 t/Form/DataTable.t diff --git a/t/Asset/Wobject/DataTable.t b/t/Asset/Wobject/DataTable.t new file mode 100644 index 000000000..9e8396bcb --- /dev/null +++ b/t/Asset/Wobject/DataTable.t @@ -0,0 +1,146 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# This tests the DataTable asset +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use Test::Deep; +use JSON; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +my $node = WebGUI::Asset->getImportNode( $session ); + +my $dt; + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 1; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# Datatable creation +use_ok( "WebGUI::Asset::Wobject::DataTable" ); +my $dt = $node->addChild( { + className => 'WebGUI::Asset::Wobject::DataTable', +} ); +isa_ok( $dt, 'WebGUI::Asset::Wobject::DataTable' ); + +#---------------------------------------------------------------------------- +# Value and variables +my $value = { + columns => [ + { + key => "Col1", + formatter => "number", + }, + { + key => "Col2", + formatter => "text", + }, + ], + rows => [ + { + Col1 => "1", + Col2 => "two", + }, + { + Col1 => "2", + Col2 => "three", + }, + { + Col1 => "3", + Col2 => "four", + }, + ], +}; + +$dt->update( { + data => JSON->new->encode( $value ), +} ); + +cmp_deeply( + JSON->new->decode( $dt->getDataJson ), + hash( $value ), + "getDataJson returns JSON data structure", +); + +cmp_deeply( + JSON->new->decode( $dt->www_ajaxGetData ), + hash( $value ), + "www_ajaxGetData returns JSON data structure", +); + +#---------------------------------------------------------------------------- +# getTemplateVars and getDataTemplateVars +cmp_deeply( + $dt->getDataTemplateVars, + hash( { + columns => [ + subhashof( { key => "Col1", formatter => "number" } ), + subhashof( { key => "Col2", formatter => "text" } ), + ], + rows => [ + { + row_columns => [ + subhashof( { key => "Col1", formatter => "number", value => 1 } ), + subhashof( { key => "Col2", formatter => "text", value => "two" } ), + ], + Col1 => "1", + Col2 => "two", + }, + { + row_columns => [ + subhashof( { key => "Col1", formatter => "number", value => 2 } ), + subhashof( { key => "Col2", formatter => "text", value => "three" } ), + ], + Col1 => "2", + Col2 => "three", + }, + { + row_columns => [ + subhashof( { key => "Col1", formatter => "number", value => 3 } ), + subhashof( { key => "Col2", formatter => "text", value => "four" } ), + ], + Col1 => "3", + Col2 => "four", + }, + ], + } ), + "getDataTemplateVars returns complete and correct data structure", +); + +cmp_deeply( + $dt->getTemplateVars, + hash( { + %{ $dt->getTemplateVars }, + %{ $dt->get }, + url => $dt->getUrl, + dataJson => $dt->getDataJson, + dataTable => $dt->getDataTable, + } ), + "getTemplateVars returns complete and correct data structure", +); + +#---------------------------------------------------------------------------- +# Cleanup +END { + $dt->purge; +} +#vim:ft=perl diff --git a/t/Form/DataTable.t b/t/Form/DataTable.t new file mode 100644 index 000000000..88aa5c4f5 --- /dev/null +++ b/t/Form/DataTable.t @@ -0,0 +1,128 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Test the API of the DataTable form control +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +use Test::More; +use Test::Deep; +use JSON; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 11; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# DataTable creation +use_ok( "WebGUI::Form::DataTable" ); +my $dt = WebGUI::Form::DataTable->new( $session, { } ); +isa_ok( $dt, 'WebGUI::Form::DataTable' ); + +#---------------------------------------------------------------------------- +# Default value +cmp_deeply( + JSON->new->decode( $dt->getDefaultValue ), + { + columns + => bag( { + key => "New Column", + formatter => "text", + } ), + rows + => bag( { + "New Column" => "Value", + } ), + }, + "Default value contains proper data structure and dummy data", +); + +#---------------------------------------------------------------------------- +# getValue +my $value = { + columns => [ + { + key => "Col1", + formatter => "number", + }, + { + key => "Col2", + formatter => "text", + }, + ], + rows => [ + { + Col1 => "1", + Col2 => "two", + }, + { + Col1 => "2", + Col2 => "three", + }, + { + Col1 => "3", + Col2 => "four", + }, + ], +}; + +# Passing in JSON +is( ref $dt->getValue( $value ), '', 'getValue( $value ) returns scalar' ); +cmp_deeply( + JSON->new->decode( $dt->getValue( $value ) ), + hash( $value ), + 'getValue( $value ) returns proper JSON data structure', +); + +#---------------------------------------------------------------------------- +# getOriginalValue +# Set at beginning +$dt = WebGUI::Form::DataTable->new( $session, { + name => "test", + value => JSON->new->encode( $value ), + id => "test", +} ); +cmp_deeply( + JSON->new->decode( $dt->getOriginalValue ), + hash( $value ), + 'getOriginalValue returns proper JSON data structure', +); + +#---------------------------------------------------------------------------- +# getTableHtml +my $html = $dt->getTableHtml( JSON->new->decode( $dt->getOriginalValue ) ); +like( $html, qr{^]*class="yui-skin-sam"}, "getTableHtml contains a div with skin class" ); +like( $html, qr{^]*id="test-container"}, "getTableHtml div has ID" ); +like( $html, qr{}, "getTableHtml contains a table" ); +like( $html, qr{]*id="test-container-table"}, "getTableHtml table has ID" ); + +#---------------------------------------------------------------------------- +# toHtml +my $html = $dt->toHtml; +ok( $dt->get( "showEdit" ), "showEdit gets set by toHtml" ); + +#---------------------------------------------------------------------------- +# Cleanup +END { + +} +#vim:ft=perl