webcu's avatar
Level 2

Test that the elements in a collection belongs to an user

Hi!

I'm writing a Livewire component called Habits. I would like to test that the user only see the habits that belong to him. This is the current code of the render method.

public function render()
{
    return view('livewire.habit.index', [
        'habits' => Habit::where('user_id', Auth::id())->paginate(10),
    ]);
}

I tried moving habits to a public property, but then Livewire complained about the public properties can't be a Collection. I converted the collection to an array and then the view complained about not finding the properties of the object.

Not sure what is the best strategy to accomplish this.

Thanks in advance for your help!

0 likes
3 replies
Tray2's avatar
Tray2
Best Answer
Level 74

Do a feature test to check if the habbit is in the view or not.

Never tried it with Livewire but something like this might work.

/**
 * @test
 */
public function see_all_the_books_belonging_to_the_author()
{
  $author1 = Author::factory()->create();
  $author2 = Author::factory()->create(['first_name' => 'Secret', 'last_name' => 'Arne']);
  $book1Author1 = Book::factory()->create();
  $book2Author1 = Book::factory()->create();
  $book3Author2 = Book::factory()->create(['title' => 'Invisible Book']);

  AuthorBook::factory()->create([
     'book_id' => $book1Author1->id,
     'author_id' => $author1->id
  ]);

AuthorBook::factory()->create([
    'book_id' => $book2Author1->id,
    'author_id' => $author1->id
]);

AuthorBook::factory()->create([
    'book_id' => $book3Author2->id,
    'author_id' => $author2->id
]);

$response = $this->get('/authors/' . $author1->id);

$response->assertSee($author1->name);
$response->assertSee($book1Author1->title);
$response->assertSee($book2Author1->title);
$response->assertDontSee($book3Author2->title);
}
1 like
webcu's avatar
Level 2

Thanks @tray2! I followed your advice and I have the tests working perfectly now. I implement them like this:

    /** @test */
    function user_can_see_habits_belong_him()
    {
        $currentUser = User::factory()->create();
        $this->actingAs($currentUser);

        $habit = Habit::factory()->createOne([
            'name' => 'currrent-user-habit',
            'user_id' => $currentUser->id
        ]);

        Livewire::test(Index::class)
            ->assertSee($habit->name);
    }

    /** @test */
    function user_can_not_see_habits_do_not_belong_him()
    {
        // Current User
        $this->actingAs(User::factory()->create());

        // Second user, owner of the new Habit
        $secondUser = User::factory()->create();
        $habit = Habit::factory()->createOne([
            'name' => 'second-user-habit',
            'user_id' => $secondUser->id
        ]);

        Livewire::test(Index::class)
            ->assertDontSee($habit->name);
    }

I wasn't sure of using some text to verify if the functionality was working correctly or not, because text can change more often, but like I'm controlling the text directly from the test, I think is good enough.

Thanks again and have a nice day!

Tray2's avatar

As long as you are the one in control over the text like is this case it's fine to test for it.

Please or to participate in this conversation.