Skip to content

Commit 42fd6fb

Browse files
authored
Merge pull request #180 from episerver/feature/CMS-42559-add-cmp-fragments-types
Add Support for CMP DAM assets
2 parents aa01f74 + 3b69ebf commit 42fd6fb

File tree

13 files changed

+1074
-37
lines changed

13 files changed

+1074
-37
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
import { describe, expect, test } from 'vitest';
2+
import {
3+
createFragment,
4+
createSingleContentQuery,
5+
createMultipleContentQuery,
6+
} from '../createQuery.js';
7+
import { contentType, initContentTypeRegistry } from '../../model/index.js';
8+
9+
describe('createFragment() with damEnabled for contentReference properties', () => {
10+
test('damEnabled = false should not include ContentReferenceItem fragment', async () => {
11+
const ct1 = contentType({
12+
key: 'ct1',
13+
baseType: '_page',
14+
properties: {
15+
image: { type: 'contentReference' },
16+
},
17+
});
18+
initContentTypeRegistry([ct1]);
19+
20+
// DAM disabled
21+
const result = await createFragment('ct1', new Set(), '', true, false);
22+
23+
// Should not include ContentReferenceItem fragments
24+
expect(result.some((line) => line.includes('PublicImageAsset'))).toBe(
25+
false
26+
);
27+
expect(result.some((line) => line.includes('PublicVideoAsset'))).toBe(
28+
false
29+
);
30+
expect(result.some((line) => line.includes('PublicRawFileAsset'))).toBe(
31+
false
32+
);
33+
expect(result.some((line) => line.includes('ContentReferenceItem'))).toBe(
34+
false
35+
);
36+
37+
// Should only include key and url
38+
expect(result).toMatchInlineSnapshot(`
39+
[
40+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
41+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
42+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
43+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
44+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
45+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
46+
"fragment ct1 on ct1 { __typename image { key url { ...ContentUrl } } ..._IContent }",
47+
]
48+
`);
49+
});
50+
51+
test('damEnabled = true should include ContentReferenceItem fragment', async () => {
52+
const ct1 = contentType({
53+
key: 'ct1',
54+
baseType: '_page',
55+
properties: {
56+
image: { type: 'contentReference' },
57+
},
58+
});
59+
initContentTypeRegistry([ct1]);
60+
61+
// DAM enabled
62+
const result = await createFragment('ct1', new Set(), '', true, true);
63+
64+
// Should include all ContentReferenceItem fragments
65+
expect(result.some((line) => line.includes('PublicImageAsset'))).toBe(true);
66+
expect(result.some((line) => line.includes('PublicVideoAsset'))).toBe(true);
67+
expect(result.some((line) => line.includes('PublicRawFileAsset'))).toBe(
68+
true
69+
);
70+
expect(result.some((line) => line.includes('ContentReferenceItem'))).toBe(
71+
true
72+
);
73+
74+
expect(result).toMatchInlineSnapshot(`
75+
[
76+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
77+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
78+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
79+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
80+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
81+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
82+
"fragment PublicImageAsset on cmp_PublicImageAsset { Url Title AltText Description Renditions { Id Name Url Width Height } FocalPoint { X Y } Tags { Guid Name } }",
83+
"fragment PublicVideoAsset on cmp_PublicVideoAsset { Url Title AltText Description Renditions { Id Name Url Width Height } Tags { Guid Name } }",
84+
"fragment PublicRawFileAsset on cmp_PublicRawFileAsset { Url Title Description Tags { Guid Name } }",
85+
"fragment ContentReferenceItem on ContentReference { item { ...PublicImageAsset ...PublicVideoAsset ...PublicRawFileAsset } }",
86+
"fragment ct1 on ct1 { __typename image { key url { ...ContentUrl } ...ContentReferenceItem } ..._IContent }",
87+
]
88+
`);
89+
});
90+
91+
test('damEnabled = false with array of contentReference', async () => {
92+
const ct1 = contentType({
93+
key: 'ct1',
94+
baseType: '_page',
95+
properties: {
96+
images: { type: 'array', items: { type: 'contentReference' } },
97+
},
98+
});
99+
initContentTypeRegistry([ct1]);
100+
101+
// DAM disabled
102+
const result = await createFragment('ct1', new Set(), '', true, false);
103+
104+
expect(result.some((line) => line.includes('ContentReferenceItem'))).toBe(
105+
false
106+
);
107+
expect(result).toMatchInlineSnapshot(`
108+
[
109+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
110+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
111+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
112+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
113+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
114+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
115+
"fragment ct1 on ct1 { __typename images { key url { ...ContentUrl } } ..._IContent }",
116+
]
117+
`);
118+
});
119+
120+
test('damEnabled = true with array of contentReference', async () => {
121+
const ct1 = contentType({
122+
key: 'ct1',
123+
baseType: '_page',
124+
properties: {
125+
images: { type: 'array', items: { type: 'contentReference' } },
126+
},
127+
});
128+
initContentTypeRegistry([ct1]);
129+
130+
const result = await createFragment('ct1', new Set(), '', true, true);
131+
132+
expect(result.some((line) => line.includes('ContentReferenceItem'))).toBe(
133+
true
134+
);
135+
expect(result).toMatchInlineSnapshot(`
136+
[
137+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
138+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
139+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
140+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
141+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
142+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
143+
"fragment PublicImageAsset on cmp_PublicImageAsset { Url Title AltText Description Renditions { Id Name Url Width Height } FocalPoint { X Y } Tags { Guid Name } }",
144+
"fragment PublicVideoAsset on cmp_PublicVideoAsset { Url Title AltText Description Renditions { Id Name Url Width Height } Tags { Guid Name } }",
145+
"fragment PublicRawFileAsset on cmp_PublicRawFileAsset { Url Title Description Tags { Guid Name } }",
146+
"fragment ContentReferenceItem on ContentReference { item { ...PublicImageAsset ...PublicVideoAsset ...PublicRawFileAsset } }",
147+
"fragment ct1 on ct1 { __typename images { key url { ...ContentUrl } ...ContentReferenceItem } ..._IContent }",
148+
]
149+
`);
150+
});
151+
152+
test('damEnabled with nested component containing contentReference', async () => {
153+
const ctBlock = contentType({
154+
key: 'ctBlock',
155+
baseType: '_component',
156+
properties: {
157+
image: { type: 'contentReference' },
158+
},
159+
});
160+
const ct1 = contentType({
161+
key: 'ct1',
162+
baseType: '_page',
163+
properties: {
164+
block: { type: 'component', contentType: ctBlock },
165+
},
166+
});
167+
initContentTypeRegistry([ct1, ctBlock]);
168+
169+
// Test with damEnabled = false
170+
const resultDisabled = await createFragment(
171+
'ct1',
172+
new Set(),
173+
'',
174+
true,
175+
false
176+
);
177+
expect(
178+
resultDisabled.some((line) => line.includes('ContentReferenceItem'))
179+
).toBe(false);
180+
181+
// Test with damEnabled = true
182+
const resultEnabled = await createFragment(
183+
'ct1',
184+
new Set(),
185+
'',
186+
true,
187+
true
188+
);
189+
expect(
190+
resultEnabled.some((line) => line.includes('ContentReferenceItem'))
191+
).toBe(true);
192+
expect(resultEnabled).toMatchInlineSnapshot(`
193+
[
194+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
195+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
196+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
197+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
198+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
199+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
200+
"fragment PublicImageAsset on cmp_PublicImageAsset { Url Title AltText Description Renditions { Id Name Url Width Height } FocalPoint { X Y } Tags { Guid Name } }",
201+
"fragment PublicVideoAsset on cmp_PublicVideoAsset { Url Title AltText Description Renditions { Id Name Url Width Height } Tags { Guid Name } }",
202+
"fragment PublicRawFileAsset on cmp_PublicRawFileAsset { Url Title Description Tags { Guid Name } }",
203+
"fragment ContentReferenceItem on ContentReference { item { ...PublicImageAsset ...PublicVideoAsset ...PublicRawFileAsset } }",
204+
"fragment ctBlockProperty on ctBlockProperty { __typename image { key url { ...ContentUrl } ...ContentReferenceItem } }",
205+
"fragment ct1 on ct1 { __typename ct1__block:block { ...ctBlockProperty } ..._IContent }",
206+
]
207+
`);
208+
});
209+
210+
test('damEnabled with content property containing contentReference', async () => {
211+
const ctRef = contentType({
212+
key: 'ctRef',
213+
baseType: '_component',
214+
properties: {
215+
image: { type: 'contentReference' },
216+
},
217+
});
218+
const ct1 = contentType({
219+
key: 'ct1',
220+
baseType: '_page',
221+
properties: {
222+
content: { type: 'content', allowedTypes: [ctRef] },
223+
},
224+
});
225+
initContentTypeRegistry([ct1, ctRef]);
226+
227+
// Test with damEnabled = true
228+
const result = await createFragment('ct1', new Set(), '', true, true);
229+
expect(result.some((line) => line.includes('ContentReferenceItem'))).toBe(
230+
true
231+
);
232+
expect(result).toMatchInlineSnapshot(`
233+
[
234+
"fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }",
235+
"fragment ItemMetadata on ItemMetadata { changeset displayOption }",
236+
"fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }",
237+
"fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }",
238+
"fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }",
239+
"fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }",
240+
"fragment PublicImageAsset on cmp_PublicImageAsset { Url Title AltText Description Renditions { Id Name Url Width Height } FocalPoint { X Y } Tags { Guid Name } }",
241+
"fragment PublicVideoAsset on cmp_PublicVideoAsset { Url Title AltText Description Renditions { Id Name Url Width Height } Tags { Guid Name } }",
242+
"fragment PublicRawFileAsset on cmp_PublicRawFileAsset { Url Title Description Tags { Guid Name } }",
243+
"fragment ContentReferenceItem on ContentReference { item { ...PublicImageAsset ...PublicVideoAsset ...PublicRawFileAsset } }",
244+
"fragment ctRef on ctRef { __typename image { key url { ...ContentUrl } ...ContentReferenceItem } ..._IContent }",
245+
"fragment ct1 on ct1 { __typename ct1__content:content { __typename ...ctRef } ..._IContent }",
246+
]
247+
`);
248+
});
249+
});
250+
251+
describe('createSingleContentQuery() with damEnabled', () => {
252+
test('damEnabled = false should not include DAM fragments', async () => {
253+
const ct1 = contentType({
254+
key: 'ct1',
255+
baseType: '_page',
256+
properties: {
257+
image: { type: 'contentReference' },
258+
},
259+
});
260+
initContentTypeRegistry([ct1]);
261+
262+
const query = await createSingleContentQuery('ct1', false);
263+
264+
expect(query.includes('PublicImageAsset')).toBe(false);
265+
expect(query.includes('ContentReferenceItem')).toBe(false);
266+
expect(query).toContain('image { key url { ...ContentUrl } }');
267+
});
268+
269+
test('damEnabled = true should include DAM fragments', async () => {
270+
const ct1 = contentType({
271+
key: 'ct1',
272+
baseType: '_page',
273+
properties: {
274+
image: { type: 'contentReference' },
275+
},
276+
});
277+
initContentTypeRegistry([ct1]);
278+
279+
const query = await createSingleContentQuery('ct1', true);
280+
281+
expect(query.includes('PublicImageAsset')).toBe(true);
282+
expect(query.includes('PublicVideoAsset')).toBe(true);
283+
expect(query.includes('PublicRawFileAsset')).toBe(true);
284+
expect(query.includes('ContentReferenceItem')).toBe(true);
285+
expect(query).toContain(
286+
'image { key url { ...ContentUrl } ...ContentReferenceItem }'
287+
);
288+
});
289+
});
290+
291+
describe('createMultipleContentQuery() with damEnabled', () => {
292+
test('damEnabled = false should not include DAM fragments', async () => {
293+
const ct1 = contentType({
294+
key: 'ct1',
295+
baseType: '_page',
296+
properties: {
297+
image: { type: 'contentReference' },
298+
},
299+
});
300+
initContentTypeRegistry([ct1]);
301+
302+
const query = await createMultipleContentQuery('ct1', false);
303+
304+
expect(query.includes('PublicImageAsset')).toBe(false);
305+
expect(query.includes('ContentReferenceItem')).toBe(false);
306+
});
307+
308+
test('damEnabled = true should include DAM fragments', async () => {
309+
const ct1 = contentType({
310+
key: 'ct1',
311+
baseType: '_page',
312+
properties: {
313+
image: { type: 'contentReference' },
314+
},
315+
});
316+
initContentTypeRegistry([ct1]);
317+
318+
const query = await createMultipleContentQuery('ct1', true);
319+
320+
expect(query.includes('PublicImageAsset')).toBe(true);
321+
expect(query.includes('PublicVideoAsset')).toBe(true);
322+
expect(query.includes('PublicRawFileAsset')).toBe(true);
323+
expect(query.includes('ContentReferenceItem')).toBe(true);
324+
});
325+
});

0 commit comments

Comments
 (0)