Skip to content

Commit f56304b

Browse files
committed
feat: add get entry csm tests
1 parent 6c6090e commit f56304b

8 files changed

+210
-15
lines changed

lib/create-contentful-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ export default function createContentfulApi<OptionType extends ChainOptions>(
240240
const entries = await get({
241241
context: 'environment',
242242
path: 'entries',
243-
config: createRequestConfig({ query: normalizeSearchParameters(normalizeSelect(query)) }),
243+
config: createRequestConfig({
244+
query: maybeEnableSourceMaps(normalizeSearchParameters(normalizeSelect(query))),
245+
}),
244246
})
245247

246248
return resolveCircular(entries, {

lib/types/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { ChainModifiers } from './client'
12
import { ContentfulCollection } from './collection'
23
import { LocaleCode } from './locale'
34
import { Metadata } from './metadata'
45
import { EntitySys } from './sys'
5-
import { ChainModifiers } from './client'
66

77
/**
88
* @category Asset

lib/types/collection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { AssetSys } from './asset'
2+
import { EntrySys } from './entry'
3+
14
/**
25
* A wrapper object containing additional information for
36
* a collection of Contentful resources
@@ -9,4 +12,5 @@ export interface ContentfulCollection<T> {
912
skip: number
1013
limit: number
1114
items: Array<T>
15+
sys?: AssetSys | EntrySys
1216
}

test/integration/getAsset.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ describe('getAsset', () => {
2626
expect(typeof response.fields.title).toBe('object')
2727
})
2828

29-
test('preview client has alpha_withContentSourceMaps enabled', async () => {
29+
test('preview client has (alpha) withContentSourceMaps enabled', async () => {
3030
const response = await previewClient.getAsset(asset)
3131

3232
expect(response.fields).toBeDefined()
3333
expect(typeof response.fields.title).toBe('string')
3434
expect(response.sys.contentSourceMaps).toBeDefined()
35-
expect(response.sys.contentSourceMapsLookup).toBeDefined()
35+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
3636
})
3737

38-
test('preview client has alpha_withContentSourceMaps enabled + withAllLocales modifier', async () => {
38+
test('preview client has (alpha) withContentSourceMaps enabled + withAllLocales modifier', async () => {
3939
const response = await previewClient.withAllLocales.getAsset(asset)
4040

4141
expect(response.fields).toBeDefined()
4242
expect(typeof response.fields.title).toBe('object')
4343
expect(response.sys.contentSourceMaps).toBeDefined()
44-
expect(response.sys.contentSourceMapsLookup).toBeDefined()
44+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
4545
})
4646
})

test/integration/getAssets.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as contentful from '../../lib/contentful'
2-
import { params } from './utils'
2+
import { params, previewParamsWithCSM } from './utils'
33

44
if (process.env.API_INTEGRATION_TESTS) {
55
params.host = '127.0.0.1:5000'
66
params.insecure = true
77
}
88

99
const client = contentful.createClient(params)
10+
const previewClient = contentful.createClient(previewParamsWithCSM)
1011

1112
describe('getAssets', () => {
1213
test('default client', async () => {
@@ -32,4 +33,32 @@ describe('getAssets', () => {
3233
expect(typeof item.fields.title).toBe('object')
3334
})
3435
})
36+
37+
test('preview client has (alpha) withContentSourceMaps enabled', async () => {
38+
const response = await previewClient.getAssets()
39+
40+
expect(response.items).not.toHaveLength(0)
41+
42+
response.items.forEach((item) => {
43+
expect(item.sys.type).toEqual('Asset')
44+
expect(item.fields).toBeDefined()
45+
expect(typeof item.fields.title).toBe('string')
46+
})
47+
48+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
49+
})
50+
51+
test('preview client has (alpha) withContentSourceMaps enabled withAllLocales modifier', async () => {
52+
const response = await previewClient.withAllLocales.getAssets()
53+
54+
expect(response.items).not.toHaveLength(0)
55+
56+
response.items.forEach((item) => {
57+
expect(item.sys.type).toEqual('Asset')
58+
expect(item.fields).toBeDefined()
59+
expect(typeof item.fields.title).toBe('object')
60+
})
61+
62+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
63+
})
3564
})

test/integration/getEntries.test.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { EntryFieldTypes, EntrySkeletonType } from '../../lib'
22
import * as contentful from '../../lib/contentful'
3-
import { params, previewParams } from './utils'
3+
import { params, previewParamsWithCSM } from './utils'
44

55
if (process.env.API_INTEGRATION_TESTS) {
66
params.host = '127.0.0.1:5000'
77
params.insecure = true
88
}
99

1010
const client = contentful.createClient(params)
11-
const previewClient = contentful.createClient(previewParams)
11+
const previewClient = contentful.createClient(previewParamsWithCSM)
1212

1313
describe('getEntries via client chain modifiers', () => {
1414
const entryWithUnresolvableLink = '4SEhTg8sYJ1H3wDAinzhTp'
@@ -370,6 +370,54 @@ describe('getEntries via client chain modifiers', () => {
370370
})
371371
})
372372

373+
describe('preview client with (alpha) withContentSourceMaps enabled', () => {
374+
test('with no modifer', async () => {
375+
const response = await previewClient.getEntries({
376+
include: 5,
377+
'sys.id': entryWithResolvableLink,
378+
})
379+
380+
assertCSMEntriesResponse(response)
381+
})
382+
383+
test('withAllLocales modifier', async () => {
384+
const response = await previewClient.withAllLocales.getEntries({
385+
include: 5,
386+
'sys.id': entryWithResolvableLink,
387+
})
388+
389+
assertLocalizedEntriesResponse(response)
390+
expect(response?.sys?.contentSourceMapsLookup).toBeDefined()
391+
})
392+
393+
test('withAllLocales.withoutLinkResolution', async () => {
394+
const response = await previewClient.withAllLocales.withoutLinkResolution.getEntries<
395+
EntrySkeletonType<{
396+
bestFriend: EntryFieldTypes.EntryLink<EntrySkeletonType>
397+
color: EntryFieldTypes.Symbol
398+
}>
399+
>({
400+
'sys.id': entryWithResolvableLink,
401+
include: 2,
402+
})
403+
expect(response.items[0].fields).toBeDefined()
404+
expect(response.items[0].fields.color).toHaveProperty('en-US')
405+
expect(response.items[0].fields.bestFriend).toHaveProperty('[en-US].sys.type', 'Link')
406+
})
407+
408+
test('withAllLocales.withoutUnresolvableLinks', async () => {
409+
const response = await previewClient.withAllLocales.withoutUnresolvableLinks.getEntries({
410+
'sys.id': entryWithUnresolvableLink,
411+
include: 2,
412+
})
413+
414+
expect(response.items[0].fields).toBeDefined()
415+
expect(response.items[0].fields.name).toHaveProperty('en-US')
416+
expect(response.items[0].fields.color).toHaveProperty('en-US')
417+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
418+
})
419+
})
420+
373421
describe('client has withoutLinkResolution modifier', () => {
374422
test('client.withoutLinkResolution', async () => {
375423
const response = await client.withoutLinkResolution.getEntries<
@@ -410,7 +458,21 @@ function assertLocalizedEntriesResponse(response) {
410458
expect(response.includes!.Asset).toBeDefined()
411459
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
412460
expect(Object.keys(response.includes!.Asset!).length).toBeGreaterThan(0)
461+
413462
expect(response.items[0].fields.bestFriend['en-US'].fields).toBeDefined()
414463
expect(response.items[0].fields.bestFriend['en-US'].sys.type).toBe('Entry')
415464
expect(response.items[0].metadata).toEqual({ tags: [] })
416465
}
466+
467+
function assertCSMEntriesResponse(response) {
468+
expect(response.includes).toBeDefined()
469+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
470+
expect(response.includes!.Asset).toBeDefined()
471+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
472+
expect(Object.keys(response.includes!.Asset!).length).toBeGreaterThan(0)
473+
474+
expect(response.items[0].fields.bestFriend.fields).toBeDefined()
475+
expect(response.items[0].fields.bestFriend.sys.type).toBe('Entry')
476+
expect(response.items[0].metadata).toEqual({ tags: [] })
477+
expect(response.sys?.contentSourceMapsLookup).toBeDefined()
478+
}

test/integration/getEntry.test.ts

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
1-
import * as contentful from '../../lib/contentful'
2-
import { localeSpaceParams, params, previewParams } from './utils'
31
import { EntryFields, EntrySkeletonType } from '../../lib'
2+
import * as contentful from '../../lib/contentful'
3+
import { localeSpaceParams, params, previewParamsWithCSM } from './utils'
44

55
if (process.env.API_INTEGRATION_TESTS) {
66
params.host = '127.0.0.1:5000'
77
params.insecure = true
88
}
99

1010
const client = contentful.createClient(params)
11-
const previewClient = contentful.createClient(previewParams)
11+
const previewClient = contentful.createClient(previewParamsWithCSM)
1212
const localeClient = contentful.createClient(localeSpaceParams)
1313

1414
describe('getEntry via client chain modifiers', () => {
1515
const entryWithUnresolvableLink = '4SEhTg8sYJ1H3wDAinzhTp'
1616
const entryWithResolvableLink = 'nyancat'
17-
17+
const exampleCsm = {
18+
sys: {
19+
type: 'ContentSourceMaps',
20+
},
21+
mappings: {
22+
'/fields/name': {
23+
source: {
24+
editorInterface: 0,
25+
fieldType: 0,
26+
},
27+
},
28+
'/fields/likes': {
29+
source: {
30+
editorInterface: 1,
31+
fieldType: 1,
32+
},
33+
},
34+
'/fields/color': {
35+
source: {
36+
editorInterface: 2,
37+
fieldType: 2,
38+
},
39+
},
40+
},
41+
}
1842
describe('default client', () => {
1943
test('Gets an entry with the correct ID', async () => {
2044
const response = await client.getEntry(entryWithResolvableLink, {
@@ -148,6 +172,79 @@ describe('getEntry via client chain modifiers', () => {
148172
})
149173
})
150174

175+
describe('preview client has (alpha) withContentSourceMaps enabled', () => {
176+
test('default', async () => {
177+
const response = await previewClient.getEntry(entryWithResolvableLink, {
178+
include: 2,
179+
})
180+
181+
expect(response.sys.contentSourceMaps).toBeDefined()
182+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
183+
})
184+
185+
test('previewClient.withoutLinkResolution', async () => {
186+
const response = await previewClient.withoutLinkResolution.getEntry(entryWithResolvableLink, {
187+
include: 2,
188+
})
189+
190+
expect(response.sys.contentSourceMaps).toBeDefined()
191+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
192+
expect(response.fields.bestFriend).toHaveProperty('sys.type', 'Link')
193+
})
194+
195+
test('previewClient.withoutUnresolvableLinks', async () => {
196+
const response = await previewClient.withoutUnresolvableLinks.getEntry(
197+
entryWithUnresolvableLink,
198+
{
199+
include: 2,
200+
},
201+
)
202+
203+
expect(response.fields.name).toBeDefined()
204+
expect(response.fields.bestFriend).toHaveProperty('sys.type', 'Entry')
205+
expect(response.sys.contentSourceMaps).toBeDefined()
206+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
207+
})
208+
209+
test('previewClient.withAllLocales', async () => {
210+
const response = await previewClient.withAllLocales.getEntry(entryWithResolvableLink, {
211+
include: 2,
212+
})
213+
214+
expect(response.fields.color).toHaveProperty('en-US')
215+
expect(response.fields.bestFriend).toHaveProperty('[en-US].sys.type', 'Entry')
216+
expect(response.sys.contentSourceMaps).toBeDefined()
217+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
218+
})
219+
220+
test('previewClient.withAllLocales.withoutLinkResolution', async () => {
221+
const response = await previewClient.withAllLocales.withoutLinkResolution.getEntry(
222+
entryWithResolvableLink,
223+
{
224+
include: 2,
225+
},
226+
)
227+
228+
expect(response.fields.color).toHaveProperty('en-US')
229+
expect(response.fields.bestFriend).toHaveProperty('[en-US].sys.type', 'Link')
230+
expect(response.sys.contentSourceMaps).toBeDefined()
231+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
232+
})
233+
234+
test('previewClient.withAllLocales.withoutUnresolvableLinks', async () => {
235+
const response = await previewClient.withAllLocales.withoutUnresolvableLinks.getEntry(
236+
entryWithUnresolvableLink,
237+
{
238+
include: 2,
239+
},
240+
)
241+
242+
expect(response.fields.name).toHaveProperty('en-US')
243+
expect(response.sys.contentSourceMaps).toBeDefined()
244+
expect(response.sys.contentSourceMaps).toMatchObject(exampleCsm)
245+
})
246+
})
247+
151248
describe('client has withoutLinkResolution modifier', () => {
152249
test('client.withoutLinkResolution', async () => {
153250
const response = await client.withoutLinkResolution.getEntry(entryWithResolvableLink)
@@ -167,6 +264,7 @@ describe('getEntry via client chain modifiers', () => {
167264
const response = await client.withoutLinkResolution.getEntry(entryWithUnresolvableLink, {
168265
include: 2,
169266
})
267+
170268
expect(response.fields).toBeDefined()
171269
expect(response.fields.bestFriend).toMatchObject({
172270
sys: {

test/unit/make-contentful-api-client-parseentries.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { makeClient } from '../../lib/make-client'
21
import { createGlobalOptions } from '../../lib/create-global-options'
3-
import { EntrySys, EntryCollection, EntrySkeletonType, EntryFieldTypes } from '../../lib/types'
2+
import { makeClient } from '../../lib/make-client'
3+
import { EntryCollection, EntryFieldTypes, EntrySkeletonType, EntrySys } from '../../lib/types'
44

55
interface AnimalTypeFields {
66
animal?: EntryFieldTypes.EntryLink<EntrySkeletonType>

0 commit comments

Comments
 (0)