Skip to content

Testing widgets

Widgets are plain objects: test canView() with the gate, render() for the HTML and, for charts, config() for the data. TableWidget needs the panel as current panel.

use Anvil\Widget\ChartWidget;
use Anvil\Widget\TableWidget;
protected function setUp(): void
{
parent::setUp();
service('panels')->setCurrent(service('panels')->get('admin'));
}
public function testChartPermission(): void
{
$chart = ChartWidget::make('Pipeline')->permission('deals.view');
$this->grantPermissions([]);
$this->assertFalse($chart->canView(service('gate')));
$this->grantPermissions(['deals.view']);
$this->assertTrue($chart->canView(service('gate')));
}
public function testLatestDealsStayInTheTenant(): void
{
// A deal "Deal of another tenant" was created in another tenant before.
$table = TableWidget::make('Latest deals')->resource(DealResource::class)->columns(['title']);
$this->grantPermissions(['deals.view']);
$this->assertStringNotContainsString('Deal of another tenant', $table->render());
}

grantPermissions() comes from Anvil\Test\InteractsWithAnvil, see Testing.