Skip to content

Commit

Permalink
Add Tests for Prepend/Append Cols
Browse files Browse the repository at this point in the history
  • Loading branch information
lrljoe committed Aug 31, 2023
1 parent 03ce733 commit ef35e4f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
47 changes: 47 additions & 0 deletions src/Traits/Configuration/ColumnConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

use Illuminate\Support\Collection;
use Rappasoft\LaravelLivewireTables\Views\Column;

trait ColumnConfiguration
{
public function setPrependedColumns(array $prependedColumns): void
{
$this->prependedColumns = collect($prependedColumns)
->filter(fn ($column) => $column instanceof Column)
->map(function (Column $column) {
$column->setComponent($this);

if ($column->hasField()) {
if ($column->isBaseColumn()) {
$column->setTable($this->getBuilder()->getModel()->getTable());
} else {
$column->setTable($this->getTableForColumn($column));
}
}

return $column;
});
}

public function setAppendedColumns(array $appendedColumns): void
{
$this->appendedColumns = collect($appendedColumns)
->filter(fn ($column) => $column instanceof Column)
->map(function (Column $column) {
$column->setComponent($this);

if ($column->hasField()) {
if ($column->isBaseColumn()) {
$column->setTable($this->getBuilder()->getModel()->getTable());
} else {
$column->setTable($this->getTableForColumn($column));
}
}

return $column;
});
}
}
12 changes: 7 additions & 5 deletions src/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ColumnHelpers
*/
public function setColumns(): void
{
$prependedColumns = $this->getPrependedColumns();
$this->prependedColumns = $this->getPrependedColumns();

$columns = collect($this->columns())
->filter(fn ($column) => $column instanceof Column)
Expand All @@ -30,9 +30,9 @@ public function setColumns(): void
return $column;
});

$appendedColumns = $this->getAppendedColumns();
$this->appendedColumns = $this->getAppendedColumns();

$this->columns = collect([...$prependedColumns, ...$columns, ...$appendedColumns]);
$this->columns = collect([...$this->prependedColumns, ...$columns, ...$this->appendedColumns]);
}

public function getColumns(): Collection
Expand Down Expand Up @@ -180,9 +180,10 @@ public function getColspanCount(): int
return 100;
}


public function getPrependedColumns(): Collection
{
return collect($this->prependColumns())
return collect($this->prependedColumns ?? $this->prependColumns())
->filter(fn ($column) => $column instanceof Column)
->map(function (Column $column) {
$column->setComponent($this);
Expand All @@ -201,7 +202,7 @@ public function getPrependedColumns(): Collection

public function getAppendedColumns(): Collection
{
return collect($this->appendColumns())
return collect($this->appendedColumns ?? $this->appendColumns())
->filter(fn ($column) => $column instanceof Column)
->map(function (Column $column) {
$column->setComponent($this);
Expand All @@ -216,5 +217,6 @@ public function getAppendedColumns(): Collection

return $column;
});

}
}
6 changes: 5 additions & 1 deletion src/Traits/WithColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

use Illuminate\Support\Collection;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ColumnHelpers;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\ColumnConfiguration;

trait WithColumns
{
use ColumnHelpers;
use ColumnConfiguration;

protected Collection $columns;
protected Collection $prependedColumns;
protected Collection $appendedColumns;

public function bootWithColumns(): void
{
Expand All @@ -19,7 +23,7 @@ public function bootWithColumns(): void
/**
* Prepend columns.
*/
public function prependColumns(): array
public function prependColumns()
{
return [];
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Traits/Helpers/ColumnHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ public function can_get_column_list(): void
$this->assertCount(9, $this->basicTable->getColumns()->toArray());
}

/** @test */
public function can_append_column(): void
{
$this->assertCount(9, $this->basicTable->getColumns()->toArray());

$this->basicTable->setAppendedColumns([Column::make('IDLabel')->label(function ($row) {
return 'Test';
})]);

$this->basicTable->setColumns();

$this->assertCount(10, $this->basicTable->getColumns()->toArray());

}

/** @test */
public function can_prepend_column(): void
{
$this->assertCount(9, $this->basicTable->getColumns()->toArray());

$this->basicTable->setPrependedColumns([Column::make('IDLabel')->label(function ($row) {
return 'Test';
})]);

$this->basicTable->setColumns();

$this->assertCount(10, $this->basicTable->getColumns()->toArray());
}

/** @test */
public function can_get_column_by_column(): void
{
Expand Down

0 comments on commit ef35e4f

Please sign in to comment.