Skip to content

Commit

Permalink
refactor: cleaned up test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MathewEm committed Dec 19, 2024
1 parent 10dd455 commit 3cd81bb
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 151 deletions.
151 changes: 0 additions & 151 deletions tests/Feature/Filament/UserResourceTest.php

This file was deleted.

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
);
});

0 comments on commit 3cd81bb

Please sign in to comment.