Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working selector and defaults for InputLiteral using common reducers #426

Merged
merged 4 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions __tests__/components/editor/InputLiteral.test.js
Original file line number Diff line number Diff line change
@@ -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 @@ -172,27 +172,37 @@ describe('When the user enters input into field', ()=>{
})

describe('when there is a default literal value in the property template', () => {
it('sets the default values according to the property template if they exist', () => {
plProps.propertyTemplate['valueConstraint'] = valConstraintProps

const setDefaultsForLiteralWithPayLoad = jest.fn()
const defaultsForLiteral = jest.fn()
defaultsForLiteral.mockReturnValue({
content: "content",
id: 0,
bnode: { termType: 'BlankNode', value: 'n3-0'},
propPredicate: "predicate"
})
const mockMyItemsChange = jest.fn()
const mockRemoveItem = jest.fn()

const wrapper = shallow(<InputLiteral {...plProps} id={12}
it('sets the default values according to the property template if they exist', () => {
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'}}
rtId={'resourceTemplate:bf2:Monograph:Instance'}
setDefaultsForLiteralWithPayLoad={setDefaultsForLiteralWithPayLoad}
defaultsForLiteral={defaultsForLiteral}
/>)
expect(setDefaultsForLiteralWithPayLoad).toHaveBeenCalledTimes(1)
expect(defaultsForLiteral).toHaveBeenCalledTimes(1)
expect(wrapper.instance().lastId).toEqual(0)
handleMyItemsChange={mockMyItemsChange}
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').text()).toMatch(items[0].content)
})

describe('when repeatable="false"', () => {
Expand All @@ -207,9 +217,6 @@ describe('when there is a default literal value in the property template', () =>
}
}

const mockMyItemsChange = jest.fn()
const mockRemoveItem = jest.fn()

const nonrepeat_wrapper = shallow(
<InputLiteral {...nrProps}
id={11}
Expand Down
189 changes: 121 additions & 68 deletions __tests__/reducers/literal.test.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,141 @@
import { literal } from '../../src/reducers/literal'
import { removeAllContent, removeMyItem, setMyItems } from '../../src/reducers/literal'

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

it('should handle SET_ITEMS', () => {
expect(
literal({formData: []}, {
type: 'SET_ITEMS',
payload: {id: 1, uri:'Run the tests', items: []}
})
).toEqual({
"formData": [{
"id": 1, "uri": "Run the tests", "items": []
}]
it('SET_ITEMS adds item to state', () => {
const literalSetItems = setMyItems({ "resourceTemplate:Monograph:Instance": {
'http://schema.org/name': { items: [] }
}}, {
type: 'SET_ITEMS',
payload: {
rtId: 'resourceTemplate:Monograph:Instance',
uri:'http://schema.org/name',
items: [ { id: 0, content: 'Run the tests'} ]
}
})
expect(literalSetItems).toEqual({
jermnelson marked this conversation as resolved.
Show resolved Hide resolved
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [{ id: 0, content: 'Run the tests'}]
}
}
jermnelson marked this conversation as resolved.
Show resolved Hide resolved
})
})

it('SET_ITEMS adds new item to state when state has existing selector for another literal', () => {
expect(
jermnelson marked this conversation as resolved.
Show resolved Hide resolved
literal({
"formData": [{
"id": 1, "uri": "Run the tests", "items": []
}]}, {
setMyItems({ "resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [{ id: 1, content: "Run the tests" }] },
'http://schema.org/description': {
items: []}
}
},
{
type: 'SET_ITEMS',
payload: {id:2, uri: "add this!", items: []}
})
).toEqual({
"formData": [
{"id": 1, "uri": "Run the tests", "items": []},
{"id": 2, "uri": "add this!", "items": []}
]})
})
it('should handle REMOVE_ITEM', () => {
expect(
literal({formData: [{id: 1, uri:"Test", items:[
{content: "test content", id: 0},
{content: "more content", id: 1}
]}]}, {
type: 'REMOVE_ITEM',
payload: {id: 0, label: "Test"}
payload: {
rtId: "resourceTemplate:Monograph:Instance",
uri: 'http://schema.org/description',
items: [ { id: 2, content: "add this!"}]
}
})
).toEqual({
"formData": [{
id: 1, uri: "Test", "items": [{content: "more content", id: 1}]
}]
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [{ id: 1, content: 'Run the tests'}]
},
'http://schema.org/description': {
items: [{ id: 2, content: "add this!"}]
}
}
})
})

it('REMOVE_ITEM removes an item from state', () => {
expect(removeMyItem({
jermnelson marked this conversation as resolved.
Show resolved Hide resolved
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{content: "test content", id: 0},
{content: "more content", id: 1}
]
}
}
},
{
type: 'REMOVE_ITEM',
payload: {
id: 0,
rtId: "resourceTemplate:Monograph:Instance",
uri: "http://schema.org/name",
content: "test content"
}
})).toEqual({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [{ id: 1, content: "more content" }]
}
}
})
})

expect(
literal({formData: [
{id: 1, uri:"Test", items:[{content: "test content", id: 0}]},
{id: 2, uri:"Statement", items:[{content: "more test content", id: 0}]}
]}, {
type: 'REMOVE_ITEM',
payload: {id: 0, label: "Statement"}
})
).toEqual({
"formData": [
{"id": 1, "uri": "Test", "items": [{content: "test content", id: 0}]},
{"id": 2, "uri": "Statement", "items": []}
]
it('Calling REMOVE_ITEMS with non-existent id does not change state', () => {
expect(removeMyItem({
jermnelson marked this conversation as resolved.
Show resolved Hide resolved
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{content: "Test", id: 1},
{content: "Statement", id: 2}
]
}
}
},
{
type: 'REMOVE_ITEM',
payload: {
id: 0,
rtId: "resourceTemplate:Monograph:Instance",
uri: "http://schema.org/name",
content: "test content"
}
})
).toEqual({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{content: "Test", id: 1},
{content: "Statement", id: 2}
]
}
}
})
})

it('should handle REMOVE_ALL_CONTENT', () => {
expect(
literal({
formData: [
{id: 1, uri:"Test", items:[
{content: "test content", id: 0},
{content: "test content2", id: 1}
]},
{id: 2, uri:"Test2", items:[
{content: "test2 content", id: 0},
{content: "test2 content2", id: 1}
]}
]},
{ type: 'REMOVE_ALL_CONTENT', payload: 1})
removeAllContent({
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: [
{content: "Test", id: 1},
{content: "Statement", id: 2}
]
}
}
},
{
type: "REMOVE_ALL_CONTENT",
payload:{
rtId: "resourceTemplate:Monograph:Instance",
uri: 'http://schema.org/name'
}
})
).toEqual({
"formData": [
{"id": 2, "uri": "Test2", "items": [{content: "test2 content", id: 0}, {content: "test2 content2", id: 1}]}
]
"resourceTemplate:Monograph:Instance": {
'http://schema.org/name': {
items: []
}
}
})
})
})
Loading