F
Filament3mo 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
dissto3mo 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
Lucky03mo 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":"super@admin.com","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":"super@admin.com","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
dissto3mo 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
Lucky03mo 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
dissto3mo ago
Can you show the complete error? Do you have any setup method? Like the ->beforeEach etc? You are testing locally, right?
Lucky0
Lucky03mo 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" => "super2@admin.com",
"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" => "super2@admin.com",
"password" => bcrypt("password"),
]);

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

$this->actingAs($this->superAdmin);
}
}
dissto
dissto3mo 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
Lucky03mo 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
dissto3mo 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
Lucky03mo 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
dissto3mo ago
And $users is not more than 10 users?
Lucky0
Lucky03mo 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
dissto3mo ago
In your resource do you have a defaultSort or something?
Lucky0
Lucky03mo ago
if i try it with 'name', it does not return as failed.
Lucky0
Lucky03mo ago
this is my UserResource.
dissto
dissto3mo ago
It passes for name but not for email ?
Lucky0
Lucky03mo ago
yes
dissto
dissto3mo ago
Im having troubles reproducing your error ☹️
No description
dissto
dissto3mo ago
Do you do something out of the ordinary in the ListUsers class?
Lucky0
Lucky03mo 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
dissto3mo 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
Lucky03mo ago
9 // tests\Feature\Admin\UserResourceTest.php:107
dissto
dissto3mo ago
I assume if you remove both inOrder: true it passes? Have you manually tested the sorting? Can you sort both by hand?
Lucky0
Lucky03mo ago
you are correct about removing the
inOrder:true
inOrder:true
. it passes yes, i did manually sorting and it is working fine.
dissto
dissto3mo 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
Lucky03mo ago
its okay and thank you..
dissto
dissto3mo ago
Do you have a custom getter or setter for the user's name in your User class?
Lucky0
Lucky03mo ago
in my model?
dissto
dissto3mo ago
Yes
Lucky0
Lucky03mo 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
More Posts
How to exclude a Model's attribute(object) to be passed to filament livewire component by defaultI'm trying to build a admin interface for https://github.com/spatie/laravel-uptime-monitor and i gotQuerying a distant relationshipin my filamentPHP dashboard I have a parcelResource. each parcel has one `region`, and a `state` hasFormatting dates returned in multi-relationship table column?I have a table column that references a many-to-many relationship field, and I want to display a forAdd info list to Page (with no resource)I've read through the docos ... i've created a page with no resource and edited the pages/page.php Show disabled EditAction instead of hide it to user who can't edit the record.I've added `canEdit()` method in a `BookResource` to control authorization for the `EditAction` in aAccess the current tenant in the importer resolveRecord() methodI'm using filament multitenancy. And I created an import for users. However, when I try to access thneed to tap into file upload save processI have this action in my page class: ``` public function getHeaderActions(): array { re->live on custom page HelpI have a custom page ```php <?php namespace App\Filament\Resources\FcmNotificationTemplateResource\One to many polymorphic relationshipHow can I retrieve each model in appointment table to filament table column? Example ```php $type_naFilterDatepicker with extraInputAttributes defaulta valueHello, I have the following filter but it does not load any default values. ```php Forms\Components