Skip to content

Commit 9ceb5bd

Browse files
committed
feat: Add locale instance methods
1 parent a5b4634 commit 9ceb5bd

File tree

6 files changed

+128
-21
lines changed

6 files changed

+128
-21
lines changed

lib/create-space-api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export default function createSpaceApi ({
321321
*/
322322
function getLocale (id) {
323323
return http.get('locales/' + id)
324-
.then((response) => wrapLocale(response.data), errorHandler)
324+
.then((response) => wrapLocale(http, response.data), errorHandler)
325325
}
326326

327327
/**
@@ -334,7 +334,7 @@ export default function createSpaceApi ({
334334
*/
335335
function getLocales () {
336336
return http.get('locales')
337-
.then((response) => wrapLocaleCollection(response.data), errorHandler)
337+
.then((response) => wrapLocaleCollection(http, response.data), errorHandler)
338338
}
339339

340340
/**
@@ -344,7 +344,7 @@ export default function createSpaceApi ({
344344
* @param {object} data - Object representation of the Locale to be created
345345
* @return {Promise<Entities.Locale>} Promise for the newly created Locale
346346
* @example
347-
* space.createLocale({fields: {}})
347+
* space.createLocale({name: 'German (Germany)', code: 'de-DE'})
348348
* .then(e => console.log(e))
349349
*/
350350
function createLocale (data) {

lib/entities/entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function createEntryApi (http) {
8989
* @func update
9090
* @return {Promise<Entry>} Object returned from the server with updated changes.
9191
* @example
92-
* contentType.fields.name['en-US'] = 'Blog Post'
92+
* entry.fields.name['en-US'] = 'Blog Post'
9393
* entry.update()
9494
* .then(entry => console.log(entry.fields.name['en-US']))
9595
*/

lib/entities/locale.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {cloneDeep} from 'lodash/lang'
22
import freezeSys from 'contentful-sdk-core/freeze-sys'
33
import enhanceWithMethods from '../enhance-with-methods'
44
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
5-
6-
// Methods to add:
7-
// - update
8-
// - delete
5+
import {
6+
createUpdateEntity,
7+
createDeleteEntity
8+
} from '../instance-actions'
99

1010
/**
1111
* @memberof Entities
@@ -20,7 +20,44 @@ import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
2020
*/
2121

2222
function createLocaleApi (http) {
23-
return {}
23+
return {
24+
/**
25+
* Sends an update to the server with any changes made to the object's properties
26+
* @memberof Locale
27+
* @func update
28+
* @return {Promise<Locale>} Object returned from the server with updated changes.
29+
* @example
30+
* locale.name = 'English'
31+
* locale.update()
32+
* .then(locale => console.log(locale.name))
33+
*/
34+
update: function () {
35+
// delete some properties which are returned by the server but shouldn't
36+
// be sent back.
37+
const locale = this
38+
delete locale.default
39+
delete locale.fallback_code
40+
return createUpdateEntity({
41+
http: http,
42+
entityPath: 'locales',
43+
wrapperMethod: wrapLocale
44+
}).call(locale)
45+
},
46+
47+
/**
48+
* Deletes this object on the server.
49+
* @memberof Locale
50+
* @func delete
51+
* @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
52+
* @example
53+
* locale.delete()
54+
* .catch(err => console.log(err))
55+
*/
56+
delete: createDeleteEntity({
57+
http: http,
58+
entityPath: 'locales'
59+
})
60+
}
2461
}
2562

2663
/**
@@ -31,6 +68,7 @@ function createLocaleApi (http) {
3168
*/
3269
export function wrapLocale (http, data) {
3370
const locale = mixinToPlainObject(cloneDeep(data))
71+
delete locale.internal_code
3472
enhanceWithMethods(locale, createLocaleApi(http))
3573
return freezeSys(locale)
3674
}

test/integration/locale.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default function localeTests (t, space) {
2+
t.test('Gets locales', (t) => {
3+
t.plan(2)
4+
return space.getLocales()
5+
.then((response) => {
6+
t.ok(response.items[0].name, 'English')
7+
t.ok(response.items[0].code, 'en-US')
8+
})
9+
})
10+
11+
t.test('Creates, gets, updates and deletes a locale', (t) => {
12+
t.plan(3)
13+
return space.createLocale({
14+
name: 'German (Austria)',
15+
code: 'de-AT'
16+
})
17+
.then((response) => {
18+
t.equals(response.code, 'de-AT', 'locale code after creation')
19+
return new Promise((resolve) => {
20+
setTimeout(() => {
21+
resolve(space.getLocale(response.sys.id)
22+
.then((locale) => {
23+
t.equals(locale.code, 'de-AT', 'locale code after getting')
24+
locale.name = 'Deutsch (Österreich)'
25+
return locale.update()
26+
.then((updatedLocale) => {
27+
t.equals(updatedLocale.name, 'Deutsch (Österreich)', 'locale name after update')
28+
return updatedLocale.delete()
29+
})
30+
}))
31+
}, 3000)
32+
})
33+
})
34+
})
35+
}

test/integration/tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'blue-tape'
2+
import localeTests from './locale'
23
import contentTypeTests from './content-type'
34
import entryTests from './entry'
45
import assetTests from './asset'
@@ -72,6 +73,7 @@ test.only('Gets space with entities', (t) => {
7273
// in case someone forgets to comment this line again.
7374
// client.getSpace("wolk5fnyla9z")
7475
.then((space) => {
76+
localeTests(t, space)
7577
contentTypeTests(t, space)
7678
entryTests(t, space)
7779
assetTests(t, space)

test/unit/entities/locale-test.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
11
import test from 'tape'
2-
import {localeMock, mockCollection} from '../mocks/entities'
2+
import {cloneMock} from '../mocks/entities'
33
import setupHttpMock from '../mocks/http'
44
import {wrapLocale, wrapLocaleCollection} from '../../../lib/entities/locale'
5+
import {
6+
entityWrappedTest,
7+
entityCollectionWrappedTest,
8+
entityUpdateTest,
9+
entityDeleteTest,
10+
failingActionTest,
11+
failingVersionActionTest
12+
} from '../test-creators/instance-entity-methods'
513

6-
function setup () {
14+
function setup (promise) {
715
return {
8-
httpMock: setupHttpMock()
16+
httpMock: setupHttpMock(promise),
17+
entityMock: cloneMock('locale')
918
}
1019
}
1120

1221
test('Locale is wrapped', (t) => {
13-
const {httpMock} = setup()
14-
const wrappedLocale = wrapLocale(httpMock, localeMock)
15-
t.looseEqual(wrappedLocale.toPlainObject(), localeMock)
16-
t.end()
22+
entityWrappedTest(t, setup, {
23+
wrapperMethod: wrapLocale
24+
})
1725
})
1826

1927
test('Locale collection is wrapped', (t) => {
20-
const {httpMock} = setup()
21-
const localeCollection = mockCollection(localeMock)
22-
const wrappedLocale = wrapLocaleCollection(httpMock, localeCollection)
23-
t.looseEqual(wrappedLocale.toPlainObject(), localeCollection)
24-
t.end()
28+
return entityCollectionWrappedTest(t, setup, {
29+
wrapperMethod: wrapLocaleCollection
30+
})
31+
})
32+
33+
test('Locale update', (t) => {
34+
return entityUpdateTest(t, setup, {
35+
wrapperMethod: wrapLocale
36+
})
37+
})
38+
39+
test('Locale update fails', (t) => {
40+
return failingVersionActionTest(t, setup, {
41+
wrapperMethod: wrapLocale,
42+
actionMethod: 'update'
43+
})
44+
})
45+
46+
test('Locale delete', (t) => {
47+
return entityDeleteTest(t, setup, {
48+
wrapperMethod: wrapLocale
49+
})
50+
})
51+
52+
test('Locale delete fails', (t) => {
53+
return failingActionTest(t, setup, {
54+
wrapperMethod: wrapLocale,
55+
actionMethod: 'delete'
56+
})
2557
})

0 commit comments

Comments
 (0)