Write tests - Write tests for what was done to this point that will be kept #11
|
|
@ -45,12 +45,14 @@ class Room extends Model
|
|||
public function addJudge($userId): void
|
||||
{
|
||||
$this->judges()->attach($userId);
|
||||
$this->load('judges');
|
||||
AuditionChange::dispatch();
|
||||
}
|
||||
|
||||
public function removeJudge($userId): void
|
||||
{
|
||||
$this->judges()->detach($userId);
|
||||
$this->load('judges');
|
||||
AuditionChange::dispatch();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Audition;
|
||||
use App\Models\Entry;
|
||||
use App\Models\Room;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->room = Room::factory()->create();
|
||||
$otherRoom = Room::factory()->create();
|
||||
$auditions = Audition::factory()->count(3)->create(['room_id' => $this->room->id]);
|
||||
foreach ($auditions as $audition) {
|
||||
Entry::factory()->count(5)->create(['audition_id' => $audition->id]);
|
||||
}
|
||||
$otherAuditions = Audition::factory()->count(3)->create(['room_id' => $otherRoom->id]);
|
||||
foreach ($otherAuditions as $audition) {
|
||||
Entry::factory()->count(5)->create(['audition_id' => $audition->id]);
|
||||
}
|
||||
});
|
||||
|
||||
test('created 30 entries in prep', function () {
|
||||
expect(Entry::all()->count())->toBe(30);
|
||||
});
|
||||
|
||||
it('has auditions', function () {
|
||||
expect($this->room->auditions->count())->toBe(3)
|
||||
->and($this->room->auditions->first())->toBeInstanceOf(Audition::class);
|
||||
});
|
||||
|
||||
it('has entries', function () {
|
||||
expect($this->room->entries->count())->toBe(15)
|
||||
->and($this->room->entries->first())->toBeInstanceOf(Entry::class);
|
||||
});
|
||||
|
||||
it('has users', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->room->users()->attach($user->id);
|
||||
expect($this->room->users->count())->toBe(1)
|
||||
->and($this->room->users->first()->first_name)->toBe($user->first_name);
|
||||
});
|
||||
|
||||
it('has judges', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->room->judges()->attach($user->id);
|
||||
expect($this->room->judges->count())->toBe(1)
|
||||
->and($this->room->judges->first()->first_name)->toBe($user->first_name);
|
||||
});
|
||||
|
||||
it('can add a judge', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->room->addJudge($user->id);
|
||||
expect($this->room->judges->count())->toBe(1)
|
||||
->and($this->room->judges->first()->first_name)->toBe($user->first_name);
|
||||
});
|
||||
|
||||
it('can remove a judge', function () {
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
$this->room->addJudge($user->id);
|
||||
// Act & Assert
|
||||
expect($this->room->judges->count())->toBe(1)
|
||||
->and($this->room->judges->first()->first_name)->toBe($user->first_name);
|
||||
// Re Act
|
||||
$this->room->removeJudge($user->id);
|
||||
// Reassert
|
||||
expect($this->room->judges->count())->toBe(0);
|
||||
});
|
||||
Loading…
Reference in New Issue