Skip to content

Commit 68e3f7c

Browse files
committed
adding tests [#149832554]
1 parent b411981 commit 68e3f7c

File tree

12 files changed

+9945
-20
lines changed

12 files changed

+9945
-20
lines changed

src/actions/claimPublications.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export const POSSIBLY_YOUR_PUBLICATIONS_LOADING = 'POSSIBLY_YOUR_PUBLICATIONS_LO
1414
export const POSSIBLY_YOUR_PUBLICATIONS_COMPLETED = 'POSSIBLY_YOUR_PUBLICATIONS_COMPLETED';
1515
export const POSSIBLY_YOUR_PUBLICATIONS_FAILED = 'POSSIBLY_YOUR_PUBLICATIONS_FAILED';
1616

17-
export const LATEST_PUBLICATIONS_LOADING = 'LATEST_PUBLICATIONS_LOADING';
18-
export const LATEST_PUBLICATIONS_COMPLETED = 'LATEST_PUBLICATIONS_COMPLETED';
19-
export const LATEST_PUBLICATIONS_FAILED = 'LATEST_PUBLICATIONS_FAILED';
20-
2117
export const COUNT_POSSIBLY_YOUR_PUBLICATIONS_LOADING = 'COUNT_POSSIBLY_YOUR_PUBLICATIONS_LOADING';
2218
export const COUNT_POSSIBLY_YOUR_PUBLICATIONS_COMPLETED = 'COUNT_POSSIBLY_YOUR_PUBLICATIONS_COMPLETED';
2319
export const COUNT_POSSIBLY_YOUR_PUBLICATIONS_FAILED = 'COUNT_POSSIBLY_YOUR_PUBLICATIONS_FAILED';

src/modules/App/containers/App.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ function mapDispatchToProps(dispatch) {
1414
actions: bindActionCreators(actions, dispatch)
1515
};
1616
}
17-
1817
const AppContainer = connect(mapStateToProps, mapDispatchToProps)(App);
1918
export default AppContainer;

src/modules/PublicationForm/components/Forms/BookForm.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export default class BookForm extends Component {
5555
<StandardCard title={txt.authors.title} help={txt.authors.help}>
5656
<Field
5757
component={ContributorsEditorField}
58-
showIdentifierLookup
5958
name="authors"
6059
locale={{contributorSuffix: txt.authors.suffix}}
6160
showContributorAssignment

src/modules/SharedComponents/ContributorsEditor/components/ContributorForm.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import RaisedButton from 'material-ui/RaisedButton';
66

77
import {connect} from 'react-redux';
88
import {bindActionCreators} from 'redux';
9-
import * as actions from 'actions';
9+
import * as actions from 'actions/authors';
1010

11-
class ContributorForm extends Component {
11+
export class ContributorForm extends Component {
1212

1313
static propTypes = {
1414
authorsList: PropTypes.array,
@@ -43,12 +43,12 @@ class ContributorForm extends Component {
4343
// I need to catch scrolling event of scrolled container (which is not a window) to set position of autosuggest list when user scrolls
4444
// another solution, close the box when user tries to scroll
4545
const div = document.querySelector('div.layout-fill.align-stretch');
46-
div.addEventListener('scroll', this.handleParentContainerScroll.bind(this));
46+
if (div) div.addEventListener('scroll', this.handleParentContainerScroll.bind(this));
4747
}
4848

4949
componentWillUnmount() {
5050
const div = document.querySelector('div.layout-fill.align-stretch');
51-
div.removeEventListener('scroll', this.handleParentContainerScroll.bind(this));
51+
if (div) div.removeEventListener('scroll', this.handleParentContainerScroll.bind(this));
5252
}
5353

5454
handleParentContainerScroll() {
@@ -70,7 +70,7 @@ class ContributorForm extends Component {
7070
});
7171

7272
// move focus to name as published text field after item was added
73-
this.refs.nameAsPublishedField.focus();
73+
if (this.refs.nameAsPublishedField) this.refs.nameAsPublishedField.focus();
7474
}
7575

7676
_onNameChanged = (event, newValue) => {
@@ -104,8 +104,8 @@ class ContributorForm extends Component {
104104
const autoCompleteDataFormat = {text: 'displayName', value: 'aut_id'};
105105

106106
return (
107-
<div className="columns contributors dataList">
108-
<div className="column contributorsEntry">
107+
<div className="columns">
108+
<div className="column">
109109
<TextField
110110
fullWidth
111111
ref="nameAsPublishedField"
@@ -118,7 +118,7 @@ class ContributorForm extends Component {
118118
</div>
119119
{
120120
this.props.showIdentifierLookup &&
121-
<div className="column contributorsLinking">
121+
<div className="column">
122122
<AutoComplete
123123
disabled={this.props.disabled || this.state.nameAsPublished.trim().length === 0}
124124
listStyle={{maxHeight: 200, overflow: 'auto'}}
@@ -137,7 +137,7 @@ class ContributorForm extends Component {
137137
/>
138138
</div>
139139
}
140-
<div className="column is-narrow contributorsButton">
140+
<div className="column is-narrow">
141141
<RaisedButton
142142
className="is-mui-spacing-button"
143143
fullWidth
@@ -153,7 +153,7 @@ class ContributorForm extends Component {
153153

154154
const mapStateToProps = (state) => {
155155
return {
156-
authorsList: state.get('authorsReducer').authorsList || []
156+
authorsList: state.get('authorsReducer') ? state.get('authorsReducer').authorsList : []
157157
};
158158
};
159159

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
jest.dontMock('./ContributorForm');
2+
3+
import { mount } from 'enzyme';
4+
import { shallow } from 'enzyme';
5+
import toJson from 'enzyme-to-json';
6+
import React from 'react';
7+
import {ContributorForm} from './ContributorForm';
8+
import getMuiTheme from 'material-ui/styles/getMuiTheme';
9+
import PropTypes from 'prop-types';
10+
import {Provider} from 'react-redux';
11+
import Immutable from 'immutable';
12+
import injectTapEventPlugin from 'react-tap-event-plugin';
13+
import {authorsSearch} from 'mock/data/authors';
14+
15+
beforeAll(() => {
16+
injectTapEventPlugin();
17+
});
18+
19+
const create = () => {
20+
const initialState = Immutable.Map();
21+
22+
const store = {
23+
getState: jest.fn(() => (initialState)),
24+
dispatch: jest.fn(),
25+
subscribe: jest.fn()
26+
};
27+
const next = jest.fn();
28+
const invoke = (action) => thunk(store)(next)(action);
29+
return {store, next, invoke}
30+
};
31+
32+
function setup({authorsList, onAdd, showIdentifierLookup, actions, disabled, isMount}){
33+
34+
const props = {
35+
authorsList: authorsList || [], // : PropTypes.array,
36+
onAdd: onAdd || jest.fn(), // : PropTypes.func.isRequired,
37+
showIdentifierLookup: showIdentifierLookup || false, // : PropTypes.bool,
38+
actions: actions || {}, // : PropTypes.object.isRequired,
39+
disabled // : PropTypes.bool
40+
//locale, // : PropTypes.object,
41+
};
42+
43+
if (isMount) {
44+
return mount(
45+
<Provider store={create().store}>
46+
<ContributorForm {...props} />
47+
</Provider>, {
48+
context: {
49+
muiTheme: getMuiTheme()
50+
},
51+
childContextTypes: {
52+
muiTheme: PropTypes.object.isRequired
53+
}
54+
});
55+
}
56+
57+
return shallow(<Provider store={create().store}><ContributorForm {...props} /></Provider>);
58+
}
59+
60+
61+
62+
describe('ContributorForm tests ', () => {
63+
it('rendering display name field only', () => {
64+
const wrapper = setup({ isMount: true });
65+
expect(toJson(wrapper)).toMatchSnapshot();
66+
});
67+
68+
it('rendering display name field and identifier field', () => {
69+
const wrapper = setup({ showIdentifierLookup: true, isMount: true });
70+
expect(toJson(wrapper)).toMatchSnapshot();
71+
});
72+
73+
it('loads authors when value is changed', () => {
74+
const testFunction = jest.fn();
75+
const wrapper = setup({ showIdentifierLookup: true, actions: { searchAuthors: testFunction } }).find('ContributorForm').dive();
76+
wrapper.instance()._onUQIdentifierChanged('smith');
77+
expect(testFunction).toBeCalled();
78+
});
79+
80+
it('sets display name of a contributor ', () => {
81+
const testFunction = jest.fn();
82+
const wrapper = setup({ showIdentifierLookup: true, actions: { searchAuthors: testFunction } }).find('ContributorForm').dive();
83+
expect(wrapper.state.nameAsPublished).toBeFalsy();
84+
wrapper.instance()._onNameChanged({}, 'J. Smith');
85+
expect(wrapper.state().nameAsPublished).toEqual('J. Smith');
86+
});
87+
88+
89+
it('selects author identifier, calls add contributor ', () => {
90+
const testFunction = jest.fn();
91+
const wrapper = setup({ showIdentifierLookup: true, onAdd: testFunction }).find('ContributorForm').dive();
92+
wrapper.instance()._onUQIdentifierSelected(authorsSearch.data[0], 0);
93+
expect(testFunction).toBeCalled();
94+
});
95+
96+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
jest.dontMock('./ContributorRowHeader');
2+
3+
import { shallow } from 'enzyme';
4+
import toJson from 'enzyme-to-json';
5+
import React from 'react';
6+
import ContributorRowHeader from './ContributorRowHeader';
7+
8+
function setup({onDeleteAll, showIdentifierLookup, showContributorAssignment, disabled}){
9+
10+
const props = {
11+
onDeleteAll: onDeleteAll || jest.fn(), // : PropTypes.func.isRequired,
12+
showIdentifierLookup: showIdentifierLookup || false, // : PropTypes.bool,
13+
showContributorAssignment: showContributorAssignment || false, // : PropTypes.bool,
14+
// locale, // : PropTypes.object,
15+
disabled: disabled || false // : PropTypes.bool
16+
};
17+
18+
return shallow(<ContributorRowHeader {...props} />);
19+
}
20+
21+
describe('ContributorRowHeader renders ', () => {
22+
23+
it('header for contributor editor control with name and delete all button only', () => {
24+
const wrapper = setup({ });
25+
expect(toJson(wrapper)).toMatchSnapshot();
26+
});
27+
28+
it('header for contributor editor control with all options', () => {
29+
const wrapper = setup({ showIdentifierLookup: true, showContributorAssignment: true });
30+
expect(toJson(wrapper)).toMatchSnapshot();
31+
});
32+
33+
it('header for contributor editor control with delete all disabled', () => {
34+
const wrapper = setup({ disabled: true });
35+
expect(toJson(wrapper)).toMatchSnapshot();
36+
});
37+
});

src/modules/SharedComponents/ContributorsEditor/components/ContributorsEditor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ContributorForm from './ContributorForm';
77
import {Alert} from 'uqlibrary-react-toolbox';
88

99

10-
class ContributorsEditor extends Component {
10+
export class ContributorsEditor extends Component {
1111

1212
static propTypes = {
1313
showIdentifierLookup: PropTypes.bool,
@@ -60,7 +60,7 @@ class ContributorsEditor extends Component {
6060
}, () => {
6161
// try to automatically select contributor if they are a current author
6262
if (this.props.author && contributor.aut_id === this.props.author.aut_id) {
63-
this.onContributorAssigned(contributor, this.state.contributors.length - 1);
63+
this.assignContributor(contributor, this.state.contributors.length - 1);
6464
}
6565
});
6666
}
@@ -102,7 +102,7 @@ class ContributorsEditor extends Component {
102102
});
103103
}
104104

105-
onContributorAssigned = (contributor, index) => {
105+
assignContributor = (contributor, index) => {
106106
const newContributors = this.state.contributors.map((item, itemIndex) => (
107107
{
108108
...item,
@@ -131,7 +131,7 @@ class ContributorsEditor extends Component {
131131
disabled={this.props.disabled}
132132
showContributorAssignment={this.props.showContributorAssignment}
133133
disabledContributorAssignment={this.state.isCurrentAuthorSelected}
134-
onContributorAssigned={this.onContributorAssigned} />
134+
onContributorAssigned={this.assignContributor} />
135135
);
136136

137137
return (

0 commit comments

Comments
 (0)