Overview
All examples in this guide will be written using Pest. To use Pest’s Livewire plugin for testing, you can follow the installation instructions in the Pest documentation on plugins: Livewire plugin for Pest. However, you can easily adapt this to PHPUnit. Since the Table Builder works on Livewire components, you can use the Livewire testing helpers. However, we have many custom testing helpers that you can use for tables:Render
To ensure a table component renders, use theassertSuccessful() Livewire helper:
assertCanSeeTableRecords(), assertCanNotSeeTableRecords() and assertCountTableRecords():
If your table uses pagination,assertCanSeeTableRecords()will only check for records on the first page. To switch page, callcall('gotoPage', 2).
If your table usesdeferLoading(), you should callloadTable()beforeassertCanSeeTableRecords().
Columns
To ensure that a certain column is rendered, pass the column name toassertCanRenderTableColumn():
assertCanNotRenderTableColumn():
Sorting
To sort table records, you can callsortTable(), passing the name of the column to sort by. You can use 'desc' in the second parameter of sortTable() to reverse the sorting direction.
Once the table is sorted, you can ensure that the table records are rendered in order using assertCanSeeTableRecords() with the inOrder parameter:
Searching
To search the table, call thesearchTable() method with your search query.
You can then use assertCanSeeTableRecords() to check your filtered table records, and use assertCanNotSeeTableRecords() to assert that some records are no longer in the table:
searchTableColumns():
State
To assert that a certain column has a state or does not have a state for a record you can useassertTableColumnStateSet() and assertTableColumnStateNotSet():
assertTableColumnFormattedStateSet() and assertTableColumnFormattedStateNotSet():
Existence
To ensure that a column exists, you can use theassertTableColumnExists() method:
Authorization
To ensure that a particular user cannot see a column, you can use theassertTableColumnVisible() and assertTableColumnHidden() methods:
Descriptions
To ensure a column has the correct description above or below you can use theassertTableColumnHasDescription() and assertTableColumnDoesNotHaveDescription() methods:
Extra Attributes
To ensure that a column has the correct extra attributes, you can use theassertTableColumnHasExtraAttributes() and assertTableColumnDoesNotHaveExtraAttributes() methods:
Select Columns
If you have a select column, you can ensure it has the correct options withassertTableSelectColumnHasOptions() and assertTableSelectColumnDoesNotHaveOptions():
Filters
To filter the table records, you can use thefilterTable() method, along with assertCanSeeTableRecords() and assertCanNotSeeTableRecords():
SelectFilter or TernaryFilter, pass the value as a second argument:
Resetting filters
To reset all filters to their original state, callresetTableFilters():
Removing Filters
To remove a single filter you can useremoveTableFilter():
removeTableFilters():
Hidden filters
To ensure that a particular user cannot see a filter, you can use theassertTableFilterVisible() and assertTableFilterHidden() methods:
Filter existence
To ensure that a filter exists, you can use theassertTableFilterExists() method:
Actions
Calling actions
You can call an action by passing its name or class tocallTableAction():
DeleteAction on your table. If you have a custom Action::make('reorder'), you may use callTableAction('reorder').
For column actions, you may do the same, using callTableColumnAction():
callTableBulkAction():
data parameter:
Execution
To check if an action or bulk action has been halted, you can useassertTableActionHalted() / assertTableBulkActionHalted():
Errors
assertHasNoTableActionErrors() is used to assert that no validation errors occurred when submitting the action form.
To check if a validation error has occurred with the data, use assertHasTableActionErrors(), similar to assertHasErrors() in Livewire:
assertHasTableBulkActionErrors() and assertHasNoTableBulkActionErrors().
Pre-filled data
To check if an action or bulk action is pre-filled with data, you can use theassertTableActionDataSet() or assertTableBulkActionDataSet() method:
assertTableActionDataSet() and assertTableBulkActionDataSet() methods, which allow you to access the form $state and perform additional assertions:
Action state
To ensure that an action or bulk action exists or doesn’t in a table, you can use theassertTableActionExists() / assertTableActionDoesNotExist() or assertTableBulkActionExists() / assertTableBulkActionDoesNotExist() method:
assertTableActionEnabled() / assertTableActionDisabled() or assertTableBulkActionEnabled() / assertTableBulkActionDisabled() methods:
assertTableActionVisible() / assertTableActionHidden() or assertTableBulkActionVisible() / assertTableBulkActionHidden() methods:
Button Style
To ensure an action or bulk action has the correct label, you can useassertTableActionHasLabel() / assertTableBulkActionHasLabel() and assertTableActionDoesNotHaveLabel() / assertTableBulkActionDoesNotHaveLabel():
assertTableActionHasIcon() / assertTableBulkActionHasIcon() or assertTableActionDoesNotHaveIcon() / assertTableBulkActionDoesNotHaveIcon():
assertTableActionHasColor() / assertTableBulkActionHasColor() or assertTableActionDoesNotHaveColor() / assertTableBulkActionDoesNotHaveColor():
URL
To ensure an action or bulk action has the correct URL traits, you can useassertTableActionHasUrl(), assertTableActionDoesNotHaveUrl(), assertTableActionShouldOpenUrlInNewTab(), and assertTableActionShouldNotOpenUrlInNewTab():
Summaries
To test that a summary calculation is working, you may use theassertTableColumnSummarySet() method:
123.12 is considered the same as "123.12", and ['Fred', 'Jim'] is the same as ['Jim', 'Fred'].
You may set a summarizer ID by passing it to the make() method:
Summarizing only one pagination page
To calculate the average for only one pagination page, use theisCurrentPaginationPageOnly argument:
Testing a range summarizer
To test a range, pass the minimum and maximum value into a tuple-style[$minimum, $maximum] array: