Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): added User Resource tests for Librarium #880

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"nunomaduro/collision": "^8.1.1",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"pestphp/pest-plugin-livewire": "^3.0",
"spatie/laravel-ignition": "^2.7.0"
},
"autoload": {
Expand Down
70 changes: 68 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions tests/Feature/Librarium/UserResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Tests\Feature\Librarium;

use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Filament\Resources\UserResource\Pages\ListUsers;
use App\Models\User;

describe('Access control for User Resources', function () {

beforeEach(function () {
$this->record = User::factory()->create();
$this->user = User::factory()->create();
$this->admin = User::factory()->create();
$this->admin->assignRole('admin');
});

it('An unauthorized user cannot render the page', function ($url) {
$this->actingAs($this->user)->get($url)->assertForbidden();
})->with([
'ListUsers' => [fn () => ListUsers::getUrl()],
'CreateUser' => [fn () => CreateUser::getUrl()],
]);

it('An unauthorized user cannot render the edit page', function () {
$this->actingAs($this->user)->livewire(EditUser::class, ['record' => $this->record->getRouteKey()])->assertForbidden();
});

it('Can render the index page', function () {
$this->actingAs($this->admin)->get(ListUsers::getUrl())->assertOk();
});

it('Cannot render the user create page', function () {
$this->actingAs($this->admin)->get(CreateUser::getUrl())->assertForbidden();
});

it('Can render logout button', function () {})->todo();
it('Can logout path successful', function () {})->todo();
});

describe('List Users table features', function () {

beforeEach(function () {
$this->records = User::factory(5)->create();
$this->admin = User::factory()->create();
$this->admin->assignRole('admin');
});

it('Columns are displayed', function () {
collect([
'first_name',
'last_name',
'email',
'email_verified_at',
'active',
'roles.name',
])->each(fn ($column) => $this->actingAs($this->admin)->livewire(ListUsers::class)
->assertTableColumnExists($column)
->assertCanRenderTableColumn($column));
});

it('Columns can be sorted', function () {
collect([
'first_name',
'last_name',
'email_verified_at',
'active',
])->each(fn ($column) => $this->actingAs($this->admin)->livewire(ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($this->records->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($this->records->sortByDesc($column), inOrder: true));
});

it('Columns can be searched', function () {
collect([
'first_name',
'last_name',
'email',
'roles.name',
])->each(function ($column) {
$value = $this->records->first()->{$column};

$this->actingAs($this->admin)->livewire(ListUsers::class)
->searchTable($value)
->assertCanSeeTableRecords($this->records->where($column, $value))
->assertCanNotSeeTableRecords($this->records->where($column, '!=', $value));
});
});

it('Delete User button cannot be rendered', function () {
$this->actingAs($this->admin)->get(ListUsers::getUrl())
->assertDontSee('Delete');
});
});

describe('Edit User page features', function () {

beforeEach(function () {
$this->record = User::factory()->create();
$this->admin = User::factory()->create();
$this->admin->assignRole('admin');
});

it('Delete User button cannot be rendered', function () {
$this->actingAs($this->admin)->get(ListUsers::getUrl())
->assertDontSee('Delete');
});

it('Can edit an existing user', function () {
$this->actingAs($this->admin)->livewire(Edituser::class, ['record' => $this->record->getRouteKey()])
->fillForm([
'email' => "{$this->record->first_name}.{$this->record->last_name}@dfo-mpo.gc.ca",
])
->assertActionExists('save')
->call('save')
->assertHasNoFormErrors();

$this->actingAs($this->admin)->assertDatabaseHas(User::class, [
'email' => "{$this->record->first_name}.{$this->record->last_name}@dfo-mpo.gc.ca",
]);
})->todo(note: <<<'NOTE'
Add attributes: roles
NOTE);

it('Cannot save an invalid field', function () {
//
})->todo(
issue: 857,
note: <<<'NOTE'
validate inputs
NOTE
);
});
Loading