Skip to content

Commit b114af6

Browse files
committed
feat: Add getControlForField for EditorInterface
now you can get the representation for a specific field from a contentType editor interface
1 parent b7bc01b commit b114af6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

lib/entities/editor-interface.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ import errorHandler from '../error-handler'
1212

1313
function createEditorInterfaceApi (http) {
1414
return {
15+
/**
16+
* Sends an update to the server with any changes made to the object's properties
17+
* @memberof EditorInterface
18+
* @func update
19+
* @return {Promise<EditorInterface>} Object returned from the server with updated changes.
20+
* @example
21+
* editorInterface.controls[0] = { "fieldId": "title", "widgetId": "singleLine"}
22+
* editorInterface.update()
23+
* .then(editorInterface => console.log(editorInterface.controls))
24+
*/
1525
update: function () {
1626
const raw = this.toPlainObject()
1727
const data = omit(raw, ['sys'])
@@ -26,6 +36,21 @@ function createEditorInterfaceApi (http) {
2636
}
2737
)
2838
.then((response) => wrapEditorInterface(http, response.data), errorHandler)
39+
},
40+
/**
41+
* gets a control for a specific field
42+
* @memberof EditorInterface
43+
* @func getControlForField
44+
* @return {Object} control object for specific field.
45+
* @example
46+
* const control = editorInterface.getControlForField('fieldId')
47+
* console.log(control)
48+
*/
49+
getControlForField: function (fieldId) {
50+
const result = this.controls.filter((control) => {
51+
return control.fieldId === fieldId
52+
})
53+
return (result && result.length > 0) ? result[0] : null
2954
}
3055
}
3156
}

test/unit/entities/editor-interface-test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {cloneMock} from '../mocks/entities'
33
import setupHttpMock from '../mocks/http'
44
import {wrapEditorInterface} from '../../../lib/entities/editor-interface'
55
import {
6-
entityWrappedTest
6+
entityWrappedTest,
7+
entityUpdateTest
78
} from '../test-creators/instance-entity-methods'
89

910
function setup (promise) {
@@ -18,3 +19,29 @@ test('Editor Interface is wrapped', (t) => {
1819
wrapperMethod: wrapEditorInterface
1920
})
2021
})
22+
23+
test('ContentType update', (t) => {
24+
return entityUpdateTest(t, setup, {
25+
wrapperMethod: wrapEditorInterface
26+
})
27+
})
28+
29+
test('EditorInterface getControlForField with an existing fieldId', (t) => {
30+
t.plan(5)
31+
const {httpMock, entityMock} = setup()
32+
const editorInterface = wrapEditorInterface(httpMock, entityMock)
33+
const control = editorInterface.getControlForField('fieldId')
34+
t.ok(control, 'control object sould be there')
35+
t.ok(control.fieldId, 'should have a fieldId')
36+
t.ok(control.widgetId, 'should have a widgetId')
37+
t.equals(control.fieldId, 'fieldId')
38+
t.equals(control.widgetId, 'singleLine')
39+
})
40+
41+
test('EditorInterface getControlForField without an existing fieldId', (t) => {
42+
t.plan(1)
43+
const {httpMock, entityMock} = setup()
44+
const editorInterface = wrapEditorInterface(httpMock, entityMock)
45+
const control = editorInterface.getControlForField('notThere')
46+
t.equals(control, null)
47+
})

test/unit/mocks/entities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const editorInterfaceMock = {
6060
}),
6161
controls: [
6262
{
63-
'fieldId': 'fieldid',
63+
'fieldId': 'fieldId',
6464
'widgetId': 'singleLine'
6565
}
6666
]

0 commit comments

Comments
 (0)