Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created January 27, 2020 01:33
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save calebporzio/f9df5776e3b978844e19e50b36d70376 to your computer and use it in GitHub Desktop.
A Livewire Test
<?php
namespace Tests\Feature;
use App\User;
use Tests\TestCase;
use Livewire\Livewire;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Testing\RefreshDatabase;
class LoginTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_view_login_page()
{
$this->get(route('login'))
->assertSuccessful()
->assertSeeLivewire('login');
}
/** @test */
public function is_redirected_if_already_logged_in()
{
auth()->login(
factory(User::class)->create()
);
$this->get(route('login'))
->assertRedirect('/');
}
/** @test */
public function can_login()
{
$user = factory(User::class)->create();
Livewire::test('login')
->set('email', $user->email)
->set('password', 'secret')
->call('submit');
$this->assertTrue(
auth()->user()->is(User::where('email', $user->email)->first())
);
}
/** @test */
public function is_redirected_to_intended_after_login_prompt_from_auth_guard()
{
Route::get('/intended')->middleware('auth');
$user = factory(User::class)->create();
$this->get('/intended')->assertRedirect('/login');
Livewire::test('login')
->set('email', $user->email)
->set('password', 'secret')
->call('submit')
->assertRedirect('/intended');
}
/** @test */
public function is_redirected_to_root_after_login()
{
$user = factory(User::class)->create();
Livewire::test('login')
->set('email', $user->email)
->set('password', 'secret')
->call('submit')
->assertRedirect('/');
}
/** @test */
public function email_is_required()
{
factory(User::class)->create();
Livewire::test('login')
->set('password', 'secret')
->call('submit')
->assertHasErrors(['email' => 'required']);
}
/** @test */
public function email_must_be_valid_email()
{
factory(User::class)->create();
Livewire::test('login')
->set('email', 'invalid-email')
->set('password', 'secret')
->call('submit')
->assertHasErrors(['email' => 'email']);
}
/** @test */
public function password_is_required()
{
$user = factory(User::class)->create();
Livewire::test('login')
->set('email', $user->email)
->call('submit')
->assertHasErrors(['password' => 'required']);
}
/** @test */
public function bad_login_attempt_shows_message()
{
$user = factory(User::class)->create();
Livewire::test('login')
->set('email', $user->email)
->set('password', 'bad-password')
->call('submit')
->assertHasErrors('email');
$this->assertNull(auth()->user());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment