Skip to content

Commit

Permalink
Merge pull request #262 from hypermedia-app/default-iri
Browse files Browse the repository at this point in the history
fix: no default node for select editors
  • Loading branch information
tpluscode authored Nov 6, 2022
2 parents 07658ea + 86bd1fe commit e649150
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-suns-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/shaperone-core": patch
---

Do not create a new node when the editor is a `dash:*SelectEditor`
38 changes: 34 additions & 4 deletions packages/core-tests/models/forms/effects/addObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cf from 'clownface'
import $rdf from 'rdf-ext'
import { expect } from 'chai'
import { sinon } from '@shaperone/testing'
import { dash } from '@tpluscode/rdf-ns-builders'
import { dash } from '@tpluscode/rdf-ns-builders/loose'
import { testStore } from '@shaperone/testing/models/form.js'
import { addObject } from '@hydrofoil/shaperone-core/models/forms/effects/addObject.js'
import { Store } from '@hydrofoil/shaperone-core/state'
Expand All @@ -22,12 +22,12 @@ describe('models/forms/effects/addObject', () => {
// given
const property = propertyShape()
const focusNode = cf({ dataset: $rdf.dataset() }).blankNode()
const matchedEditors: SingleEditorMatch[] = [{
const editors: SingleEditorMatch[] = [{
term: dash.TextFieldEditor,
score: 5,
meta: <any> {},
}]
store.getState().editors.matchSingleEditors = () => matchedEditors
store.getState().editors.matchSingleEditors = () => editors

// when
addObject(store)({
Expand All @@ -42,7 +42,37 @@ describe('models/forms/effects/addObject', () => {
form,
property,
focusNode,
matchedEditors,
editors,
selectedEditor: dash.TextFieldEditor,
}))
})

it('takes property editor as default match', () => {
// given
const property = propertyShape({
editor: dash.FooEditor,
})
const focusNode = cf({ dataset: $rdf.dataset() }).blankNode()
store.getState().editors.matchSingleEditors = () => [{
term: dash.TextFieldEditor,
score: 5,
meta: <any> {},
}]

// when
addObject(store)({
form,
property,
focusNode,
})

// then
const dispatch = store.getDispatch()
expect(dispatch.forms.addFormField).to.have.been.calledWith(sinon.match({
form,
property,
focusNode,
selectedEditor: dash.FooEditor,
}))
})
})
34 changes: 19 additions & 15 deletions packages/core-tests/models/forms/reducers/addFormField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('core/models/forms/reducers/addObject', () => {
})
const focusNode = graph.node(ex.FocusNode)
const { form, store } = testStore()
const { editors, forms } = store.getState()
const { forms } = store.getState()

forms.get(form)!.focusNodes = testFocusNodeState(focusNode, {
properties: [testPropertyState(focusNode.blankNode(), {
Expand All @@ -38,8 +38,8 @@ describe('core/models/forms/reducers/addObject', () => {
form,
property,
focusNode,
matchedEditors: [],
editors,
editors: [],
selectedEditor: undefined,
})

// then
Expand All @@ -56,7 +56,7 @@ describe('core/models/forms/reducers/addObject', () => {
})
const focusNode = graph.node(ex.FocusNode)
const { form, store } = testStore()
const { editors, forms } = store.getState()
const { forms } = store.getState()

forms.get(form)!.focusNodes = testFocusNodeState(focusNode, {
properties: [testPropertyState(focusNode.blankNode(), {
Expand All @@ -74,8 +74,8 @@ describe('core/models/forms/reducers/addObject', () => {
form,
property,
focusNode,
matchedEditors: [],
editors,
editors: [],
selectedEditor: undefined,
})

// then
Expand All @@ -92,7 +92,7 @@ describe('core/models/forms/reducers/addObject', () => {
})
const focusNode = graph.node(ex.FocusNode)
const { form, store } = testStore()
const { editors, forms } = store.getState()
const { forms } = store.getState()

forms.get(form)!.focusNodes = testFocusNodeState(focusNode, {
properties: [testPropertyState(focusNode.blankNode(), {
Expand All @@ -110,8 +110,8 @@ describe('core/models/forms/reducers/addObject', () => {
form,
property,
focusNode,
matchedEditors: [],
editors,
editors: [],
selectedEditor: undefined,
})

// then
Expand All @@ -127,7 +127,7 @@ describe('core/models/forms/reducers/addObject', () => {
})
const focusNode = graph.node(ex.FocusNode)
const { form, store } = testStore()
const { editors, forms } = store.getState()
const { forms } = store.getState()

forms.get(form)!.focusNodes = testFocusNodeState(focusNode, {
properties: [testPropertyState(focusNode.blankNode(), {
Expand All @@ -145,8 +145,8 @@ describe('core/models/forms/reducers/addObject', () => {
form,
property,
focusNode,
matchedEditors: [],
editors,
editors: [],
selectedEditor: undefined,
})

// then
Expand All @@ -163,7 +163,7 @@ describe('core/models/forms/reducers/addObject', () => {
})
const focusNode = graph.node(ex.FocusNode)
const { form, store } = testStore()
const { editors, forms } = store.getState()
const { forms } = store.getState()

forms.get(form)!.focusNodes = testFocusNodeState(focusNode, {
properties: [testPropertyState(focusNode.blankNode(), {
Expand All @@ -181,12 +181,16 @@ describe('core/models/forms/reducers/addObject', () => {
form,
property,
focusNode,
matchedEditors: [{
editors: [{
term: dash.FooEditor,
score: 100,
meta: {} as any,
}, {
term: dash.TextFieldEditor,
score: 10,
meta: {} as any,
}],
editors,
selectedEditor: dash.FooEditor,
})

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('models/resources/effects/forms/addFormField', () => {
form,
focusNode,
property,
selectedEditor: undefined,
})

// then
Expand All @@ -64,6 +65,7 @@ describe('models/resources/effects/forms/addFormField', () => {
form,
focusNode,
property,
selectedEditor: undefined,
})

// then
Expand All @@ -88,6 +90,7 @@ describe('models/resources/effects/forms/addFormField', () => {
form,
focusNode,
property,
selectedEditor: undefined,
})

// then
Expand Down
55 changes: 43 additions & 12 deletions packages/core-tests/models/resources/lib/objectValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ describe('core/models/resources/lib/defaultValue', () => {
const property = propertyShape(graph.blankNode(), {
defaultValue: literal('foo', xsd.anySimpleType),
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer?.term).to.deep.eq(literal('foo', xsd.anySimpleType))
Expand All @@ -30,9 +31,10 @@ describe('core/models/resources/lib/defaultValue', () => {
const graph = cf({ dataset: $rdf.dataset() })
const property = propertyShape(graph.blankNode(), {
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer).to.be.null
Expand All @@ -44,9 +46,10 @@ describe('core/models/resources/lib/defaultValue', () => {
const property = propertyShape(graph.blankNode(), {
class: foaf.Person,
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer?.term?.termType).to.eq('BlankNode')
Expand All @@ -59,9 +62,10 @@ describe('core/models/resources/lib/defaultValue', () => {
const property = propertyShape(graph.blankNode(), {
nodeKind: sh.IRIOrLiteral,
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer).to.be.null
Expand All @@ -73,10 +77,11 @@ describe('core/models/resources/lib/defaultValue', () => {
const property = propertyShape(graph.blankNode(), {
nodeKind: sh.IRI,
})
const focusNode = graph.blankNode()

// when
const first = defaultValue(property, graph.blankNode())
const second = defaultValue(property, graph.blankNode())
const first = defaultValue({ property, focusNode })
const second = defaultValue({ property, focusNode })

// then
expect(first?.term).not.to.deep.eq(second)
Expand All @@ -89,9 +94,10 @@ describe('core/models/resources/lib/defaultValue', () => {
nodeKind: sh.IRI,
[sh1.iriPrefix.value]: 'http://example.com/foo/',
})
const focusNode = graph.blankNode()

// when
const term = defaultValue(property, graph.blankNode())?.term
const term = defaultValue({ property, focusNode })?.term

// then
expect(term?.termType).to.eq('NamedNode')
Expand All @@ -105,9 +111,10 @@ describe('core/models/resources/lib/defaultValue', () => {
nodeKind: sh.BlankNodeOrIRI,
[sh1.iriPrefix.value]: 'http://example.com/foo/',
})
const focusNode = graph.blankNode()

// when
const term = defaultValue(property, graph.blankNode())?.term
const term = defaultValue({ property, focusNode })?.term

// then
expect(term?.termType).to.eq('NamedNode')
Expand All @@ -121,9 +128,10 @@ describe('core/models/resources/lib/defaultValue', () => {
nodeKind: sh.IRIOrLiteral,
[sh1.iriPrefix.value]: 'http://example.com/foo/',
})
const focusNode = graph.blankNode()

// when
const term = defaultValue(property, graph.blankNode())?.term
const term = defaultValue({ property, focusNode })?.term

// then
expect(term?.termType).to.eq('NamedNode')
Expand All @@ -144,9 +152,10 @@ describe('core/models/resources/lib/defaultValue', () => {
const property = propertyShape(graph.blankNode(), {
nodeKind,
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer?.term?.termType).to.eq(termType)
Expand All @@ -160,9 +169,10 @@ describe('core/models/resources/lib/defaultValue', () => {
class: foaf.Agent,
[sh1.iriPrefix.value]: 'http://example.com/foo/',
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer?.out(rdf.type).term).to.deep.eq(foaf.Agent)
Expand All @@ -177,12 +187,33 @@ describe('core/models/resources/lib/defaultValue', () => {
[dash.editor.value]: dash.InstancesSelectEditor,
[sh1.iriPrefix.value]: 'http://example.com/foo/',
})
const focusNode = graph.blankNode()

// when
const pointer = defaultValue(property, graph.blankNode())
const pointer = defaultValue({ property, focusNode })

// then
expect(pointer?.out(rdf.type).term).to.be.undefined
})
})

const selectEditors = [
dash.EnumSelectEditor,
dash.InstancesSelectEditor,
dash.AutoCompleteEditor,
]

selectEditors.forEach((editor) => {
it(`does not create a node when editor is ${editor.value}`, () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
const property = propertyShape(graph.blankNode(), {
nodeKind: sh.IRI,
})
const focusNode = graph.blankNode()

// then
expect(defaultValue({ property, focusNode, editor })).to.be.null
})
})
})
20 changes: 18 additions & 2 deletions packages/core/models/forms/effects/addObject.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import { PropertyShape } from '@rdfine/shacl'
import { NamedNode } from 'rdf-js'
import type { Store } from '../../../state'
import { FocusNode } from '../../../index'
import { BaseParams } from '../../index'
import { SingleEditorMatch } from '../../editors'

export function addObject(store: Store) {
const dispatch = store.getDispatch()
return function ({ form, property, focusNode }: { focusNode: FocusNode; property: PropertyShape } & BaseParams) {
const { editors, resources } = store.getState()
const { editors: editorsState, resources } = store.getState()
const graph = resources.get(form)?.graph
if (!graph) {
return
}

const matchedEditors = editorsState.matchSingleEditors({ shape: property })
let editors: SingleEditorMatch[]
let selectedEditor: NamedNode | undefined
if (property.editor?.id.termType === 'NamedNode') {
selectedEditor = property.editor.id
editors = [
{ term: selectedEditor, score: null, meta: editorsState.metadata.node(selectedEditor) },
...matchedEditors,
]
} else {
editors = matchedEditors
selectedEditor = editors[0]?.term
}

dispatch.forms.addFormField({
form,
property,
focusNode,
matchedEditors: editors.matchSingleEditors({ shape: property }),
editors,
selectedEditor,
})
}
}
Loading

0 comments on commit e649150

Please sign in to comment.