Skip to content

Commit 8a085c8

Browse files
authored
Merge pull request #310 from robertocarlous/test/creator-list-default-sort-integration
Test/creator list default sort integration
2 parents 2c56c07 + e97562c commit 8a085c8

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Integration test: creator list default sort when sort is omitted
2+
//
3+
// Ensures the creator list handler applies the documented default sort field/order
4+
// when the client omits the `sort` query param, returning a stable response.
5+
6+
import { httpListCreators } from './creators.controllers';
7+
import * as creatorsUtils from './creators.utils';
8+
import type { CreatorProfile } from '../../types/profile.types';
9+
import {
10+
DEFAULT_CREATOR_LIST_ORDER,
11+
DEFAULT_CREATOR_LIST_SORT,
12+
} from '../../constants/creator-list-sort.constants';
13+
14+
// ── Lightweight request/response mocks ────────────────────────────────────────
15+
16+
function makeReq(query: Record<string, string> = {}): any {
17+
return { query };
18+
}
19+
20+
function makeRes(): any {
21+
const res: any = {};
22+
res.status = jest.fn().mockReturnValue(res);
23+
res.json = jest.fn().mockReturnValue(res);
24+
res.setHeader = jest.fn().mockReturnValue(res);
25+
res.set = jest.fn().mockReturnValue(res);
26+
return res;
27+
}
28+
29+
function makeNext(): jest.Mock {
30+
return jest.fn();
31+
}
32+
33+
// ── Fixtures ─────────────────────────────────────────────────────────────────
34+
35+
const FIXTURE_OLD: CreatorProfile = {
36+
id: 'cuid-old',
37+
userId: 'user-old',
38+
handle: 'old_creator',
39+
displayName: 'Old Creator',
40+
isVerified: false,
41+
createdAt: new Date('2024-01-01T00:00:00.000Z'),
42+
updatedAt: new Date('2024-01-01T00:00:00.000Z'),
43+
};
44+
45+
const FIXTURE_NEW: CreatorProfile = {
46+
id: 'cuid-new',
47+
userId: 'user-new',
48+
handle: 'new_creator',
49+
displayName: 'New Creator',
50+
isVerified: true,
51+
createdAt: new Date('2024-02-01T00:00:00.000Z'),
52+
updatedAt: new Date('2024-02-01T00:00:00.000Z'),
53+
};
54+
55+
describe('GET /api/v1/creators — default sort when sort is omitted', () => {
56+
afterEach(() => {
57+
jest.restoreAllMocks();
58+
});
59+
60+
it('returns 200, applies default sort, and responds with well-formed pagination meta', async () => {
61+
const fixturesOutOfOrder = [FIXTURE_OLD, FIXTURE_NEW];
62+
63+
jest
64+
.spyOn(creatorsUtils, 'fetchCreatorList')
65+
.mockImplementation(async (query: any) => {
66+
// The schema should supply defaults when `sort` is omitted.
67+
expect(query.sort).toBe(DEFAULT_CREATOR_LIST_SORT);
68+
expect(query.order).toBe(DEFAULT_CREATOR_LIST_ORDER);
69+
70+
// Return items in the expected default order (createdAt desc).
71+
const sorted = [...fixturesOutOfOrder].sort(
72+
(a, b) => b.createdAt.getTime() - a.createdAt.getTime()
73+
);
74+
return [sorted, sorted.length];
75+
});
76+
77+
// No `sort` query param provided.
78+
const req = makeReq({});
79+
const res = makeRes();
80+
await httpListCreators(req, res, makeNext());
81+
82+
expect(res.status).toHaveBeenCalledWith(200);
83+
const body = res.json.mock.calls[0][0];
84+
85+
expect(body).toHaveProperty('success', true);
86+
expect(body).toHaveProperty('data');
87+
88+
// Items are serialized via creator list mappers — assert order.
89+
expect(Array.isArray(body.data.items)).toBe(true);
90+
expect(body.data.items).toHaveLength(2);
91+
expect(body.data.items[0].name).toBe('New Creator');
92+
expect(body.data.items[1].name).toBe('Old Creator');
93+
94+
// Pagination meta should be present and well-formed.
95+
expect(body.data).toHaveProperty('meta');
96+
expect(body.data.meta).toEqual(
97+
expect.objectContaining({
98+
limit: expect.any(Number),
99+
offset: 0,
100+
total: 2,
101+
hasMore: false,
102+
})
103+
);
104+
});
105+
});
106+

0 commit comments

Comments
 (0)