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
2 changed files with 160 additions and 1 deletions
Showing only changes of commit 0c782c9a64 - Show all commits

View File

@ -1,7 +1,7 @@
<x-layout.app>
<x-card.card class="mx-auto max-w-xl">
<x-card.heading>Edit Student</x-card.heading>
<x-form.form method="PATCH" action="/admin/students/{{ $student->id }}">
<x-form.form method="PATCH" action="{{ route('admin.students.update',$student) }}">
<x-form.body-grid columns="12">
<x-form.field name="first_name" label_text="First Name" colspan="5" value="{{ $student->first_name }}" />
<x-form.field name="last_name" label_text="Last Name" colspan="5" value="{{ $student->last_name }}" />

View File

@ -0,0 +1,159 @@
<?php
use App\Models\Audition;
use App\Models\School;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
use function Pest\Laravel\patch;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->schools = School::factory()->count(5)->create();
$this->student = Student::factory()->create();
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.students.edit';
get(route($checkRoute, $this->student))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute, $this->student))->assertOk();
actingAs(User::factory()->create());
get(route($checkRoute, $this->student))->assertRedirect(route('dashboard'));
});
it('submits a patch request', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.students.edit', $this->student));
$response->assertOk();
$response->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.students.update', $this->student),
'/form',
], false);
$response->assertSee('<input type="hidden" name="_method" value="PATCH">', false);
});
it('has an Edit Student submit button', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.students.edit', $this->student));
$response->assertOk();
$response->assertSeeInOrder([
'button',
'type',
'submit',
'Edit Student',
'/button',
], false);
});
it('has all needed fields', function () {
// Arrange
actingAs($this->adminUser);
$fieldNames = [
'first_name',
'last_name',
];
// Act & Assert
$response = get(route('admin.students.edit', $this->student));
$response->assertOk();
foreach ($fieldNames as $fieldName) {
$response->assertSeeInOrder([
'input',
'name=',
$fieldName,
'/',
]);
}
$response->assertSeeInOrder([
'select',
'name=',
'school_id',
'/select',
]);
$response->assertSeeInOrder([
'select',
'name=',
'grade',
'/select',
]);
});
it('has all schools in a dropdown', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.students.edit', $this->student));
$response->assertOk();
foreach ($this->schools as $school) {
$response->assertSeeInOrder([
'option',
'value=',
$school->id,
$school->name,
'/option',
]);
}
});
it('is populated with existing data', function () {
// Arrange
Audition::factory()->create(['minimum_grade' => 1, 'maximum_grade' => 18]); // Needed for the grade select
actingAs($this->adminUser);
$fieldList = ['first_name', 'last_name', 'grade', 'school_id'];
// Act & Assert
$response = get(route('admin.students.edit', $this->student));
$response->assertOk();
$response->assertSeeInOrder(['name=', 'first_name', 'value=', $this->student->first_name]);
$response->assertSeeInOrder(['name=', 'last_name', 'value=', $this->student->last_name]);
$response->assertSeeInOrder(['grade', 'option', 'value=', $this->student->grade], 'selected');
$response->assertSeeInOrder(['name=', 'school_id', 'value=', $this->student->school_id], 'selected');
});
it('rejects a submission by a non administrator', function () {
// Arrange
actingAs(User::factory()->create());
// Act & Assert
$response = patch(route('admin.students.update', $this->student), [
'first_name' => 'New First Name',
'last_name' => 'New Last Name',
]);
$response->assertRedirect(route('dashboard'));
});
it('allows an administrator to edit a student', function () {
// Arrange
$newSchool = School::factory()->create(['name' => 'New School']);
actingAs($this->adminUser);
$newData = [
'first_name' => 'New First Name',
'last_name' => 'New Last Name',
'grade' => '9',
'school_id' => $newSchool->id,
];
// Act
$response = patch(route('admin.students.update', $this->student), $newData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.students.index'));
// Get the changed student
$this->student->refresh();
expect($this->student->first_name)->toBe($newData['first_name'])
->and($this->student->last_name)->toBe($newData['last_name'])
->and($this->student->grade)->toEqual($newData['grade'])
->and($this->student->school->name)->toBe($newSchool->name);
get(route('admin.students.index'))
->assertOk()
->assertSee($newData['first_name'])
->assertSee($newData['last_name'])
->assertSee($newData['grade'])
->assertSee($newSchool->name);
});