Skip to content

Commit 4be410c

Browse files
authored
test: add types testing for select and joins (#11138)
Adds additional type testing for `select` and `joins` Local API properties to ensure we don't break those between changes
1 parent cd11175 commit 4be410c

File tree

3 files changed

+199
-2
lines changed

3 files changed

+199
-2
lines changed

test/types/config.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ export default buildConfigWithDefaults({
1818
type: 'text',
1919
name: 'text',
2020
},
21+
{
22+
type: 'text',
23+
name: 'title',
24+
},
25+
],
26+
},
27+
{
28+
slug: 'pages',
29+
fields: [
30+
{
31+
type: 'text',
32+
name: 'title',
33+
},
34+
{
35+
type: 'relationship',
36+
relationTo: 'pages-categories',
37+
name: 'category',
38+
},
39+
],
40+
},
41+
{
42+
slug: 'pages-categories',
43+
fields: [
44+
{
45+
type: 'text',
46+
name: 'title',
47+
},
48+
{
49+
type: 'join',
50+
name: 'relatedPages',
51+
collection: 'pages',
52+
on: 'category',
53+
},
2154
],
2255
},
2356
],

test/types/payload-types.ts

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,82 @@
66
* and re-run `payload generate:types` to regenerate this file.
77
*/
88

9+
/**
10+
* Supported timezones in IANA format.
11+
*
12+
* This interface was referenced by `Config`'s JSON-Schema
13+
* via the `definition` "supportedTimezones".
14+
*/
15+
export type SupportedTimezones =
16+
| 'Pacific/Midway'
17+
| 'Pacific/Niue'
18+
| 'Pacific/Honolulu'
19+
| 'Pacific/Rarotonga'
20+
| 'America/Anchorage'
21+
| 'Pacific/Gambier'
22+
| 'America/Los_Angeles'
23+
| 'America/Tijuana'
24+
| 'America/Denver'
25+
| 'America/Phoenix'
26+
| 'America/Chicago'
27+
| 'America/Guatemala'
28+
| 'America/New_York'
29+
| 'America/Bogota'
30+
| 'America/Caracas'
31+
| 'America/Santiago'
32+
| 'America/Buenos_Aires'
33+
| 'America/Sao_Paulo'
34+
| 'Atlantic/South_Georgia'
35+
| 'Atlantic/Azores'
36+
| 'Atlantic/Cape_Verde'
37+
| 'Europe/London'
38+
| 'Europe/Berlin'
39+
| 'Africa/Lagos'
40+
| 'Europe/Athens'
41+
| 'Africa/Cairo'
42+
| 'Europe/Moscow'
43+
| 'Asia/Riyadh'
44+
| 'Asia/Dubai'
45+
| 'Asia/Baku'
46+
| 'Asia/Karachi'
47+
| 'Asia/Tashkent'
48+
| 'Asia/Calcutta'
49+
| 'Asia/Dhaka'
50+
| 'Asia/Almaty'
51+
| 'Asia/Jakarta'
52+
| 'Asia/Bangkok'
53+
| 'Asia/Shanghai'
54+
| 'Asia/Singapore'
55+
| 'Asia/Tokyo'
56+
| 'Asia/Seoul'
57+
| 'Australia/Sydney'
58+
| 'Pacific/Guam'
59+
| 'Pacific/Noumea'
60+
| 'Pacific/Auckland'
61+
| 'Pacific/Fiji';
62+
963
export interface Config {
1064
auth: {
1165
users: UserAuthOperations;
1266
};
1367
collections: {
1468
posts: Post;
69+
pages: Page;
70+
'pages-categories': PagesCategory;
1571
users: User;
1672
'payload-locked-documents': PayloadLockedDocument;
1773
'payload-preferences': PayloadPreference;
1874
'payload-migrations': PayloadMigration;
1975
};
20-
collectionsJoins: {};
76+
collectionsJoins: {
77+
'pages-categories': {
78+
relatedPages: 'pages';
79+
};
80+
};
2181
collectionsSelect: {
2282
posts: PostsSelect<false> | PostsSelect<true>;
83+
pages: PagesSelect<false> | PagesSelect<true>;
84+
'pages-categories': PagesCategoriesSelect<false> | PagesCategoriesSelect<true>;
2385
users: UsersSelect<false> | UsersSelect<true>;
2486
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
2587
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -68,6 +130,32 @@ export interface UserAuthOperations {
68130
export interface Post {
69131
id: string;
70132
text?: string | null;
133+
title?: string | null;
134+
updatedAt: string;
135+
createdAt: string;
136+
}
137+
/**
138+
* This interface was referenced by `Config`'s JSON-Schema
139+
* via the `definition` "pages".
140+
*/
141+
export interface Page {
142+
id: string;
143+
title?: string | null;
144+
category?: (string | null) | PagesCategory;
145+
updatedAt: string;
146+
createdAt: string;
147+
}
148+
/**
149+
* This interface was referenced by `Config`'s JSON-Schema
150+
* via the `definition` "pages-categories".
151+
*/
152+
export interface PagesCategory {
153+
id: string;
154+
title?: string | null;
155+
relatedPages?: {
156+
docs?: (string | Page)[] | null;
157+
hasNextPage?: boolean | null;
158+
} | null;
71159
updatedAt: string;
72160
createdAt: string;
73161
}
@@ -99,6 +187,14 @@ export interface PayloadLockedDocument {
99187
relationTo: 'posts';
100188
value: string | Post;
101189
} | null)
190+
| ({
191+
relationTo: 'pages';
192+
value: string | Page;
193+
} | null)
194+
| ({
195+
relationTo: 'pages-categories';
196+
value: string | PagesCategory;
197+
} | null)
102198
| ({
103199
relationTo: 'users';
104200
value: string | User;
@@ -151,6 +247,27 @@ export interface PayloadMigration {
151247
*/
152248
export interface PostsSelect<T extends boolean = true> {
153249
text?: T;
250+
title?: T;
251+
updatedAt?: T;
252+
createdAt?: T;
253+
}
254+
/**
255+
* This interface was referenced by `Config`'s JSON-Schema
256+
* via the `definition` "pages_select".
257+
*/
258+
export interface PagesSelect<T extends boolean = true> {
259+
title?: T;
260+
category?: T;
261+
updatedAt?: T;
262+
createdAt?: T;
263+
}
264+
/**
265+
* This interface was referenced by `Config`'s JSON-Schema
266+
* via the `definition` "pages-categories_select".
267+
*/
268+
export interface PagesCategoriesSelect<T extends boolean = true> {
269+
title?: T;
270+
relatedPages?: T;
154271
updatedAt?: T;
155272
createdAt?: T;
156273
}

test/types/types.spec.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import type { BulkOperationResult, PaginatedDocs, SelectType, TypeWithVersion } from 'payload'
1+
import type {
2+
BulkOperationResult,
3+
JoinQuery,
4+
PaginatedDocs,
5+
SelectType,
6+
TypeWithVersion,
7+
Where,
8+
} from 'payload'
29

310
import payload from 'payload'
411
import { describe, expect, test } from 'tstyche'
512

613
import type { Menu, Post, User } from './payload-types.js'
714

15+
const asType = <T>() => {
16+
return '' as T
17+
}
18+
819
describe('Types testing', () => {
920
test('payload.find', () => {
1021
expect(payload.find({ collection: 'users' })).type.toBe<Promise<PaginatedDocs<User>>>()
@@ -77,4 +88,40 @@ describe('Types testing', () => {
7788
Promise<TypeWithVersion<Menu>>
7889
>()
7990
})
91+
92+
describe('select', () => {
93+
test('should include only ID if select is an empty object', () => {
94+
expect(payload.findByID({ collection: 'posts', id: 'id', select: {} })).type.toBe<
95+
Promise<{ id: Post['id'] }>
96+
>()
97+
})
98+
99+
test('should include only title and ID', () => {
100+
expect(
101+
payload.findByID({ collection: 'posts', id: 'id', select: { title: true } }),
102+
).type.toBe<Promise<{ id: Post['id']; title?: Post['title'] }>>()
103+
})
104+
105+
test('should exclude title', () => {
106+
expect(
107+
payload.findByID({ collection: 'posts', id: 'id', select: { title: false } }),
108+
).type.toBe<Promise<Omit<Post, 'title'>>>()
109+
})
110+
})
111+
112+
describe('joins', () => {
113+
test('join query for pages should have type never as pages does not define any joins', () => {
114+
expect(asType<JoinQuery<'pages'>>()).type.toBe<never>()
115+
})
116+
117+
test('join query for pages-categories should be defined with the relatedPages key', () => {
118+
expect(asType<JoinQuery<'pages-categories'>>()).type.toBeAssignableWith<{
119+
relatedPages?: {
120+
limit?: number
121+
sort?: string
122+
where?: Where
123+
}
124+
}>()
125+
})
126+
})
80127
})

0 commit comments

Comments
 (0)