Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grafitto committed Sep 1, 2022
1 parent 1b37ba6 commit 396064a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 15 deletions.
11 changes: 2 additions & 9 deletions app/react/ConnectionsList/components/ConnectionsGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { connect } from 'react-redux';
import { t } from 'app/I18N';

import ConnectionsGroup from './ConnectionsGroup';
import { LibraryViewRelationships } from './LibraryViewRelationships';

class ConnectionsGroupsComponent extends Component {
render() {
Expand All @@ -21,15 +22,7 @@ class ConnectionsGroupsComponent extends Component {

if (connectionsGroups.size) {
if (this.props.sidePanelTrigger === 'library') {
Results = (
<div styles={{ padding: '20px' }}>
<div className="list-group">
{connectionsGroups.map(group => (
<div className="list-group-item">{group.get('connectionLabel')}</div>
))}
</div>
</div>
);
Results = <LibraryViewRelationships />;
} else {
Results = (
<div>
Expand Down
115 changes: 115 additions & 0 deletions app/react/ConnectionsList/components/LibraryViewRelationships.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useEffect } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import PropTypes from 'prop-types';
import { Map, List } from 'immutable';
import { bindActionCreators } from 'redux';
import { Icon } from 'app/UI';
import * as actions from '../../Relationships/actions/actions';
import HubRelationshipMetadata from '../../Relationships/components/HubRelationshipMetadata';

function mapStateToProps(state: any) {
const { relationships, library } = state;
return {
parentEntity: library.ui.getIn(['selectedDocuments', 0]),
searchResults: relationships.list.searchResults,
search: relationships.list.sort,
hubs: relationships.hubs,
relationTypes: actions.selectRelationTypes(state),
};
}

function mapDispatchToProps(dispatch: any) {
return bindActionCreators(
{
parseResults: actions.parseResults,
},
dispatch
);
}

const connector = connect(mapStateToProps, mapDispatchToProps);

type ComponentProps = ConnectedProps<typeof connector>;

const createRightRelationshipGroups = (rightRelationships: any, relationTypes: any[]) => (
<div className="list-group">
{rightRelationships.map((relationship: any, index: number) => (
<>
<div className="list-group-item" key={index}>
<span className="property-name">
{' '}
{(() => {
const relationshipTemplateId = relationship.get('template');
const relationType = relationTypes.find(r => r._id === relationshipTemplateId);
if (relationType) {
return relationshipTemplateId ? (
relationTypes.find(r => r._id === relationshipTemplateId).name
) : (
<Icon icon="link" />
);
}
return null;
})()}
</span>
</div>
{(() => {
return relationship.get('relationships').map((rel: any, indexI: number) => (
<div className="list-group-item" key={indexI}>
<div className={`item-document item-${rel.getIn(['entityData', '_id'])}`}>
<div className="item-info">
<div className="item-name">
<span>{rel.getIn(['entityData', 'title'])}</span>
</div>
</div>
<HubRelationshipMetadata relationship={rel} />
</div>
</div>
));
})()}
</>
))}
</div>
);

const createLabelGroups = (hub: any, relationTypes: any[]) => {
const template = hub.getIn(['leftRelationship', 'template']);
return (
<div key={hub.get('hub')} className="list-group-item">
<span className="property-name">
{template && <div>{relationTypes.find(r => r._id === template).name}</div>}
</span>
{createRightRelationshipGroups(hub.get('rightRelationships'), relationTypes)}
</div>
);
};

const LibraryViewRelationshipsComp = ({
hubs,
searchResults,
parentEntity,
parseResults,
relationTypes,
}: ComponentProps) => {
useEffect(() => {
if (parentEntity) {
parseResults(searchResults, parentEntity, false);
}
}, [searchResults, parentEntity]);
return (
<div className="list-group">
{hubs.map((hub: any) => createLabelGroups(hub, relationTypes))}
</div>
);
};

LibraryViewRelationshipsComp.propTypes = {
parentEntity: PropTypes.instanceOf(Map),
hubs: PropTypes.instanceOf(List).isRequired,
searchResults: PropTypes.instanceOf(Map).isRequired,
parseResults: PropTypes.func.isRequired,
relationTypes: PropTypes.instanceOf(Array).isRequired,
};

const LibraryViewRelationships = connector(LibraryViewRelationshipsComp);

export { LibraryViewRelationshipsComp, LibraryViewRelationships };
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('ConnectionsGroups', () => {
],
},
]),
sidePanelTrigger: 'entityView',
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('DocumentSidePanel', () => {
beforeEach(() => {
state = {
documentViewer: { targetDoc: Immutable.fromJS({ _id: null }) },
relationships: { list: { connectionsGroups: 'connectionsGroups' } },
relationships: { list: { connectionsGroups: ['connectionsGroups'] } },
relationTypes: Immutable.fromJS(['a', 'b']),
settings: { collection: Immutable.fromJS({ languages }) },
library: { sidepanel: { metadata: {} } },
Expand Down Expand Up @@ -241,12 +241,13 @@ describe('DocumentSidePanel', () => {
expect(mapStateToProps(state, ownProps).references).toBe(
'References selector used correctly'
);
expect(mapStateToProps(state, ownProps).excludeConnectionsTab).toBe(false);
expect(mapStateToProps(state, ownProps).excludeConnectionsTab).toBe(true);
});

it('should map selected target references from viewer when no ownProps and targetDoc', () => {
const ownProps = { storeKey: 'library' };
state.documentViewer.targetDoc = Immutable.fromJS({ _id: 'targetDocId' });
state.relationships.list.connectionsGroups = [];
expect(mapStateToProps(state, ownProps).references).toBe(
'Target references selector used correctly'
);
Expand All @@ -255,7 +256,7 @@ describe('DocumentSidePanel', () => {

it('should map connectionsGroups', () => {
const ownProps = { storeKey: 'library' };
expect(mapStateToProps(state, ownProps).connectionsGroups).toBe('connectionsGroups');
expect(mapStateToProps(state, ownProps).connectionsGroups).toEqual(['connectionsGroups']);
});

it('should map default language', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Array [
"type": "library.sidepanel.tab/SET",
"value": "",
},
Object {
"type": "library.sidepanel.trigger/SET",
"value": "library",
},
Object {
"doc": Object {
"sharedId": "doc",
Expand Down
1 change: 1 addition & 0 deletions app/react/Library/actions/specs/libraryActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ describe('libraryActions', () => {

const expectedActions = [
{ type: 'library.sidepanel.references/SET', value: 'referencesResponse' },
{ type: 'relationships/list/sharedId/SET', value: 'id' },
];

const store = mockStore({ locale: 'es' });
Expand Down
5 changes: 2 additions & 3 deletions app/react/Viewer/components/ViewerComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PDFView from '../PDFView';
export class ViewerComponent extends Component {
constructor(props, context) {
super(props, context);
props.setSidepanelTrigger('entityViewer');
props.setSidepanelTrigger();
}

render() {
Expand All @@ -31,8 +31,7 @@ ViewerComponent.propTypes = {
setSidepanelTrigger: PropTypes.func.isRequired,
};

const setSidepanelTrigger = name => dispatch =>
dispatch(actions.set('library.sidepanel.trigger', name));
const setSidepanelTrigger = () => actions.set('library.sidepanel.trigger', 'entityViewer');

const mapStateToProps = state => {
const entity = state.documentViewer.doc.get('_id')
Expand Down

0 comments on commit 396064a

Please sign in to comment.