Skip to content

Commit

Permalink
Adds the abilty to select a language for a Literal item from a list o…
Browse files Browse the repository at this point in the history
…f LOC language terms; specifies follow-up work to be done
  • Loading branch information
jgreben committed Feb 5, 2019
1 parent 203873a commit d5cc496
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 34 deletions.
6 changes: 6 additions & 0 deletions __tests__/actions/index.test.js
Expand Up @@ -13,6 +13,12 @@ describe('setItems actions', () => {
payload: {id: 0, label: "Instance of"}
})
})
it('setLang should create SET_LANG action', () => {
expect(actions.setLang({id:"Instance of", items: [{label: "food", id: "http://uri1", uri: "URI"}]})).toEqual({
type: 'SET_LANG',
payload: {id:"Instance of", items: [{label: "food", id: "http://uri1", uri:"URI"}]}
})
})
})

describe('getRDF action', () => {
Expand Down
66 changes: 66 additions & 0 deletions __tests__/components/editor/InputLang.test.js
@@ -0,0 +1,66 @@
// Copyright 2018 Stanford University see Apache2.txt for license
import React from 'react'
import { shallow } from 'enzyme'
import InputLang from '../../../src/components/editor/InputLang'

const plProps = {
"propertyTemplate":
{
"propertyLabel": "Instance of",
"propertyURI": "http://id.loc.gov/ontologies/bibframe/instanceOf",
"type": "literal",
},
"textValue": "test1"
}

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

it('contains a label with the value of propertyLabel', () => {
const expected = "Select langauge for test1"
expect(wrapper.find('label').text()).toEqual(
expect.stringContaining(expected)
)
})

it('typeahead component should useCache attribute', () => {
expect(wrapper.find('#langComponent').props().useCache).toBeTruthy()
})

it('typeahead component should use selectHintOnEnter', () => {
expect(wrapper.find('#langComponent').props().selectHintOnEnter).toBeTruthy()
})

it('should call the onChange event and set the state with the selected option', () => {
const event = (wrap) => {
wrap.setState({options: ["{id: 'test1', uri: 'URI', label: 'LABEL'}"]})
}
wrapper.find('#langComponent').simulate('change', event(wrapper))
expect(wrapper.state().options[0]).toBe("{id: 'test1', uri: 'URI', label: 'LABEL'}")
})

it('should call the onFocus event and set the selected option', () => {
const opts = {id: 'URI', label: 'LABEL', uri: 'URI'}
wrapper.instance().opts = opts
const event = (wrap) => {
global.fetch = jest.fn().mockImplementation(async () => await ({ok: true, resp: wrapper.instance().opts }))
wrap.setState({options: [ wrapper.instance().opts ]})
wrap.setState({selected: [ wrapper.instance().opts ]})
}
wrapper.find('#langComponent').simulate('focus', event(wrapper))
expect(wrapper.state().options[0]).toEqual(opts)

wrapper.find('#langComponent').simulate('change', event(wrapper))
expect(wrapper.state().selected[0]).toEqual(opts)

wrapper.find('#langComponent').simulate('blur', event(wrapper))
expect(wrapper.state("isLoading")).toBeFalsy()

})

it('sets the formData store with the total number of objects sent to selected', () => {
expect(mockFormDataFn.mock.calls.length).toBe(2)
})
})
40 changes: 31 additions & 9 deletions __tests__/components/editor/InputLiteral.test.js
Expand Up @@ -16,7 +16,7 @@ const plProps = {
}

describe('<InputLiteral />', () => {
const wrapper = shallow(<InputLiteral {...plProps} id={10}/>)
const wrapper = shallow(<InputLiteral {...plProps} id={10} rtId={'resourceTemplate:bf2:Monograph:Instance'} />)

it('contains a label with "Instance of"', () => {
expect(wrapper.find('label').text()).toBe('Instance of')
Expand Down Expand Up @@ -48,9 +48,11 @@ describe('When the user enters input into field', ()=>{
// our mock formData function to replace the one provided by mapDispatchToProps
const mockFormDataFn = jest.fn()
const removeMockDataFn = jest.fn()
mock_wrapper = shallow(<InputLiteral {...plProps} id={11}
mock_wrapper = shallow(<InputLiteral {...plProps}
id={11}
rtId={'resourceTemplate:bf2:Monograph:Instance'}
handleMyItemsChange={mockFormDataFn}
handleRemoveItem={removeMockDataFn}/>)
handleRemoveItem={removeMockDataFn} />)

it('has an id value as a unique property', () => {
expect(mock_wrapper.find('input').prop('id')).toEqual("typeLiteral11")
Expand All @@ -69,7 +71,11 @@ describe('When the user enters input into field', ()=>{
mock_wrapper.find('input').simulate('keypress', {key: 'Enter', preventDefault: () => {}})
// test to see arguments used after its been submitted
expect(mockFormDataFn.mock.calls[1][0]).toEqual(
{id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items:[{content: 'foo', id: 0}]}
{
id: "http://id.loc.gov/ontologies/bibframe/instanceOf",
items:[{ content: 'foo', id: 0 }],
rtId: "resourceTemplate:bf2:Monograph:Instance"
}
)
mockFormDataFn.mock.calls = [] // reset the redux store to empty
})
Expand All @@ -82,10 +88,18 @@ describe('When the user enters input into field', ()=>{
mock_wrapper.find('input').simulate('keypress', {key: 'Enter', preventDefault: () => {}})

expect(mockFormDataFn.mock.calls[0][0]).toEqual(
{id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items:[{content: 'fooby', id: 1}]}
{
id: "http://id.loc.gov/ontologies/bibframe/instanceOf",
items:[{ content: 'fooby', id: 1 }],
rtId: "resourceTemplate:bf2:Monograph:Instance"
}
)
expect(mockFormDataFn.mock.calls[1][0]).toEqual(
{id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items:[{content: 'bar', id: 2}]}
{
id: "http://id.loc.gov/ontologies/bibframe/instanceOf",
items:[{ content: 'bar', id: 2 }],
rtId: "resourceTemplate:bf2:Monograph:Instance"
}
)
mockFormDataFn.mock.calls = [] // reset the redux store to empty
})
Expand All @@ -102,10 +116,18 @@ describe('When the user enters input into field', ()=>{
mock_wrapper.find('input').simulate('keypress', {key: 'Enter', preventDefault: () => {}})

expect(mockFormDataFn.mock.calls[0][0]).toEqual(
{id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items:[{content: 'fooby', id: 3}]}
{
id: "http://id.loc.gov/ontologies/bibframe/instanceOf",
items:[{ content: 'fooby', id: 3 }],
rtId: "resourceTemplate:bf2:Monograph:Instance"
}
)
expect(mockFormDataFn.mock.calls[1][0]).toEqual(
{id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items:[]}
{
id: "http://id.loc.gov/ontologies/bibframe/instanceOf",
items:[],
rtId: "resourceTemplate:bf2:Monograph:Instance"
}
)
mockFormDataFn.mock.calls = [] // reset the redux store to empty

Expand All @@ -128,7 +150,7 @@ describe('When the user enters input into field', ()=>{
mock_wrapper.instance().props.propertyTemplate.repeatable = "false"
mock_wrapper.instance().forceUpdate()
mock_wrapper.setProps({formData: { id: "http://id.loc.gov/ontologies/bibframe/instanceOf", items: [{content: "foo", id: 4}]} })
expect(mock_wrapper.find('div#userInput').text()).toEqual('fooX') // contains X as a button to delete the input
expect(mock_wrapper.find('div#userInput').text()).toEqual('fooX<Button /><Modal />') // contains X as a button to delete the input
mock_wrapper.setProps({formData: undefined }) // reset props for next test
mockFormDataFn.mock.calls = [] // reset the redux store to empty
})
Expand Down
28 changes: 24 additions & 4 deletions __tests__/components/editor/ResourceTemplateForm.test.js
Expand Up @@ -60,11 +60,20 @@ const rtProps = {
]
}

const rtTest = { resourceURI: "http://id.loc.gov/ontologies/bibframe/Work" }
const mockHandleGenerateRDF = jest.fn()
const lits = { id: 0, content: 'content' }
const lups = { id: 'id', uri: 'uri', label: 'label' }

describe('<ResourceTemplateForm />', () => {
const mockHandleGenerateRDF = jest.fn()
const wrapper = shallow(<ResourceTemplateForm.WrappedComponent
{...rtProps}
handleGenerateRDF = {mockHandleGenerateRDF} />)
rtId={'resourceTemplate:bf2:Monograph:Instance'}
resourceTemplate = {rtTest}
handleGenerateRDF = {mockHandleGenerateRDF}
literals={lits}
lookups={lups}
/>)

it('renders the ResourceTemplateForm text nodes', () => {
wrapper.find('div.ResourceTemplateForm > p').forEach((node) => {
Expand Down Expand Up @@ -119,7 +128,11 @@ describe('<ResourceTemplateForm />', () => {
const rdf_wrapper = shallow(<ResourceTemplateForm.WrappedComponent
{...rtProps}
resourceTemplate = {rtTest}
handleGenerateRDF = {mockHandleGenerateRDF} />)
rtId={'resourceTemplate:bf2:Monograph:Instance'}
handleGenerateRDF = {mockHandleGenerateRDF}
literals={lits}
lookups={lups}
/>)
it('renders a Preview RDF button', () =>{
expect(rdf_wrapper
.find('div > button.btn-success').length)
Expand All @@ -132,7 +145,14 @@ describe('<ResourceTemplateForm />', () => {
})

it('renders error text when there are no propertyTemplates', () => {
const myWrap = shallow(<ResourceTemplateForm.WrappedComponent propertyTemplates={[]} />)
const myWrap = shallow(<ResourceTemplateForm.WrappedComponent
propertyTemplates={[]}
rtId={'resourceTemplate:bf2:Monograph:Instance'}
resourceTemplate = {rtTest}
handleGenerateRDF = {mockHandleGenerateRDF}
literals={lits}
lookups={lups}
/>)
const errorEl = myWrap.find('h1')
expect(errorEl).toHaveLength(1)
expect(errorEl.text()).toEqual('There are no propertyTemplates - probably an error.')
Expand Down
57 changes: 57 additions & 0 deletions __tests__/reducers/lang.test.js
@@ -0,0 +1,57 @@
import lang from '../../src/reducers/lang'

describe('changing the reducer state', () => {
const item_one = { "id": "http://uri1", "uri": "http://uri1", "label": "selection1" }
const item_two = { "id": "http://uri2", "uri": "http://uri2", "label": "selection2" }

it('should handle initial state', () => {
expect(
lang(undefined, {})
).toEqual({formData: []})
})



it('should handle SET_LANG', () => {
expect(
lang({formData: []}, {
type: 'SET_LANG',
payload: {id:'Run the tests', items: [ item_one ]}
})
).toEqual({
"formData": [{
"id": "Run the tests", "items": [ item_one ]
}]
})

expect(
lang({
"formData": [{
"id": "Run the tests", "items": [ item_one ]
}]}, {
type: 'SET_LANG',
payload: {id: "Run the tests", items: [ item_two ]}
})
).toEqual({
"formData": [
{"id": "Run the tests", "items": [ item_two ]}
]
})


expect(
lang({
"formData": [{
"id": "Run the tests", "items": [ item_one ]
}]}, {
type: 'SET_LANG',
payload: {id: "add this!", items: [ item_two ]}
})
).toEqual({
"formData": [
{"id": "Run the tests", "items": [ item_one ]},
{"id": "add this!", "items": [ item_two ]}
]
})
})
})
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -74,7 +74,7 @@
"dev-build": "webpack --progress --mode development",
"dev-build-test": "npm run dev-build && npm run test",
"dev-start": "webpack-dev-server --config ./webpack.config.js --mode development",
"eslint": "eslint --max-warnings 29 --color -c .eslintrc.js --ext js,jsx ./src",
"eslint": "eslint --max-warnings 31 --color -c .eslintrc.js --ext js,jsx ./src",
"jest-cov": "jest --coverage --colors",
"jest-ci": "jest --ci --runInBand --coverage --reporters=default --reporters=jest-junit --colors && cat ./coverage/lcov.info | coveralls",
"test": "jest --colors"
Expand Down
5 changes: 5 additions & 0 deletions src/actions/index.js
Expand Up @@ -17,3 +17,8 @@ export const getRDF = inputs => ({
type: 'GENERATE_RDF',
payload: inputs
})

export const setLang = item => ({
type: "SET_LANG",
payload: item
})

0 comments on commit d5cc496

Please sign in to comment.