Write tests - Write tests for what was done to this point that will be kept #11

Merged
okorpheus merged 61 commits from write-tests into master 2024-07-05 21:21:32 +00:00
4 changed files with 87 additions and 9 deletions
Showing only changes of commit e5699ed8d8 - Show all commits

View File

@ -6,7 +6,7 @@
<x-slot:title class="ml-3">Users</x-slot:title> <x-slot:title class="ml-3">Users</x-slot:title>
<x-slot:subtitle class="ml-3">Click name to edit</x-slot:subtitle> <x-slot:subtitle class="ml-3">Click name to edit</x-slot:subtitle>
<x-slot:title_block_right class="mr-3"> <x-slot:title_block_right class="mr-3">
<x-form.button href="/admin/users/create">New User</x-form.button> <x-form.button href="{{ route('admin.users.create') }}">New User</x-form.button>
</x-slot:title_block_right> </x-slot:title_block_right>
<thead> <thead>
@ -21,7 +21,7 @@
<x-table.body> <x-table.body>
@foreach($users as $user) @foreach($users as $user)
<tr> <tr>
<x-table.td><a href="/admin/users/{{ $user->id }}/edit">{{ $user->full_name(true) }}</a></x-table.td> <x-table.td><a href="{{ route('admin.users.edit',$user) }}">{{ $user->full_name(true) }}</a></x-table.td>
<x-table.td>{{ $user->has_school() ? $user->school->name : ' ' }}</x-table.td> <x-table.td>{{ $user->has_school() ? $user->school->name : ' ' }}</x-table.td>
<x-table.td>{{ $user->email }}</x-table.td> <x-table.td>{{ $user->email }}</x-table.td>
<x-table.td>{{ $user->cell_phone }}</x-table.td> <x-table.td>{{ $user->cell_phone }}</x-table.td>

View File

@ -108,11 +108,11 @@ Route::middleware(['auth', 'verified', CheckIfAdmin::class])->prefix('admin/')->
// Admin User Routes // Admin User Routes
Route::prefix('users')->controller(\App\Http\Controllers\Admin\UserController::class)->group(function () { Route::prefix('users')->controller(\App\Http\Controllers\Admin\UserController::class)->group(function () {
Route::get('/', 'index'); Route::get('/', 'index')->name('admin.users.index');
Route::get('/create', 'create'); Route::get('/create', 'create')->name('admin.users.create');
Route::post('/', 'store'); Route::post('/', 'store')->name('admin.users.store');
Route::get('/{user}/edit', 'edit'); Route::get('/{user}/edit', 'edit')->name('admin.users.edit');
Route::patch('/{user}', 'update'); Route::patch('/{user}', 'update')->name('admin.users.update');
Route::delete('/{user}', 'destroy'); Route::delete('/{user}', 'destroy')->name('admin.users.destroy');
}); });
}); });

View File

@ -13,7 +13,7 @@ it('only shows for an admin user', function () {
$nonAdminUser = User::factory()->create(); $nonAdminUser = User::factory()->create();
// Act & Assert // Act & Assert
get(route('dashboard'))->assertRedirect(route('home')); get(route('admin.dashboard'))->assertRedirect(route('home'));
actingAs($adminUser); actingAs($adminUser);
get(route('admin.dashboard'))->assertOk(); get(route('admin.dashboard'))->assertOk();
actingAs($nonAdminUser); actingAs($nonAdminUser);

View File

@ -0,0 +1,78 @@
<?php
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->nonAdminUser = User::factory()->create();
$this->tabUser = User::factory()->tab()->create();
$this->users = User::factory(3)->create();
$this->schools = [];
foreach ($this->users as $user) {
$school = School::factory()->create();
$user->school_id = $school->id;
$user->save();
}
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.users.index';
get(route($checkRoute))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute))->assertOk();
actingAs($this->nonAdminUser);
get(route($checkRoute))->assertRedirect(route('dashboard'));
});
it('has a new user link', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
get(route('admin.users.index'))
->assertOk()
->assertSeeInOrder([
'href=',
route('admin.users.create'),
'New User',
'/a',
]);
});
it('shows user data', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.users.index'));
$response->assertOk();
foreach ($this->users as $user) {
$response->assertSeeInOrder([
$user->full_name(true),
$user->school->name,
$user->email,
$user->cell_phone,
$user->judging_preference,
]);
}
});
it('has an edit link for each user', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.users.index'));
$response->assertOk();
$response->assertSee(route('admin.users.edit', $this->adminUser));
$response->assertSee(route('admin.users.edit', $this->nonAdminUser));
$response->assertSee(route('admin.users.edit', $this->tabUser));
foreach ($this->users as $user) {
$response->assertSee(route('admin.users.edit', $user));
}
});