Files
SkinbaseNova/tests/Feature/Profile/ProfileFavouritesLazyTest.php

75 lines
2.7 KiB
PHP

<?php
use App\Models\Artwork;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia;
function profileFavouritesFixture(): array
{
$user = User::factory()->create(['username' => 'lazyfavourites']);
$artworks = Artwork::factory()->count(13)->for($user)->create();
foreach ($artworks as $index => $artwork) {
DB::table('artwork_favourites')->insert([
'user_id' => $user->id,
'artwork_id' => $artwork->id,
'created_at' => now()->subMinutes($index),
'updated_at' => now()->subMinutes($index),
]);
}
return [$user, $artworks];
}
it('skips the favourites producer on the default Posts SSR request', function (): void {
[$user] = profileFavouritesFixture();
DB::flushQueryLog();
DB::enableQueryLog();
$this->get('/@' . $user->username)
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->where('initialTab', 'posts')
->where('favourites', null));
expect(collect(DB::getQueryLog())->pluck('query')->filter(fn (string $query): bool => str_contains(strtolower($query), 'artwork_favourites'))->all())->toBe([]);
});
it('keeps the first favourites page in direct SSR with the existing cursor contract', function (): void {
[$user, $artworks] = profileFavouritesFixture();
$this->get('/@' . $user->username . '/favourites')
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->where('initialTab', 'favourites')
->where('favourites.next_cursor', base64_encode('12'))
->where('favourites.data.0.id', $artworks->first()->id));
});
it('returns the same ordered twelve-item page and continues with the next cursor', function (): void {
[$user, $artworks] = profileFavouritesFixture();
$expected = $artworks->values();
$first = $this->getJson('/api/profile/' . $user->username . '/favourites')
->assertOk()
->assertJsonCount(12, 'data')
->assertJsonPath('data.0.id', $expected[0]->id)
->assertJsonPath('next_cursor', base64_encode('12'));
$this->getJson('/api/profile/' . $user->username . '/favourites?cursor=' . urlencode($first->json('next_cursor')))
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $expected[12]->id)
->assertJsonPath('next_cursor', null);
});
it('does not increment profile views for the lazy favourites endpoint', function (): void {
[$user] = profileFavouritesFixture();
$before = (int) $user->profile_views_count;
$this->getJson('/api/profile/' . $user->username . '/favourites')->assertOk();
expect((int) $user->fresh()->profile_views_count)->toBe($before);
});