xtremer360
4 months ago
598
9
Testing

Generating Test with Mocking Query

Posted 4 months ago by xtremer360

I have the main feature request test partly written. On my users.index page it renders 3 different Livewire components that are paginated user datatables. One for active, one for inactive, and unactivated. I'm wanting to test each individual Livewire component.

The problem I'm having is that I was trying to run a query inside of the test and make sure I return the right data. The query is the same as the one inside the active users component but I want to make sure that it looks to make sure the right methods are being called because the test could change and the right data could still be returned isn't testing that the same query runs inside of the component.

I think I need mockery for this situation but not 100% sure. What does anyone else think?

<?php

namespace Tests\Feature\Users;

use App\Enums\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ViewUsersListSuccessConditionsTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function an_administrator_can_view_users_page()
    {
        $this->actAs(Role::ADMINISTRATOR);

        $response = $this->indexRequest('users');

        $response->assertOk();
        $response->assertViewIs('users.index');
    }
}
<?php

namespace App\Http\Livewire\Titles;

use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;

class ActiveUsers extends Component
{
    use WithPagination;

    public $perPage = 10;

    public function render()
    {
        return view('livewire.users.active-users', [
            'activeUsers' => User::active()->paginate($this->perPage)
        ]);
    }
}
<?php

namespace Tests\Integration\Livewire\Users;

use App\Http\Livewire\Users\ActiveUsers;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\Factories\UserFactory;
use Tests\TestCase;

class ActiveUsersTest extends TestCase
{
    use RefreshDatabase;

    /** @var \Illuminate\Support\Collection */
    protected $users;

    protected function setUp(): void
    {
        parent::setUp();

        $active = UserFactory::new()->count(3)->active()->create();
        $unactivated = UserFactory::new()->count(3)->unactivated()->create();
        $inactive = UserFactory::new()->count(3)->inactive()->create();

        $this->users = collect([
            'active'            => $active,
            'unactivated'       => $unactivated,
            'inactive'          => $inactive,
            'all'               => collect()
                                    ->concat($active)
                                    ->concat($unactivated)
                                    ->concat($inactive)
        ]);
    }

    /** @test */
    public function active_users_component_should_return_correct_view()
    {
        $component = Livewire::test(ActiveUsers::class);

        $this->assertEquals(
            'livewire.users.active-users',
            $component->lastRenderedView->getName()
        );
    }

    /** @test */
    public function active_users_component_should_pass_correct_data()
    {
        $component = Livewire::test(ActiveUsers::class);

        $mockUser
            ->shouldReceive('active')
            ->once()
            ->shouldReceive('paginate')
            ->once()
            ->andReturn($this->users->get(['active']));


        // Same query ran in livewire component
        $activeUsers = User::active()->get();

        $component->assertViewHas('activeUsers');
        $this->assertCount(3, $activeUsers);

        //  Not actually testing related to the component 
        // but the query results itself.
        $this->assertEquals(
            $this->users->get('active')->pluck('id')->toArray(),
            $activeUsers->pluck('id')->sort()->values()->toArray()
        );
    }
}

Please sign in or create an account to participate in this conversation.