From 6dd147c3e24f959be57df2859304b3c974674834 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Sat, 24 Feb 2024 23:15:14 +0100 Subject: [PATCH] fix tests --- database/factories/ToDoFactory.php | 2 +- resources/js/pages/ToDos/ToDoShow.tsx | 2 +- tests/Feature/TimeTrackerTest.php | 12 ++++++------ tests/Feature/ToDoTest.php | 21 ++++++++++++++++++++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/database/factories/ToDoFactory.php b/database/factories/ToDoFactory.php index 2d0997f..22f7a2d 100644 --- a/database/factories/ToDoFactory.php +++ b/database/factories/ToDoFactory.php @@ -20,7 +20,7 @@ class ToDoFactory extends Factory return [ 'user_id' => User::inRandomOrder()->value('id'), 'name' => $this->faker->words(1, 4), - 'checked' => $this->faker->boolean, + 'checked' => rand(0, 1) ? now() : null, ]; } } diff --git a/resources/js/pages/ToDos/ToDoShow.tsx b/resources/js/pages/ToDos/ToDoShow.tsx index 96b1298..090010f 100644 --- a/resources/js/pages/ToDos/ToDoShow.tsx +++ b/resources/js/pages/ToDos/ToDoShow.tsx @@ -57,7 +57,7 @@ const ToDoShow = () => { : <>

{toDo?.name}

-

Terminé le {toDo?.checked ? (new Date(toDo.checked)).toSmallFrDate() : ''}

+ {toDo?.checked &&

Terminé le {(new Date(toDo.checked)).toSmallFrDate()}

}

{toDo?.description}

} diff --git a/tests/Feature/TimeTrackerTest.php b/tests/Feature/TimeTrackerTest.php index fcd5ec1..d9dc41b 100644 --- a/tests/Feature/TimeTrackerTest.php +++ b/tests/Feature/TimeTrackerTest.php @@ -8,7 +8,7 @@ test('user can start a time tracker', function () { Sanctum::actingAs($user = User::factory()->create()); $toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]); - $this->postJson('/api/time-tracker', ['todo_id' => $toDo->id]) + $this->postJson('/api/time-trackers', ['todo_id' => $toDo->id]) ->assertCreated() ->assertJson([ 'id' => $toDo->timeTrackers()->value('id'), @@ -27,10 +27,10 @@ test('user can retrieve his current timer', function () { Sanctum::actingAs($user = User::factory()->create()); $toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]); - $this->postJson('/api/time-tracker', ['todo_id' => $toDo->id]) + $this->postJson('/api/time-trackers', ['todo_id' => $toDo->id]) ->assertCreated(); - $this->get('/api/time-tracker/user') + $this->get('/api/time-trackers/user') ->assertOk() ->assertJson([ 'id' => $toDo->timeTrackers()->value('id'), @@ -48,7 +48,7 @@ test('user can retrieve his current timer', function () { test('user has no content response if not current time tracker', function () { Sanctum::actingAs($user = User::factory()->create()); - $this->get('/api/time-tracker/user') + $this->get('/api/time-trackers/user') ->assertNoContent(); }); @@ -56,10 +56,10 @@ test('user can stop current time tracker', function () { Sanctum::actingAs($user = User::factory()->create()); $toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]); - $this->postJson('/api/time-tracker', ['todo_id' => $toDo->id]) + $this->postJson('/api/time-trackers', ['todo_id' => $toDo->id]) ->assertCreated(); - $this->delete('/api/time-tracker/user') + $this->delete('/api/time-trackers/user') ->assertNoContent(); expect($toDo->timeTrackers->first()) diff --git a/tests/Feature/ToDoTest.php b/tests/Feature/ToDoTest.php index f6c0480..7dbf1a4 100644 --- a/tests/Feature/ToDoTest.php +++ b/tests/Feature/ToDoTest.php @@ -32,7 +32,7 @@ test('an user can retrieve his to dos', function () { Sanctum::actingAs($this->user); ToDo::factory()->count(10)->create([ 'user_id' => $this->user->id, - 'checked' => false, + 'checked' => null, ]); $response = $this->get('api/todos'); @@ -48,6 +48,25 @@ test('an user can retrieve his to dos', function () { ->assertJson($toDos); }); +test('an user can retrieve his finished to dos', function () { + Sanctum::actingAs($this->user); + ToDo::factory()->count(10)->create([ + 'user_id' => $this->user->id, + ]); + + $response = $this->get('api/todos/finished'); + + $toDos = $this->user->toDos()->whereNotNull('checked')->get()->map(fn ($toDo) => [ + 'id' => $toDo->id, + 'user_id' => $toDo->user_id, + 'name' => $toDo->name, + 'checked' => now(), + ])->toArray(); + + $response->assertOk() + ->assertJson($toDos); +}); + test('an user can retrieve a to do', function () { Sanctum::actingAs($this->user); $toDos = ToDo::factory()->count(10)->create([