|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { contentType, initContentTypeRegistry } from '../../model/index.js'; |
| 3 | +import { createFragment } from '../createQuery.js'; |
| 4 | + |
| 5 | +describe('createFragment() creates types for base types', () => { |
| 6 | + const ctImage = contentType({ |
| 7 | + key: 'ctImage', |
| 8 | + baseType: '_image', |
| 9 | + properties: { p1: { type: 'string' } }, |
| 10 | + }); |
| 11 | + const ctVideo = contentType({ |
| 12 | + key: 'ctVideo', |
| 13 | + baseType: '_video', |
| 14 | + properties: { p1: { type: 'boolean' } }, |
| 15 | + }); |
| 16 | + const ctMedia = contentType({ |
| 17 | + key: 'ctMedia', |
| 18 | + baseType: '_media', |
| 19 | + properties: { p1: { type: 'float' } }, |
| 20 | + }); |
| 21 | + |
| 22 | + test('should not create extra fragments when referred explicitly', async () => { |
| 23 | + const ctPage = contentType({ |
| 24 | + key: 'ctPage', |
| 25 | + baseType: '_page', |
| 26 | + properties: { |
| 27 | + p1: { type: 'content', allowedTypes: [ctImage, ctVideo, ctMedia] }, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + initContentTypeRegistry([ctImage, ctVideo, ctMedia, ctPage]); |
| 32 | + |
| 33 | + const result = await createFragment('ctPage'); |
| 34 | + expect(result).toMatchInlineSnapshot(` |
| 35 | + [ |
| 36 | + "fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }", |
| 37 | + "fragment ItemMetadata on ItemMetadata { changeset displayOption }", |
| 38 | + "fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }", |
| 39 | + "fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }", |
| 40 | + "fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }", |
| 41 | + "fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }", |
| 42 | + "fragment ctImage on ctImage { __typename ctImage__p1:p1 ..._IContent }", |
| 43 | + "fragment ctVideo on ctVideo { __typename ctVideo__p1:p1 ..._IContent }", |
| 44 | + "fragment ctMedia on ctMedia { __typename ctMedia__p1:p1 ..._IContent }", |
| 45 | + "fragment ctPage on ctPage { __typename ctPage__p1:p1 { __typename ...ctImage ...ctVideo ...ctMedia } ..._IContent }", |
| 46 | + ] |
| 47 | + `); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should create base types when referred by base type', async () => { |
| 51 | + const ctPage = contentType({ |
| 52 | + key: 'ctPage', |
| 53 | + baseType: '_page', |
| 54 | + properties: { |
| 55 | + p1: { type: 'content', allowedTypes: ['_image', '_video', '_media'] }, |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + initContentTypeRegistry([ctImage, ctVideo, ctMedia, ctPage]); |
| 60 | + |
| 61 | + const result = await createFragment('ctPage'); |
| 62 | + expect(result).toMatchInlineSnapshot(` |
| 63 | + [ |
| 64 | + "fragment MediaMetadata on MediaMetadata { mimeType thumbnail content }", |
| 65 | + "fragment ItemMetadata on ItemMetadata { changeset displayOption }", |
| 66 | + "fragment InstanceMetadata on InstanceMetadata { changeset locales expired container owner routeSegment lastModifiedBy path createdBy }", |
| 67 | + "fragment ContentUrl on ContentUrl { type default hierarchical internal graph base }", |
| 68 | + "fragment IContentMetadata on IContentMetadata { key locale fallbackForLocale version displayName url {...ContentUrl} types published status created lastModified sortOrder variation ...MediaMetadata ...ItemMetadata ...InstanceMetadata }", |
| 69 | + "fragment _IContent on _IContent { _id _metadata {...IContentMetadata} }", |
| 70 | + "fragment ctImage on ctImage { __typename ctImage__p1:p1 ..._IContent }", |
| 71 | + "fragment _image on _Image { __typename ..._IContent }", |
| 72 | + "fragment ctVideo on ctVideo { __typename ctVideo__p1:p1 ..._IContent }", |
| 73 | + "fragment _video on _Video { __typename ..._IContent }", |
| 74 | + "fragment ctMedia on ctMedia { __typename ctMedia__p1:p1 ..._IContent }", |
| 75 | + "fragment _media on _Media { __typename ..._IContent }", |
| 76 | + "fragment ctPage on ctPage { __typename ctPage__p1:p1 { __typename ...ctImage ..._image ...ctVideo ..._video ...ctMedia ..._media } ..._IContent }", |
| 77 | + ] |
| 78 | + `); |
| 79 | + }); |
| 80 | +}); |
0 commit comments