Skip to content

Commit

Permalink
fix(link-getter): resolve links with locale:* (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaledgarbaya authored Jun 7, 2017
1 parent e83ebf6 commit 6cf1e83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
14 changes: 1 addition & 13 deletions lib/mixins/link-getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import get from 'lodash/get'
import partial from 'lodash/partial'
import memoize from 'lodash/memoize'

let resolveAllLocales = false
/**
* Sets getters on links for a given response
* @private
* @param {Array<Entry|Asset|DeletedEntry|DeletedAsset>} items
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset
*/
export default function mixinLinkGetters (items, includes, resolveForAllLocales) {
resolveAllLocales = resolveForAllLocales
export default function mixinLinkGetters (items, includes) {
const linkGetter = memoize(getLinksFromIncludes, memoizationResolver)
each(items, (item) => {
// TODO: workaround the preview endpoint extra locale this should be removed when
// it is fixed on the backend
if (resolveForAllLocales && item.sys.locale) {
delete item.sys.locale
}
setLocalizedFieldGetters(item.fields, !!item.sys.locale)
})

Expand Down Expand Up @@ -100,11 +93,6 @@ export default function mixinLinkGetters (items, includes, resolveForAllLocales)
function getLinksFromIncludes (field) {
var link = find(includes[field.sys.linkType], ['sys.id', field.sys.id])
if (link && link.fields) {
// TODO: workaround the preview endpoint extra locale this should be removed when
// it is fixed on the backend
if (resolveAllLocales && link.sys.locale) {
delete link.sys.locale
}
setLocalizedFieldGetters(link.fields, !!link.sys.locale)
return link
}
Expand Down
19 changes: 0 additions & 19 deletions test/integration/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const params = {
accessToken: 'b4c0n73n7fu1',
space: 'cfexampleapi'
}
const previewParams = {
host: 'preview.contentful.com',
accessToken: 'e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50',
space: 'cfexampleapi'
}
const localeSpaceParams = {
accessToken: 'da1dc0e316213fe11e6139d3cd02f853b12da3f3fd0b4f146a1613a9cca277cd',
space: '7dh3w86is8ls'
Expand All @@ -23,7 +18,6 @@ if (process.env.API_INTEGRATION_TESTS) {
}

const client = contentful.createClient(params)
const previewClient = contentful.createClient(previewParams)
const localeClient = contentful.createClient(localeSpaceParams)

test('Gets space', (t) => {
Expand Down Expand Up @@ -153,19 +147,6 @@ test('Gets entries with linked includes', (t) => {
})
})

test('Gets entries with linked includes with preview', (t) => {
t.plan(5)
return previewClient.getEntries({locale: '*', include: 5, 'sys.id': 'nyancat'})
.then((response) => {
t.ok(response.includes, 'includes')
t.ok(response.includes.Asset, 'includes for Assets from preview endpoint')
t.ok(Object.keys(response.includes.Asset).length > 0, 'list of includes has asset items from preview endpoint')
// testing the fiels needs to be done first because it will call the getters and fill in bestFriend['en-US'].sys.type
t.ok(response.items[0].fields.bestFriend['en-US'].fields, 'resolved entry has fields from preview endpoint')
t.equal(response.items[0].fields.bestFriend['en-US'].sys.type, 'Entry', 'entry gets resolved from other entries in collection from preview endpoint')
})
})

test('Gets entries with content type query param', (t) => {
t.plan(2)
return client.getEntries({content_type: 'cat'})
Expand Down
27 changes: 27 additions & 0 deletions test/unit/mixins/link-getters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,30 @@ test('links in response, with circular references', (t) => {
t.equals(items[0].fields.animal.fields.friend.fields.friend.sys.id, 'oink', 'sub sub link id')
t.end()
})

test('links in response with locale: *', (t) => {
const items = [
{
sys: {type: 'Entry'},
fields: {
animal: { 'en': {sys: {type: 'Link', linkType: 'Entry', id: 'oink'}} },
animals: { 'en': [{sys: {type: 'Link', linkType: 'Entry', id: 'oink'}}] }
}
}
]
const includes = {
Entry: [
{
sys: {type: 'Entry', id: 'oink'},
fields: {
name: {
'en': 'Pig'
}
}
}
]
}
mixinLinkGetters(items, includes)
t.equals(items[0].fields.animal['en'].fields.name['en'], includes.Entry[0].fields.name['en'])
t.end()
})

0 comments on commit 6cf1e83

Please sign in to comment.