Skip to content

Commit

Permalink
Extract and pass along property template defaults from InputLookupQA …
Browse files Browse the repository at this point in the history
…component to typeahead (#574)

Extract and pass along property template defaults from InputLookupQA component to typeahead
  • Loading branch information
mjgiarlo committed May 29, 2019
2 parents d208e21 + 55d8c8c commit 7c424d5
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 268 deletions.
93 changes: 64 additions & 29 deletions __tests__/Utilities.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright 2019 Stanford University see LICENSE for license

import { isResourceWithValueTemplateRef, resourceToName, templateBoolean } from '../src/Utilities'
import {
isResourceWithValueTemplateRef,
resourceToName,
templateBoolean,
defaultValuesFromPropertyTemplate
} from '../src/Utilities'

describe('Utilities', () => {

describe('Utilities', () => {
describe('isResourceWithValueTemplateRef()', () => {

it('true when there is a valueTemplateRef', () => {
it('returns true when there is a valueTemplateRef', () => {
const templateWithValueTemplateRefs = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/note",
"type": "resource",
Expand All @@ -19,7 +23,7 @@ describe('Utilities', () => {
expect(isResourceWithValueTemplateRef(templateWithValueTemplateRefs)).toBeTruthy()
})

it('true when there are multiple valueTemplateRefs' , () => {
it('returns true when there are multiple valueTemplateRefs' , () => {
const templateWithTwoValueTemplateRefs = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/note",
"type": "resource",
Expand All @@ -33,7 +37,7 @@ describe('Utilities', () => {
expect(isResourceWithValueTemplateRef(templateWithTwoValueTemplateRefs)).toBeTruthy()
})

it('false when valueTemplateRefs is empty', () => {
it('returns false when valueTemplateRefs is empty', () => {
const emptyValueTemplateRefs = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/issuance",
"type": "resource",
Expand All @@ -44,15 +48,15 @@ describe('Utilities', () => {
expect(isResourceWithValueTemplateRef(emptyValueTemplateRefs)).toBeFalsy()
})

it('false when there is no valueConstraint', () => {
it('returns false when there is no valueConstraint', () => {
const noValueConstraint = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/issuance",
"type": "resource"
}
expect(isResourceWithValueTemplateRef(noValueConstraint)).toBeFalsy()
})

it('false when valueConstraint is empty (there are no valueTemplateRefs)', () => {
it('returns false when valueConstraint is empty (there are no valueTemplateRefs)', () => {
const emptyValueConstraint = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/issuance",
"type": "resource",
Expand All @@ -61,7 +65,7 @@ describe('Utilities', () => {
expect(isResourceWithValueTemplateRef(emptyValueConstraint)).toBeFalsy()
})

it('false when the type is other than resource', () => {
it('returns false when the type is other than resource', () => {
const notTypeResource = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/title",
"type": "literal",
Expand All @@ -74,7 +78,7 @@ describe('Utilities', () => {
expect(isResourceWithValueTemplateRef(notTypeResource)).toBeFalsy()
})

it('false when there is no type at all', () => {
it('returns false when there is no type at all', () => {
const noTypeAtAll = {
"propertyURI": "http://id.loc.gov/ontologies/bibframe/note",
"valueConstraint": {
Expand All @@ -89,7 +93,6 @@ describe('Utilities', () => {
})

describe('resourceToName()', () => {

it('returns resource name from last path part of URI', () => {
const uri = "https://trellis.sinopia.io/resources/ld4p/resourceTemplate:bf2:Note"
expect(resourceToName(uri)).toEqual("resourceTemplate:bf2:Note")
Expand All @@ -107,31 +110,63 @@ describe('Utilities', () => {
it('returns undefined when there is no URI', () => {
expect(resourceToName()).toEqual(undefined)
})

})

describe('templateBoolean()', () => {
describe('templateBoolean()', () => {
it('returns true when "true" is passed in as a string', () => {
expect(templateBoolean("true")).toBe(true)
})

it('returns true when "true" is passed in as a string', () => {
expect(templateBoolean("true")).toBe(true)
})
it('returns true when a boolean true is passed into the function', () => {
expect(templateBoolean(true)).toBe(true)
})

it('returns true when a boolean true is passed into the function', () => {
expect(templateBoolean(true)).toBe(true)
})
it('returns true as the default if the parameter is null, undefined, or a random string', () => {
expect(templateBoolean(null)).toBe(true)
expect(templateBoolean(undefined)).toBe(true)
expect(templateBoolean("asdfdsafds")).toBe(true)
})

it('returns true as the default if the parameter is null, undefined, or a random string', () => {
expect(templateBoolean(null)).toBe(true)
expect(templateBoolean(undefined)).toBe(true)
expect(templateBoolean("asdfdsafds")).toBe(true)
})
it('return false when "false" is passed in as a string', () => {
expect(templateBoolean("false")).toBe(false)
})

it('return false when "false" is passed in as a string', () => {
expect(templateBoolean("false")).toBe(false)
it('returns false when a boolean false is passed into the function', () => {
expect(templateBoolean(false)).toBe(false)
})
})

it('returns false when a boolean false is passed into the function', () => {
expect(templateBoolean(false)).toBe(false)
describe('defaultValuesFromPropertyTemplate()', () => {
it('returns an empty array if passed any value that fails to define a `valueConstraint.defaults` array', () => {
const propertyTemplate = null
expect(defaultValuesFromPropertyTemplate(propertyTemplate)).toEqual([])
})

it('returns an empty array if passed a value with an empty `valueConstraint.defaults` array', () => {
const propertyTemplate = {
valueConstraint: {
defaults: []
}
}
expect(defaultValuesFromPropertyTemplate(propertyTemplate)).toEqual([])
})

it('returns an array with an object otherwise', () => {
const propertyTemplate = {
valueConstraint: {
defaults: [
{
defaultURI: 'http://id.loc.gov/vocabulary/mcolor/mul',
defaultLiteral: 'color'
}
]
}
}
expect(defaultValuesFromPropertyTemplate(propertyTemplate)).toEqual([{
id: 'http://id.loc.gov/vocabulary/mcolor/mul',
label: 'color',
uri: 'http://id.loc.gov/vocabulary/mcolor/mul'
}])
})
})
})
})
48 changes: 41 additions & 7 deletions __tests__/components/editor/InputListLOC.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,47 @@ describe('<InputList />', () => {
expect(wrapper.find('#targetComponent').props().placeholder).toMatch('Frequency (RDA 2.14)')
})

it('sets the default values according to the property template if they exist', () => {
const defaults = [{
id: 'http://id.loc.gov/vocabulary/carriers/nc',
uri: 'http://id.loc.gov/vocabulary/carriers/nc',
label: 'volume'
}]
expect(wrapper.state('defaults')).toEqual(defaults)
describe('default values', () => {
afterAll(() => {
jest.restoreAllMocks()
})

it('sets the default values according to the property template if they exist', () => {
const defaults = [{
id: 'http://id.loc.gov/vocabulary/carriers/nc',
uri: 'http://id.loc.gov/vocabulary/carriers/nc',
label: 'volume'
}]
expect(wrapper.state('defaults')).toEqual(defaults)
})

it('logs an error when no defaults are set', () => {
const plProps = {
"propertyTemplate": {
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Frequency (RDA 2.14)",
"remark": "http://access.rdatoolkit.org/2.14.html",
"mandatory": "false",
"repeatable": "false",
"type": "lookup",
"valueConstraint": {
"repeatable": "true",
"valueTemplateRefs": [],
"useValuesFrom": [
"vocabulary:bf2:frequencies"
],
"valueDataType": {
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Frequency"
}
}
}
}
const errorSpy = jest.spyOn(console, 'error').mockReturnValue(null)
const wrapper2 = shallow(<InputList.WrappedComponent {...plProps} handleSelectedChange={mockFormDataFn} />)

expect(wrapper2.state('defaults')).toEqual([])
expect(errorSpy).toBeCalledWith(`no defaults defined in property template: ${JSON.stringify(plProps.propertyTemplate)}`)
})
})

it('should call the onFocus event and set the selected option', () => {
Expand Down
74 changes: 67 additions & 7 deletions __tests__/components/editor/InputLookupQA.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2018 Stanford University see LICENSE for license

import 'jsdom-global/register'
import React from 'react'
import { shallow } from 'enzyme'
import InputLookup from '../../../src/components/editor/InputLookupQA'
import InputLookupQA from '../../../src/components/editor/InputLookupQA'

const plProps = {
"id": "lookupComponent",
Expand All @@ -20,12 +21,16 @@ const plProps = {
"valueDataType": {
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Agent"
},
"defaults": []
"defaults": [{
"defaultURI": "http://id.loc.gov/vocabulary/carriers/nc",
"defaultLiteral": "volume"
}]
},
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Name Lookup"
}
};
}

const p2Props = {
"id": "lookupComponent",
"propertyTemplate":
Expand All @@ -48,7 +53,7 @@ const p2Props = {
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Name Lookup"
}
};
}

const multipleResults = [{
"authLabel":"Person",
Expand All @@ -59,12 +64,12 @@ const multipleResults = [{
"authLabel":"Subject",
"authURI":"SubjectURI",
"body":[{ "uri":"suri","label":"slabel" }]
}];
}]

describe('<InputLookupQA />', () => {
// our mock formData function to replace the one provided by mapDispatchToProps
const mockFormDataFn = jest.fn()
const wrapper = shallow(<InputLookup.WrappedComponent {...plProps} handleSelectedChange={mockFormDataFn} />)
const wrapper = shallow(<InputLookupQA.WrappedComponent {...plProps} handleSelectedChange={mockFormDataFn} />)

it('uses the propertyLabel from the template as the form control label', () => {
expect(wrapper.find('#lookupComponent').props().placeholder).toMatch('Name Lookup')
Expand All @@ -83,6 +88,61 @@ describe('<InputLookupQA />', () => {
expect(wrapper.find('#lookupComponent').props().multiple).toBeTruthy()
})

describe('default values', () => {
afterAll(() => {
jest.restoreAllMocks()
})

it('sets the default values according to the property template if they exist', () => {
const defaults = [{
id: 'http://id.loc.gov/vocabulary/carriers/nc',
uri: 'http://id.loc.gov/vocabulary/carriers/nc',
label: 'volume'
}]
expect(wrapper.state('defaults')).toEqual(defaults)
})

it('logs an error when no defaults are set', () => {
const plProps = {
"id": "lookupComponent",
"propertyTemplate":
{
"mandatory": "false",
"repeatable": "true",
"type": "lookup",
"resourceTemplates": [],
"valueConstraint": {
"valueTemplateRefs": [],
"useValuesFrom": [
'lookupQaLocNames'
],
"valueDataType": {
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Agent"
}
},
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Name Lookup"
}
}

const errorSpy = jest.spyOn(console, 'error').mockReturnValue(null)
const wrapper2 = shallow(<InputLookupQA.WrappedComponent {...plProps} handleSelectedChange={mockFormDataFn} />)

expect(wrapper2.state('defaults')).toEqual([])
expect(errorSpy).toBeCalledWith(`no defaults defined in property template: ${JSON.stringify(plProps.propertyTemplate)}`)
})

it('sets the async typeahead component defaultSelected attribute', () => {
const wrapper2 = shallow(<InputLookupQA.WrappedComponent {...plProps} handleSelectedChange={mockFormDataFn} />)

expect(wrapper2.find('#lookupComponent').props().defaultSelected).toEqual([{
id: 'http://id.loc.gov/vocabulary/carriers/nc',
uri: 'http://id.loc.gov/vocabulary/carriers/nc',
label: 'volume'
}])
})
})

it('should call the onChange event and set the state with the selected option', () => {
const event = (wrap) => {
wrap.setState({options: ["{uri: 'URI', label: 'LABEL'}"]})
Expand Down Expand Up @@ -114,7 +174,7 @@ describe('<InputLookupQA />', () => {
})

//Institute wrapper with multiple lookup options
const multipleWrapper = shallow(<InputLookup.WrappedComponent {...p2Props} handleSelectedChange={mockFormDataFn} />)
const multipleWrapper = shallow(<InputLookupQA.WrappedComponent {...p2Props} handleSelectedChange={mockFormDataFn} />)
it('passes multiple lookup results in state with search event', () => {
const event = (wrap) => {
wrap.setState({options: multipleResults})
Expand Down
Loading

0 comments on commit 7c424d5

Please sign in to comment.