Skip to content

Commit 61f56c7

Browse files
committed
[Tests] Update posts test to ensure default order is applied
See #88
1 parent 64851d8 commit 61f56c7

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class PostSchema extends Schema
5656
*/
5757
protected int $maxDepth = 3;
5858

59+
/**
60+
* @var string
61+
*/
62+
protected $defaultSort = '-createdAt';
63+
5964
/**
6065
* @inheritDoc
6166
*/

tests/dummy/tests/Api/V1/Posts/IndexTest.php

+25-8
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@
2424
use App\Models\User;
2525
use App\Tests\Api\V1\TestCase;
2626
use Illuminate\Support\Arr;
27+
use Illuminate\Support\Facades\Date;
2728

2829
class IndexTest extends TestCase
2930
{
3031

3132
public function test(): void
3233
{
33-
$posts = Post::factory()->count(3)->create();
34-
35-
$expected = collect($posts)
36-
->map(fn($post) => $this->serializer->post($post)->jsonSerialize())
34+
$posts = Post::factory()->sequence(
35+
['created_at' => Date::now()->subWeek()],
36+
['created_at' => Date::yesterday()],
37+
['created_at' => Date::now()],
38+
)->count(3)->create();
39+
40+
$expected = $posts
41+
->sortByDesc('created_at')
42+
->values()
3743
->all();
3844

3945
/** Draft post should not appear. */
@@ -45,7 +51,7 @@ public function test(): void
4551
->expects('posts')
4652
->get('/api/v1/posts');
4753

48-
$response->assertFetchedManyExact($expected);
54+
$response->assertFetchedManyInOrder($expected);
4955
}
5056

5157
public function testWithUser(): void
@@ -89,9 +95,18 @@ public function testPaginated(): void
8995
];
9096

9197
$links = [
92-
'first' => 'http://localhost/api/v1/posts?' . Arr::query(['page' => ['number' => 1, 'size' => 3]]),
93-
'next' => 'http://localhost/api/v1/posts?' . Arr::query(['page' => ['number' => 2, 'size' => 3]]),
94-
'last' => 'http://localhost/api/v1/posts?' . Arr::query(['page' => ['number' => 2, 'size' => 3]]),
98+
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
99+
'page' => ['number' => 1, 'size' => 3],
100+
'sort' => '-createdAt',
101+
]),
102+
'next' => 'http://localhost/api/v1/posts?' . Arr::query([
103+
'page' => ['number' => 2, 'size' => 3],
104+
'sort' => '-createdAt',
105+
]),
106+
'last' => 'http://localhost/api/v1/posts?' . Arr::query([
107+
'page' => ['number' => 2, 'size' => 3],
108+
'sort' => '-createdAt',
109+
]),
95110
];
96111

97112
$response = $this
@@ -191,10 +206,12 @@ public function testFilteredAndPaginated(): void
191206
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
192207
'filter' => ['published' => 'true'],
193208
'page' => ['number' => 1, 'size' => 10],
209+
'sort' => '-createdAt',
194210
]),
195211
'last' => 'http://localhost/api/v1/posts?' . Arr::query([
196212
'filter' => ['published' => 'true'],
197213
'page' => ['number' => 1, 'size' => 10],
214+
'sort' => '-createdAt',
198215
]),
199216
];
200217

0 commit comments

Comments
 (0)