/
InputLookupQA.test.js
148 lines (127 loc) · 5.41 KB
/
InputLookupQA.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018 Stanford University see Apache2.txt for license
import 'jsdom-global/register'
import React from 'react'
import { shallow } from 'enzyme'
import { mount } from "enzyme"
import InputLookup from '../../../src/components/editor/InputLookupQA'
const plProps = {
"propertyTemplate":
{
"mandatory": "false",
"repeatable": "true",
"type": "lookup",
"resourceTemplates": [],
"valueConstraint": {
"valueTemplateRefs": [],
"useValuesFrom": [
'lookupQaLocNames'
],
"valueDataType": {
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Agent"
},
"defaults": []
},
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Name Lookup"
}
};
const p2Props = {
"propertyTemplate":
{
"mandatory": "false",
"repeatable": "true",
"type": "lookup",
"resourceTemplates": [],
"valueConstraint": {
"valueTemplateRefs": [],
"useValuesFrom": [
'lookupQaLocNames',
'lookupQaLocSubjects'
],
"valueDataType": {
"dataTypeURI": "http://id.loc.gov/ontologies/bibframe/Agent"
},
"defaults": []
},
"propertyURI": "http://id.loc.gov/ontologies/bflc/target",
"propertyLabel": "Name Lookup"
}
};
const multipleResults = [{"authLabel":"Person",
"authURI":"PersonURI",
"body":[{"uri":"puri","label":"plabel"}]},
{"authLabel":"Subject",
"authURI":"SubjectURI",
"body":[{"uri":"suri","label":"slabel"}]
}];
describe('<InputLookup />', () => {
// our mock formData function to replace the one provided by mapDispatchToProps
const mockFormDataFn = jest.fn()
const wrapper = shallow(<InputLookup.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')
})
it('sets the typeahead component required attribute according to the mandatory property from the template', () => {
expect(wrapper.find('#lookupComponent').props().required).toBeFalsy()
})
it('displays RequiredSuperscript if mandatory from template is true', () => {
wrapper.instance().props.propertyTemplate.mandatory = "true"
wrapper.instance().forceUpdate()
expect(wrapper.find('label > RequiredSuperscript')).toBeTruthy()
})
it('sets the typeahead component multiple attribute according to the repeatable property from the template', () => {
expect(wrapper.find('#lookupComponent').props().multiple).toBeTruthy()
})
it('sets the typeahead component placeholder attribute to be the propertyLabel', () => {
expect(wrapper.find('#lookupComponent').props().placeholder).toBe('Name Lookup')
})
it('should call the onChange event and set the state with the selected option', () => {
const event = (wrap) => {
wrap.setState({options: ["{uri: 'URI', label: 'LABEL'}"]})
}
wrapper.find('#lookupComponent').simulate('change', event(wrapper))
expect(wrapper.state().options[0]).toBe("{uri: 'URI', label: 'LABEL'}")
})
it('should call the Search and Change events and set the state with the returned json', () => {
const json = "{uri: 'URI', label: 'LABEL'}"
const event = (wrap) => {
wrap.setState({options: [json]})
wrap.setState({selected: [json]})
}
wrapper.find('#lookupComponent').simulate('search', event(wrapper))
expect(wrapper.state().options[0]).toEqual(json)
wrapper.find('#lookupComponent').simulate('change', event(wrapper))
expect(wrapper.state().selected[0]).toEqual(json)
expect(mockFormDataFn.mock.calls.length).toBe(2)
})
it('should have a PropertyRemark when a remark is present', () => {
wrapper.instance().props.propertyTemplate.remark = "http://rda.test.org/1.1"
wrapper.instance().forceUpdate()
const propertyRemark = wrapper.find('label > PropertyRemark')
expect(propertyRemark).toBeTruthy()
})
//Institute wrapper with multiple lookup options
const multipleWrapper = shallow(<InputLookup.WrappedComponent {...p2Props} handleSelectedChange={mockFormDataFn} />)
it('should pass multiple lookup results in state with search event', () => {
const event = (wrap) => {
wrap.setState({options: multipleResults})
}
multipleWrapper.find('#lookupComponent').simulate('search', event(multipleWrapper))
expect(multipleWrapper.state().options[0]).toEqual(multipleResults[0])
expect(multipleWrapper.state().options[1]).toEqual(multipleResults[1])
})
//Headers expected
it('should show menu headers with lookup source labels and values in the dropdown when provided results', () => {
const instance = multipleWrapper.instance();
const menuWrapper = shallow(instance.renderMenuFunc(multipleResults, p2Props));
const menuChildrenNumber = menuWrapper.children().length;
//One top level menu component
expect(menuWrapper.find('ul').length).toEqual(1);
//Four children, with two headings and two items
expect(menuChildrenNumber).toEqual(4);
expect(menuWrapper.childAt(0).html()).toEqual("<li class=\"dropdown-header\">Person</li>");
expect(menuWrapper.childAt(1).childAt(0).text()).toEqual("plabel");
expect(menuWrapper.childAt(2).html()).toEqual("<li class=\"dropdown-header\">Subject</li>");
expect(menuWrapper.childAt(3).childAt(0).text()).toEqual("slabel");
})
})