Skip to content

Commit 09f57e9

Browse files
DanRibbensjmikrut
andauthored
test: group relationships
* test: relationship fields inside groups and subgroup * test: group nested relationships and arrays * test: improves coverage for hooks Co-authored-by: James <[email protected]>
1 parent 893772e commit 09f57e9

File tree

14 files changed

+299
-119
lines changed

14 files changed

+299
-119
lines changed

test/fields-relationship/config.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CollectionConfig } from '../../../src/collections/config/types';
1+
import type { CollectionConfig } from '../../src/collections/config/types';
22
import { buildConfig } from '../buildConfig';
33
import { devUser } from '../credentials';
44
import { mapAsync } from '../../src/utilities/mapAsync';
@@ -9,8 +9,6 @@ export const relationOneSlug = 'relation-one';
99
export const relationTwoSlug = 'relation-two';
1010
export const relationRestrictedSlug = 'relation-restricted';
1111
export const relationWithTitleSlug = 'relation-with-title';
12-
export const groupWithNestedRelationship = 'group-nested-relation-with-title';
13-
export const nestedRelationship = 'nested-relation-with-title';
1412

1513
export interface FieldsRelationship {
1614
id: string;
@@ -39,20 +37,6 @@ const baseRelationshipFields: CollectionConfig['fields'] = [
3937
},
4038
];
4139

42-
const groupWithNestedRelationshipFields = (relationTo: string): CollectionConfig['fields'] => ([
43-
{
44-
type: 'group',
45-
name: 'group',
46-
fields: [
47-
{
48-
name: 'relation',
49-
type: 'relationship',
50-
relationTo,
51-
},
52-
],
53-
},
54-
]);
55-
5640
export default buildConfig({
5741
collections: [
5842
{
@@ -126,14 +110,6 @@ export default buildConfig({
126110
},
127111
fields: baseRelationshipFields,
128112
},
129-
{
130-
slug: groupWithNestedRelationship,
131-
fields: groupWithNestedRelationshipFields(nestedRelationship),
132-
},
133-
{
134-
slug: nestedRelationship,
135-
fields: groupWithNestedRelationshipFields(relationWithTitleSlug),
136-
},
137113
],
138114
onInit: async (payload) => {
139115
await payload.create({

test/fields-relationship/int.spec.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

test/fields-relationship/payload-types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export interface GroupNestedRelationWithTitle {
9797
export interface NestedRelationWithTitle {
9898
id: string;
9999
group?: {
100-
relation?: string | RelationWithTitle;
100+
subGroup?: {
101+
relation?: string | RelationOne;
102+
};
101103
};
102104
createdAt: string;
103105
updatedAt: string;

test/fields/collections/Array/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ const ArrayFields: CollectionConfig = {
5858
},
5959
],
6060
},
61+
{
62+
type: 'array',
63+
name: 'potentiallyEmptyArray',
64+
fields: [
65+
{
66+
type: 'text',
67+
name: 'text',
68+
},
69+
],
70+
},
6171
],
6272
};
6373

test/fields/collections/Group/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ const GroupFields: CollectionConfig = {
5757
},
5858
],
5959
},
60+
{
61+
name: 'potentiallyEmptyGroup',
62+
type: 'group',
63+
fields: [
64+
{
65+
name: 'text',
66+
type: 'text',
67+
},
68+
],
69+
},
6070
],
6171
};
6272

test/fields/int.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import config from '../uploads/config';
44
import payload from '../../src';
55
import { pointDoc } from './collections/Point';
66
import type { ArrayField, GroupField } from './payload-types';
7-
import { arrayFieldsSlug, arrayDefaultValue } from './collections/Array';
7+
import { arrayFieldsSlug, arrayDefaultValue, arrayDoc } from './collections/Array';
88
import { groupFieldsSlug, groupDefaultChild, groupDefaultValue, groupDoc } from './collections/Group';
99
import { defaultText } from './collections/Text';
1010

@@ -89,6 +89,15 @@ describe('Fields', () => {
8989
});
9090
});
9191

92+
it('should return empty array for arrays when no data present', async () => {
93+
const document = await payload.create<ArrayField>({
94+
collection: arrayFieldsSlug,
95+
data: arrayDoc,
96+
});
97+
98+
expect(document.potentiallyEmptyArray).toEqual([]);
99+
});
100+
92101
it('should create with ids and nested ids', async () => {
93102
const docWithIDs = await payload.create<GroupField>({
94103
collection: groupFieldsSlug,
@@ -158,5 +167,14 @@ describe('Fields', () => {
158167
expect(document.group.defaultParent).toStrictEqual(groupDefaultValue);
159168
expect(document.group.defaultChild).toStrictEqual(groupDefaultChild);
160169
});
170+
171+
it('should return empty object for groups when no data present', async () => {
172+
const doc = await payload.create<GroupField>({
173+
collection: groupFieldsSlug,
174+
data: groupDoc,
175+
});
176+
177+
expect(doc.potentiallyEmptyGroup).toEqual({});
178+
});
161179
});
162180
});

test/fields/payload-types.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export interface ArrayField {
2020
text: string;
2121
id?: string;
2222
}[];
23-
readOnly?: {
23+
readOnly: {
24+
text?: string;
25+
id?: string;
26+
}[];
27+
potentiallyEmptyArray: {
2428
text?: string;
2529
id?: string;
2630
}[];
@@ -47,7 +51,7 @@ export interface BlockField {
4751
blockType: 'number';
4852
}
4953
| {
50-
subBlocks?: (
54+
subBlocks: (
5155
| {
5256
text: string;
5357
id?: string;
@@ -76,9 +80,9 @@ export interface BlockField {
7680
export interface CollapsibleField {
7781
id: string;
7882
text: string;
79-
group?: {
83+
group: {
8084
textWithinGroup?: string;
81-
subGroup?: {
85+
subGroup: {
8286
textWithinSubGroup?: string;
8387
};
8488
};
@@ -103,18 +107,21 @@ export interface ConditionalLogic {
103107
*/
104108
export interface GroupField {
105109
id: string;
106-
group?: {
110+
group: {
107111
text: string;
108112
defaultParent?: string;
109113
defaultChild?: string;
110-
subGroup?: {
114+
subGroup: {
111115
textWithinGroup?: string;
112-
arrayWithinGroup?: {
116+
arrayWithinGroup: {
113117
textWithinArray?: string;
114118
id?: string;
115119
}[];
116120
};
117121
};
122+
potentiallyEmptyGroup: {
123+
text?: string;
124+
};
118125
createdAt: string;
119126
updatedAt: string;
120127
}
@@ -134,7 +141,7 @@ export interface PointField {
134141
* @maxItems 2
135142
*/
136143
localized?: [number, number];
137-
group?: {
144+
group: {
138145
/**
139146
* @minItems 2
140147
* @maxItems 2
@@ -192,7 +199,7 @@ export interface TabsField {
192199
blockType: 'number';
193200
}
194201
| {
195-
subBlocks?: (
202+
subBlocks: (
196203
| {
197204
text: string;
198205
id?: string;
@@ -211,7 +218,7 @@ export interface TabsField {
211218
blockType: 'subBlocks';
212219
}
213220
)[];
214-
group?: {
221+
group: {
215222
number: number;
216223
};
217224
textarea?: string;

test/generateTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if (testConfigDir) {
3939
function setPaths(dir) {
4040
const configPath = path.resolve(dir, 'config.ts');
4141
const outputPath = path.resolve(dir, 'payload-types.ts');
42-
if (fs.existsSync(configPath) && fs.existsSync(outputPath)) {
42+
if (fs.existsSync(configPath)) {
4343
process.env.PAYLOAD_CONFIG_PATH = configPath;
4444
process.env.PAYLOAD_TS_OUTPUT_PATH = outputPath;
4545
return true;

test/hooks/collections/Hook/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const createHookOrder = [
9595
'collectionBeforeChange',
9696
'fieldBeforeChange',
9797
'fieldAfterRead',
98+
'collectionAfterRead',
9899
'fieldAfterChange',
99100
'collectionAfterChange',
100101
];
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { CollectionConfig } from '../../../../src/collections/config/types';
2+
import { relationsSlug } from '../Relations';
3+
4+
export const nestedAfterReadHooksSlug = 'nested-after-read-hooks';
5+
6+
export const generatedAfterReadText = 'hello';
7+
8+
const NestedAfterReadHooks: CollectionConfig = {
9+
slug: nestedAfterReadHooksSlug,
10+
fields: [
11+
{
12+
type: 'text',
13+
name: 'text',
14+
},
15+
{
16+
type: 'group',
17+
name: 'group',
18+
fields: [
19+
{
20+
type: 'array',
21+
name: 'array',
22+
fields: [
23+
{
24+
type: 'text',
25+
name: 'input',
26+
},
27+
{
28+
type: 'text',
29+
name: 'afterRead',
30+
hooks: {
31+
afterRead: [(): string => {
32+
return generatedAfterReadText;
33+
}],
34+
},
35+
},
36+
{
37+
name: 'shouldPopulate',
38+
type: 'relationship',
39+
relationTo: relationsSlug,
40+
},
41+
],
42+
},
43+
{
44+
type: 'group',
45+
name: 'subGroup',
46+
fields: [
47+
{
48+
name: 'afterRead',
49+
type: 'text',
50+
hooks: {
51+
afterRead: [(): string => {
52+
return generatedAfterReadText;
53+
}],
54+
},
55+
},
56+
{
57+
name: 'shouldPopulate',
58+
type: 'relationship',
59+
relationTo: relationsSlug,
60+
},
61+
],
62+
},
63+
],
64+
},
65+
],
66+
};
67+
68+
export default NestedAfterReadHooks;

0 commit comments

Comments
 (0)