Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use Illuminate\Support\Facades\Route;
use App\Http\Controllers;

Route::get('/profile', Controllers\ProfileController::class)->name('profile');
Route::get('/profile', Controllers\ProfileController::class)
->name('profile')
->middleware('auth');

Route::get('/', [Controllers\ListingController::class, 'index'])
->name('listings.index');
Expand Down
71 changes: 71 additions & 0 deletions tests/Feature/Livewire/ProfileEditorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests\Feature\Livewire;

use App\Http\Livewire\ProfileEditor;
use App\Models\User;
use Livewire\Livewire;
use Tests\Feature\FeatureTestCase;

class ProfileEditorTest extends FeatureTestCase
{
public function testRenderSuccess(): void
{
$user = User::factory()->create();

Livewire::actingAs($user);
$component = Livewire::test(ProfileEditor::class, ['user' => $user]);

$component->assertOk();
$component->assertViewIs('livewire.profile-editor');

$component->assertSet('email', $user->email);
$component->assertSet('name', $user->name);
$component->assertSet('user', $user);
}

public function testSubmitSuccess(): void
{
$user = User::factory()->create([
'name' => 'old name',
'email' => '[email protected]',
]);

$payload = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this terminology.

'name' => 'new name',
'email' => '[email protected]',
];

Livewire::actingAs($user);
$component = Livewire::test(ProfileEditor::class, ['user' => $user])
->set($payload)
->call('submit');

$user->refresh();
static::assertSame('new name', $user->name);
static::assertSame('[email protected]', $user->email);

$component->assertSessionHas('success', 'Profile updated successfully.');
$component->assertRedirect(route('profile'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't have assertRedirectToRoute available?

}

public function testSubmitFailsValidationRequired(): void
{
$user = User::factory()->create();

$payload = [
'name' => '',
'email' => '',
];

Livewire::actingAs($user);
$component = Livewire::test(ProfileEditor::class, ['user' => $user])
->set($payload)
->call('submit');

$component->assertHasErrors([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to validate length and the other things, though.

'name' => 'required',
'email' => 'required',
]);
}
}
27 changes: 27 additions & 0 deletions tests/Feature/ProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Feature;

use App\Http\Livewire\ProfileEditor;
use App\Models\User;

class ProfileTest extends FeatureTestCase
{
public function testFailsUnauthenticated(): void
{
$response = $this->get(route('profile'));

$response->assertRedirectToRoute('login');
}

public function testSuccess(): void
{
$user = User::factory()->create();

$this->actingAs($user);
$response = $this->get(route('profile'));

$response->assertOk();
$response->assertSeeLivewire(ProfileEditor::class);
}
}