Skip to content

Commit ae927de

Browse files
test(home): add feature tests for home page rendering and data sharing
1 parent 7043963 commit ae927de

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/Feature/HomeTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use App\Models\Notice;
4+
use App\Models\User;
5+
use Inertia\Testing\AssertableInertia as Assert;
6+
7+
test('home page can be rendered', function () {
8+
$response = $this->get(route('home'));
9+
$response->assertStatus(200);
10+
});
11+
12+
test('gets redirected to dashboard when authenticated', function () {
13+
$user = User::factory()->create();
14+
15+
$response = $this->actingAs($user)
16+
->get(route('home'));
17+
18+
$response->assertRedirect(route('dashboard'));
19+
});
20+
21+
test('home page shares global props for guests', function () {
22+
$response = $this->get(route('home'));
23+
24+
$response->assertInertia(fn (Assert $page) => $page
25+
->has('name')
26+
->has('quote.message')
27+
->has('quote.author')
28+
->has('auth')
29+
->where('auth.user', null)
30+
->has('sidebarOpen')
31+
->has('flash')
32+
->has('notices')
33+
->has('latestArticles')
34+
);
35+
});
36+
37+
test('about shares auth user when authenticated', function () {
38+
$user = User::factory()->create();
39+
40+
$this->actingAs($user)
41+
->get(route('about'))
42+
->assertInertia(fn (Assert $page) => $page
43+
->where('auth.user.id', $user->id)
44+
);
45+
});
46+
47+
test('home page returns only active notices', function () {
48+
$response = $this->get(route('home'));
49+
50+
$activeNotices = Notice::whereIsActive(true)->count();
51+
52+
$response->assertInertia(fn (Assert $page) => $page
53+
->has('notices', $activeNotices)
54+
);
55+
56+
$notices = $response->inertiaProps('notices');
57+
$this->assertCount($activeNotices, $notices);
58+
});
59+
60+
test('home page returns latest articles', function () {
61+
$response = $this->get(route('home'));
62+
63+
$response->assertInertia(fn (Assert $page) => $page
64+
->has('latestArticles', 3)
65+
);
66+
67+
$articles = $response->inertiaProps('latestArticles');
68+
$this->assertCount(3, $articles);
69+
});

0 commit comments

Comments
 (0)