Skip to content

Commit

Permalink
fix: Freeze only sys on returned objects
Browse files Browse the repository at this point in the history
  • Loading branch information
trodrigues committed May 11, 2016
1 parent 5978510 commit b27897f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/entities/asset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {cloneDeep} from 'lodash/lang'
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
import freezeSys from 'contentful-sdk-core/freeze-sys'

/**
* @memberof Entities
Expand All @@ -23,7 +24,7 @@ import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
* @return {Asset} Wrapped asset data
*/
export function wrapAsset (data) {
return Object.freeze(mixinToPlainObject(cloneDeep(data)))
return freezeSys(mixinToPlainObject(cloneDeep(data)))
}

/**
Expand All @@ -42,5 +43,5 @@ export function wrapAsset (data) {
* @return {AssetCollection} Wrapped asset collection data
*/
export function wrapAssetCollection (data) {
return Object.freeze(mixinToPlainObject(cloneDeep(data)))
return freezeSys(mixinToPlainObject(cloneDeep(data)))
}
5 changes: 3 additions & 2 deletions lib/entities/content-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {cloneDeep} from 'lodash/lang'
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
import freezeSys from 'contentful-sdk-core/freeze-sys'

/**
* @memberof Entities
Expand All @@ -18,7 +19,7 @@ import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
* @return {ContentType} Wrapped content type data
*/
export function wrapContentType (data) {
return Object.freeze(mixinToPlainObject(cloneDeep(data)))
return freezeSys(mixinToPlainObject(cloneDeep(data)))
}

/**
Expand All @@ -37,5 +38,5 @@ export function wrapContentType (data) {
* @return {ContentTypeCollection} Wrapped content type collection data
*/
export function wrapContentTypeCollection (data) {
return Object.freeze(mixinToPlainObject(cloneDeep(data)))
return freezeSys(mixinToPlainObject(cloneDeep(data)))
}
5 changes: 3 additions & 2 deletions lib/entities/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {cloneDeep} from 'lodash/lang'
import {uniq} from 'lodash/array'
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
import freezeSys from 'contentful-sdk-core/freeze-sys'
import mixinLinkGetters from '../mixins/link-getters'
import mixinStringifySafe from '../mixins/stringify-safe'

Expand Down Expand Up @@ -75,7 +76,7 @@ import mixinStringifySafe from '../mixins/stringify-safe'
* @return {Entry} Wrapped entry data
*/
export function wrapEntry (data) {
return Object.freeze(mixinToPlainObject(cloneDeep(data)))
return freezeSys(mixinToPlainObject(cloneDeep(data)))
}

/**
Expand Down Expand Up @@ -103,7 +104,7 @@ export function wrapEntryCollection (data, resolveLinks) {
const includes = prepareIncludes(wrappedData.includes, wrappedData.items)
mixinLinkGetters(wrappedData.items, includes)
}
return Object.freeze(wrappedData)
return freezeSys(wrappedData)
}

function prepareIncludes (includes = {}, items) {
Expand Down
3 changes: 2 additions & 1 deletion lib/entities/space.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
import freezeSys from 'contentful-sdk-core/freeze-sys'

/**
* @memberof Entities
Expand All @@ -17,5 +18,5 @@ import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
* @return {Space}
*/
export function wrapSpace (data) {
return Object.freeze(mixinToPlainObject(data))
return freezeSys(mixinToPlainObject(data))
}
3 changes: 2 additions & 1 deletion lib/paged-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {filter} from 'lodash/collection'
import {cloneDeep} from 'lodash/lang'
import createRequestConfig from 'contentful-sdk-core/create-request-config'
import freezeSys from 'contentful-sdk-core/freeze-sys'
import mixinLinkGetters from './mixins/link-getters'
import mixinStringifySafe from './mixins/stringify-safe'
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
Expand Down Expand Up @@ -69,7 +70,7 @@ export default function pagedSync (http, query, resolveLinks) {
// maps response items again after getters are attached
const mappedResponseItems = mapResponseItems(response.items)
mappedResponseItems.nextSyncToken = response.nextSyncToken
return Object.freeze(mixinStringifySafe(mixinToPlainObject(mappedResponseItems)))
return freezeSys(mixinStringifySafe(mixinToPlainObject(mappedResponseItems)))
}, (error) => {
throw error.data
})
Expand Down
12 changes: 12 additions & 0 deletions test/unit/contentful-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ test('Passes along HTTP client parameters', (t) => {
})

test('Returns a client instance', (t) => {
const createHttpClientStub = sinon.stub()
createClient.__Rewire__('createHttpClient', createHttpClientStub)
const client = createClient(axios, {accessToken: 'accesstoken', space: 'spaceid'})
t.ok(client.getSpace, 'getSpace')
t.ok(client.getEntry, 'getEntry')
Expand All @@ -38,31 +40,41 @@ test('Returns a client instance', (t) => {
t.ok(client.getContentTypes, 'getContentTypes')
t.ok(client.getAsset, 'getAsset')
t.ok(client.getAssets, 'getAssets')
createClient.__ResetDependency__('createHttpClient')
t.end()
})

test('Initializes API with link resolution turned on by default', (t) => {
const createHttpClientStub = sinon.stub()
createClient.__Rewire__('createHttpClient', createHttpClientStub)
const apiStub = sinon.stub().returns({})
createClient.__Rewire__('createContentfulApi', apiStub)
createClient(axios, {accessToken: 'accesstoken', space: 'spaceid'})
t.ok(apiStub.args[0][0].shouldLinksResolve({}), 'not overriden by query')
t.notOk(apiStub.args[0][0].shouldLinksResolve({resolveLinks: false}), 'overriden by query')
createClient.__ResetDependency__('createHttpClient')
t.end()
})

test('Initializes API with link resolution turned on explicitly', (t) => {
const createHttpClientStub = sinon.stub()
createClient.__Rewire__('createHttpClient', createHttpClientStub)
const apiStub = sinon.stub().returns({})
createClient.__Rewire__('createContentfulApi', apiStub)
createClient(axios, {accessToken: 'accesstoken', space: 'spaceid', resolveLinks: true})
t.ok(apiStub.args[0][0].shouldLinksResolve({}), 'not overriden by query')
t.notOk(apiStub.args[0][0].shouldLinksResolve({resolveLinks: false}), 'overriden by query')
createClient.__ResetDependency__('createHttpClient')
t.end()
})

test('Initializes API with link resolution turned off explicitly', (t) => {
const createHttpClientStub = sinon.stub()
createClient.__Rewire__('createHttpClient', createHttpClientStub)
const apiStub = sinon.stub().returns({})
createClient.__Rewire__('createContentfulApi', apiStub)
createClient(axios, {accessToken: 'accesstoken', space: 'spaceid', resolveLinks: false})
t.notOk(apiStub.args[0][0].resolveLinksGlobalSetting)
createClient.__ResetDependency__('createHttpClient')
t.end()
})

0 comments on commit b27897f

Please sign in to comment.