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
3 changed files with 133 additions and 5 deletions
Showing only changes of commit 8e6c966ede - Show all commits

View File

@ -1,7 +1,7 @@
<x-layout.app> <x-layout.app>
<x-card.card class="mx-auto max-w-xl"> <x-card.card class="mx-auto max-w-xl">
<x-card.heading>Create Student</x-card.heading> <x-card.heading>Create Student</x-card.heading>
<x-form.form method="POST" action="/admin/students"> <x-form.form method="POST" action="{{ route('admin.students.store') }}">
<x-form.body-grid columns="12"> <x-form.body-grid columns="12">
<x-form.field name="first_name" label_text="First Name" colspan="5" /> <x-form.field name="first_name" label_text="First Name" colspan="5" />
<x-form.field name="last_name" label_text="Last Name" colspan="5" /> <x-form.field name="last_name" label_text="Last Name" colspan="5" />
@ -22,7 +22,7 @@
</x-form.select> </x-form.select>
</x-form.body-grid> </x-form.body-grid>
<x-form.footer> <x-form.footer class="pb-5">
<x-form.button>Create Student</x-form.button> <x-form.button>Create Student</x-form.button>
</x-form.footer> </x-form.footer>
</x-form.form> </x-form.form>

View File

@ -0,0 +1,128 @@
<?php
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\post;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->adminUser = User::factory()->admin()->create();
$this->schools = School::factory()->count(5)->create();
});
it('only shows for an admin user', function () {
// Act & Assert
$checkRoute = 'admin.students.create';
get(route($checkRoute))->assertRedirect(route('home'));
actingAs($this->adminUser);
get(route($checkRoute))->assertOk();
actingAs(User::factory()->create());
get(route($checkRoute))->assertRedirect(route('dashboard'));
});
it('submits a post request', function () {
// Arrange
actingAs($this->adminUser);
// Act & Assert
$response = get(route('admin.students.create'));
$response->assertOk();
$response->assertSeeInOrder([
'form',
'method',
'POST',
'action=',
route('admin.students.store'),
'/form',
]);
});
it('has all needed fields', function () {
// Arrange
actingAs($this->adminUser);
$fieldNames = [
'first_name',
'last_name',
];
// Act & Assert
$response = get(route('admin.students.create'));
$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.create'));
$response->assertOk();
foreach ($this->schools as $school) {
$response->assertSeeInOrder([
'option',
'value=',
$school->id,
$school->name,
'/option',
]);
}
});
it('rejects a submission by a non administrator', function () {
// Arrange
actingAs(User::factory()->create());
// Act & Assert
$response = post(route('admin.students.store'), [
'first_name' => 'New First Name',
'last_name' => 'New Last Name',
]);
$response->assertRedirect(route('dashboard'));
});
it('allows an administrator to create 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 = post(route('admin.students.store'), $newData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.students.index'));
// Get the latest created user
$createdStudent = Student::where('first_name', $newData['first_name'])->first();
expect($createdStudent->first_name)->toBe($newData['first_name'])
->and($createdStudent->last_name)->toBe($newData['last_name'])
->and($createdStudent->grade)->toEqual($newData['grade'])
->and($createdStudent->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);
});

View File

@ -26,11 +26,11 @@ beforeEach(function () {
it('only shows for an admin user', function () { it('only shows for an admin user', function () {
// Act & Assert // Act & Assert
$checkRoute = 'admin.users.create'; $checkRoute = 'admin.users.create';
get(route($checkRoute, $this->users[0]))->assertRedirect(route('home')); get(route($checkRoute))->assertRedirect(route('home'));
actingAs($this->adminUser); actingAs($this->adminUser);
get(route($checkRoute, $this->users[0]))->assertOk(); get(route($checkRoute))->assertOk();
actingAs($this->nonAdminUser); actingAs($this->nonAdminUser);
get(route($checkRoute, $this->users[0]))->assertRedirect(route('dashboard')); get(route($checkRoute))->assertRedirect(route('dashboard'));
}); });
it('submits a post request', function () { it('submits a post request', function () {
// Arrange // Arrange