Skip to content

Commit

Permalink
Fixes error removing item when defaults exist, updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed Mar 21, 2019
1 parent f947360 commit 0d660f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
33 changes: 25 additions & 8 deletions __tests__/components/editor/InputLiteral.test.js
@@ -1,5 +1,5 @@
// Copyright 2018, 2019 Stanford University see Apache2.txt for license

import 'jsdom-global/register'
import React from 'react'
import { shallow } from 'enzyme'
import { InputLiteral } from '../../../src/components/editor/InputLiteral'
Expand Down Expand Up @@ -176,16 +176,33 @@ describe('when there is a default literal value in the property template', () =>
const mockRemoveItem = jest.fn()

it('sets the default values according to the property template if they exist', () => {
plProps.propertyTemplate['valueConstraint'] = valConstraintProps

const wrapper = shallow(<InputLiteral {...plProps} id={12}
const plProps = {
"propertyTemplate":
{
"propertyLabel": "Instance of",
"propertyURI": "http://id.loc.gov/ontologies/bibframe/instanceOf",
"type": "literal",
"mandatory": "",
"repeatable": "",
"valueConstraint": valConstraintProps
}
}
const wrapper = shallow(<InputLiteral propertyTemplate={plProps.propertyTemplate} id={12}
blankNodeForLiteral={{ termType: 'BlankNode', value: 'n3-0'}}
handleMyItemsChange={mockMyItemsChange}
rtId={'resourceTemplate:bf2:Monograph:Instance'}
/>)
wrapper.setProps({ formData: { items: valConstraintProps.defaults } })
rtId={'resourceTemplate:bf2:Monograph:Instance'} />)
// Mocking a call to the Redux store
const items = [{
"uri": "http://id.loc.gov/vocabulary/organizations/dlc",
"content": "DLC"
}]
wrapper.setProps({
formData: {
items: items
}
})
wrapper.instance().forceUpdate()
expect(wrapper.find('#userInput')).toBeTruthy()
expect(wrapper.find('#userInput').text()).toMatch(items[0].content)
})

describe('when repeatable="false"', () => {
Expand Down
19 changes: 14 additions & 5 deletions src/components/editor/InputLiteral.jsx
Expand Up @@ -15,12 +15,18 @@ export class InputLiteral extends Component {

constructor(props) {
super(props)
let lastId
try {
lastId = Number(props.propertyTemplate.valueConstraint.defaults.length)-1
} catch (err) {
lastId = -1
}
this.state = {
show: false,
content_add: "",
disabled: false
disabled: false,
lastId: lastId
}
this.lastId = -1
}

handleShow = () => {
Expand Down Expand Up @@ -48,10 +54,12 @@ export class InputLiteral extends Component {
}

addUserInput = (userInputArray, currentcontent) => {
const newId = this.state.lastId + 1
userInputArray.push({
content: currentcontent,
id: ++this.lastId
id: newId
})
this.setState( { lastId: newId } )
}

handleKeypress = (event) => {
Expand Down Expand Up @@ -84,14 +92,15 @@ export class InputLiteral extends Component {
handleItemClick = (event) => {
const labelToRemove = event.target.dataset["content"]
const idToRemove = Number(event.target.dataset["item"])

this.props.handleRemoveItem(
{
id: idToRemove,
label: labelToRemove,
rtId: this.props.rtId,
uri: this.props.propertyTemplate.propertyURI
})
this.setState({disabled: false})
this.setState({ disabled: false })
}

checkMandatoryRepeatable = () => {
Expand Down Expand Up @@ -157,7 +166,7 @@ export class InputLiteral extends Component {
type="button"
onClick={this.handleItemClick}
key={obj.id}
data-item={obj.id}
data-item={index}
data-label={formInfo.uri}
>X
</button>
Expand Down
3 changes: 2 additions & 1 deletion src/reducers/index.js
Expand Up @@ -17,8 +17,8 @@ const resourceTemplateSelector = (state, props) => {
if (props.propertyTemplate.propertyURI in resTemp) {
items = resTemp[props.propertyTemplate.propertyURI]
} else {
resTemp[props.propertyTemplate.propertyURI] = []
items = []
resTemp[props.propertyTemplate.propertyURI] = items
}
return items
}
Expand All @@ -44,6 +44,7 @@ export const setResourceTemplate = (state, action) => {
// This items payload needs to vary if type is literal or lookup
output[rtKey][property.propertyURI].items.push(
{
id: output[rtKey][property.propertyURI].items.length,
content: row.defaultLiteral,
uri: row.defaultURI
}
Expand Down

0 comments on commit 0d660f3

Please sign in to comment.