F
Filament9mo ago
Lucky0

UnitTest (bug)?

I am wondering why the code below only works for sorting 'name' but not 'email'.
use App\Filament\Admin\Resources\UserResource;
use App\Models\User;
use function Pest\Livewire\livewire;

describe(
'can sort users by: ',
fn() => collect([
'name',
'email'
])->each(function ($column) {
test($column, function () use ($column) {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
});
})
);
use App\Filament\Admin\Resources\UserResource;
use App\Models\User;
use function Pest\Livewire\livewire;

describe(
'can sort users by: ',
fn() => collect([
'name',
'email'
])->each(function ($column) {
test($column, function () use ($column) {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
});
})
);
UserResource:
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('email')
->sortable()
->searchable(),
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('email')
->sortable()
->searchable(),
30 Replies
dissto
dissto9mo ago
My guess would be that the closure is only run once and therefor only for the first item of the collection? Perhaps you could retry your test with using datasets. Something like that maybe:
test('can sort users by:', function($column){
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
})->with(['name', 'email']);
test('can sort users by:', function($column){
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
})->with(['name', 'email']);
Lucky0
Lucky0OP9mo ago
will try again.. thank you I've also tried using the example given in the filament docs.. and it returned as failed.
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->assertCanRenderTableColumn($users)
->sortTable('email')
->assertCanSeeTableRecords($users->sortBy('email'), inOrder: true)
->sortTable('email', 'desc')
->assertCanSeeTableRecords($users->sortByDesc('email'), inOrder: true);
});
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->assertCanRenderTableColumn($users)
->sortTable('email')
->assertCanSeeTableRecords($users->sortBy('email'), inOrder: true)
->sortTable('email', 'desc')
->assertCanSeeTableRecords($users->sortByDesc('email'), inOrder: true);
});
Tests\Feature\Admin\UserResourceTest > it can sort users by email
Failed asserting that a table column with name [[{"id":153,"name":"superadmin","email":"[email protected]","email_verified_at":"2024-03-24T09:04:27.000000Z","created_at":"2024-03-24T09:04:27.000000Z","updated_at":"2024-03-24T09:04:27.000000Z","deleted_at":null,"avatar_url":null,"breezy_sessions":[]}]] exists on the [App\Filament\Admin\Resources\UserResource\Pages\ListUsers] component.
Failed asserting that null is an instance of class Filament\Tables\Columns\Column.

at vendor\filament\tables\src\Testing\TestsColumns.php:70
66▕ $column = $this->instance()->getTable()->getColumn($name);
67▕
68▕ $livewireClass = $this->instance()::class;
69▕
➜ 70▕ Assert::assertInstanceOf(
71▕ Column::class,
72▕ $column,
73▕ message: "Failed asserting that a table column with name [{$name}] exists on the [{$livewireClass}] component.",
74▕ );

1 vendor\filament\tables\src\Testing\TestsColumns.php:70
2 vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:123
Tests\Feature\Admin\UserResourceTest > it can sort users by email
Failed asserting that a table column with name [[{"id":153,"name":"superadmin","email":"[email protected]","email_verified_at":"2024-03-24T09:04:27.000000Z","created_at":"2024-03-24T09:04:27.000000Z","updated_at":"2024-03-24T09:04:27.000000Z","deleted_at":null,"avatar_url":null,"breezy_sessions":[]}]] exists on the [App\Filament\Admin\Resources\UserResource\Pages\ListUsers] component.
Failed asserting that null is an instance of class Filament\Tables\Columns\Column.

at vendor\filament\tables\src\Testing\TestsColumns.php:70
66▕ $column = $this->instance()->getTable()->getColumn($name);
67▕
68▕ $livewireClass = $this->instance()::class;
69▕
➜ 70▕ Assert::assertInstanceOf(
71▕ Column::class,
72▕ $column,
73▕ message: "Failed asserting that a table column with name [{$name}] exists on the [{$livewireClass}] component.",
74▕ );

1 vendor\filament\tables\src\Testing\TestsColumns.php:70
2 vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:123
dissto
dissto9mo ago
Do you have more than 10 users? Usually, tests are supposed to be run with clean data coming from a factory. Also using a different database then your apps database.
Lucky0
Lucky0OP9mo ago
i make sure to have 10 users, so yes. i reduced the text in the error messages because of discord text limit. i do test it on different database. i have tried this and it still returning as failed
Tests\Feature\Admin\UserResourceTest > can sort users by: with ('email')
Failed asserting that Failed asserting that '<div wire:id="tTaQgARQX1UFIZS4ldx4"
class="fi-page fi-resource-list-records-page fi-resource-users"
....
Tests\Feature\Admin\UserResourceTest > can sort users by: with ('email')
Failed asserting that Failed asserting that '<div wire:id="tTaQgARQX1UFIZS4ldx4"
class="fi-page fi-resource-list-records-page fi-resource-users"
....
dissto
dissto9mo ago
Can you show the complete error? Do you have any setup method? Like the ->beforeEach etc? You are testing locally, right?
Lucky0
Lucky0OP9mo ago
the error is too long.. and this is my setup.. yes im testing it locally.
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;

public $ownerUser;
public $adminUser;
public $superAdmin;

protected function setUp(): void
{
parent::setUp();

seed();

$this->superAdmin = User::factory()->create([
"name" => "superadmin2",
"email" => "[email protected]",
"password" => bcrypt("password"),
]);

$this->superAdmin->assignRole(config('filament-shield.super_admin.name'));

$this->actingAs($this->superAdmin);
}
}
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;

public $ownerUser;
public $adminUser;
public $superAdmin;

protected function setUp(): void
{
parent::setUp();

seed();

$this->superAdmin = User::factory()->create([
"name" => "superadmin2",
"email" => "[email protected]",
"password" => bcrypt("password"),
]);

$this->superAdmin->assignRole(config('filament-shield.super_admin.name'));

$this->actingAs($this->superAdmin);
}
}
dissto
dissto9mo ago
Can you for testing purpose assertOk on livewire(UserResource\Pages\ListUsers::class)
livewire(UserResource\Pages\ListUsers::class)
->assertOk();
livewire(UserResource\Pages\ListUsers::class)
->assertOk();
Only run this for now
Lucky0
Lucky0OP9mo ago
PASS Tests\Feature\Admin\UserResourceTest
✓ livewire 'App\Filament\Admin\Resources\…tUsers' → assertOk 1.92s
Tests: 1 passed (1 assertions)
Duration: 2.06s
PASS Tests\Feature\Admin\UserResourceTest
✓ livewire 'App\Filament\Admin\Resources\…tUsers' → assertOk 1.92s
Tests: 1 passed (1 assertions)
Duration: 2.06s
dissto
dissto9mo ago
Ok can you run your previous test line by line and see where it fails. Like:
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->assertCanRenderTableColumn($users);
});
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->assertCanRenderTableColumn($users);
});
etc Can you run this test exactly like that:
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable('email')
->assertCanSeeTableRecords($users->sortBy('email'), inOrder: true)
->sortTable('email', 'desc')
->assertCanSeeTableRecords($users->sortByDesc('email'), inOrder: true);
});
it('can sort users by email', function () {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable('email')
->assertCanSeeTableRecords($users->sortBy('email'), inOrder: true)
->sortTable('email', 'desc')
->assertCanSeeTableRecords($users->sortByDesc('email'), inOrder: true);
});
->assertCanRenderTableColumn() you are passing a user instance as opposed to a column name. Didn't notice before 😁
Lucky0
Lucky0OP9mo ago
ohh.. i see.. but even removing that the test is still failing
<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->

<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->



<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->
</div>
' contains "LM3NVQysOUEstOReJN1Z.table.records.1" in specified order..

at vendor\livewire\livewire\src\Features\SupportTesting\MakesAssertions.php:49
45▕ }
46▕
47▕ function assertSeeHtmlInOrder($values)
48▕ {
➜ 49▕ PHPUnit::assertThat(
50▕ $values,
51▕ new SeeInOrder($this->html())
52▕ );
53▕

1 vendor\livewire\livewire\src\Features\SupportTesting\MakesAssertions.php:49
2 vendor\filament\tables\src\Testing\TestsRecords.php:34


Tests: 1 failed (1 assertions)
Duration: 2.57s
<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->

<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->



<!--[if BLOCK]><![endif]--><!--[if ENDBLOCK]><![endif]-->
</div>
' contains "LM3NVQysOUEstOReJN1Z.table.records.1" in specified order..

at vendor\livewire\livewire\src\Features\SupportTesting\MakesAssertions.php:49
45▕ }
46▕
47▕ function assertSeeHtmlInOrder($values)
48▕ {
➜ 49▕ PHPUnit::assertThat(
50▕ $values,
51▕ new SeeInOrder($this->html())
52▕ );
53▕

1 vendor\livewire\livewire\src\Features\SupportTesting\MakesAssertions.php:49
2 vendor\filament\tables\src\Testing\TestsRecords.php:34


Tests: 1 failed (1 assertions)
Duration: 2.57s
dissto
dissto9mo ago
And $users is not more than 10 users?
Lucky0
Lucky0OP9mo ago
can a plugins also influence these test? yep not more than 10 users. because the table can only render 10 users per page.
dissto
dissto9mo ago
In your resource do you have a defaultSort or something?
Lucky0
Lucky0OP9mo ago
if i try it with 'name', it does not return as failed.
Lucky0
Lucky0OP9mo ago
this is my UserResource.
dissto
dissto9mo ago
It passes for name but not for email ?
Lucky0
Lucky0OP9mo ago
yes
dissto
dissto9mo ago
Im having troubles reproducing your error ☹️
No description
dissto
dissto9mo ago
Do you do something out of the ordinary in the ListUsers class?
Lucky0
Lucky0OP9mo ago
I dont think I meddle with the ListUsers class yet.
<?php

namespace App\Filament\Admin\Resources\UserResource\Pages;

use App\Filament\Admin\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
<?php

namespace App\Filament\Admin\Resources\UserResource\Pages;

use App\Filament\Admin\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
dissto
dissto9mo ago
Please confirm that $users is not more than 10. The only time i can reproduce anything remotely close is with more than 10 users
dd($users->count()); // in the test
dd($users->count()); // in the test
Lucky0
Lucky0OP9mo ago
9 // tests\Feature\Admin\UserResourceTest.php:107
dissto
dissto9mo ago
I assume if you remove both inOrder: true it passes? Have you manually tested the sorting? Can you sort both by hand?
Lucky0
Lucky0OP9mo ago
you are correct about removing the
inOrder:true
inOrder:true
. it passes yes, i did manually sorting and it is working fine.
dissto
dissto9mo ago
it('can sort users by email', function ($column) {

$users = $this->club->users;

livewire(ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true);

})->with(['id', 'name', 'email', 'email_verified_at', 'created_at', 'updated_at']);
it('can sort users by email', function ($column) {

$users = $this->club->users;

livewire(ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true);

})->with(['id', 'name', 'email', 'email_verified_at', 'created_at', 'updated_at']);
Im at the same point now. It works for all but name and email 🤣 I am at my wits end now, sorry 🤔
Lucky0
Lucky0OP9mo ago
its okay and thank you..
dissto
dissto9mo ago
Do you have a custom getter or setter for the user's name in your User class?
Lucky0
Lucky0OP9mo ago
in my model?
dissto
dissto9mo ago
Yes
Lucky0
Lucky0OP9mo ago
i dont think i have also i just do it like this. just adding condition to the inOrder. cause i figures that if one column is inOrder then the other is not, thus that what trigger the error.
test(
'can sort users ',
function ($column) {
$users = User::get();
livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: $column === 'name' ? true : false)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: $column === 'name' ? true : false);
}
)->with(['name', 'email']);
test(
'can sort users ',
function ($column) {
$users = User::get();
livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: $column === 'name' ? true : false)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: $column === 'name' ? true : false);
}
)->with(['name', 'email']);
Want results from more Discord servers?
Add your server