Skip to content

Commit

Permalink
Merge pull request #59 from EmmanuelDemey/master
Browse files Browse the repository at this point in the history
Update React + Add unit test
  • Loading branch information
NicoLaval authored Aug 9, 2019
2 parents 97b8bac + f765cda commit 48e8d32
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 75 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ yarn-error.log*
/swagger
.cache
cypress/videos
cypress/screenshots
cypress/screenshots

coverage
127 changes: 75 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@
"moment": "2.23.0",
"moment-range": "3.0.3",
"prop-types": "15.7.2",
"react": "16.8.6",
"react": "16.9.0",
"react-16-bootstrap-date-picker": "5.1.2",
"react-bootstrap": "0.31.5",
"react-bootstrap-table": "4.3.0",
"react-bootstrap-tabs": "1.0.2",
"react-d3-tree": "1.10.3",
"react-dom": "16.8.6",
"react-dom": "16.9.0",
"react-draft-wysiwyg": "1.12.0",
"react-dropzone": "^10.1.5",
"react-loading": "2.0.3",
"react-modal": "3.1.8",
"react-redux": "7.1.0",
"react-router-dom": "4.3.1",
"react-router-dom": "5.0.1",
"react-router-hash-link": "1.2.0",
"react-scripts": "3.0.1",
"react-select": "1.1.0",
Expand Down
38 changes: 38 additions & 0 deletions src/js/actions/classifications/correspondences/association.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import getCorrespondenceAssociation from './association';
import * as A from 'js/actions/constants';
import api from 'js/remote-api/classifications-api';

const dispatch = jest.fn();
jest.mock('js/remote-api/classifications-api');

describe('Associations actions', () => {
it('should call dispatch LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_SUCCESS action with the right data', async () => {
api.getCorrespondenceAssociation = function() {
return Promise.resolve('results');
};
await getCorrespondenceAssociation(1, 2)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATION,
payload: { correspondenceId: 1, associationId: 2 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATION_SUCCESS,
payload: { correspondenceId: 1, associationId: 2, results: 'results' },
});
});

it('should call dispatch LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_FAILURE action with an error object', async () => {
api.getCorrespondenceAssociation = function() {
return Promise.reject('error');
};
await getCorrespondenceAssociation(1, 2)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATION,
payload: { correspondenceId: 1, associationId: 2 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATION_FAILURE,
payload: { err: 'error', correspondenceId: 1, associationId: 2 },
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import getCorrespondenceAssociations from './associations';
import * as A from 'js/actions/constants';
import api from 'js/remote-api/classifications-api';

const dispatch = jest.fn();
jest.mock('js/remote-api/classifications-api');

describe('Associations actions', () => {
it('should call dispatch LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_SUCCESS action with the right data', async () => {
api.getCorrespondenceAssociations = function() {
return Promise.resolve('results');
};
await getCorrespondenceAssociations(1)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS,
payload: { id: 1 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_SUCCESS,
payload: { id: 1, results: 'results' },
});
});

it('should call dispatch LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_FAILURE action with an error object', async () => {
api.getCorrespondenceAssociations = function() {
return Promise.reject('error');
};
await getCorrespondenceAssociations(1)(dispatch);
expect(dispatch).toHaveBeenCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS,
payload: { id: 1 },
});
expect(dispatch).toHaveBeenLastCalledWith({
type: A.LOAD_CLASSIFICATION_CORRESPONDENCE_ASSOCIATIONS_FAILURE,
payload: { err: 'error', id: 1 },
});
});
});
30 changes: 15 additions & 15 deletions src/js/components/operations/families/edition/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ const defaultFamily = {
abstractLg1: '',
abstractLg2: '',
};

const setInitialState = props => {
return {
serverSideError: '',
family: {
...defaultFamily,
...props.family,
},
};
};
class OperationsFamilyEdition extends Component {
static propTypes = {
family: PropTypes.object.isRequired,
Expand All @@ -30,25 +40,15 @@ class OperationsFamilyEdition extends Component {

constructor(props) {
super(props);
this.state = this.setInitialState(props);
this.state = setInitialState(props);
}

componentWillReceiveProps(nextProps) {
if (nextProps.family.id !== this.props.family.id) {
this.setState(this.setInitialState(nextProps));
}
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.family.id !== prevState.family.id) {
return setInitialState(nextProps);
} else return null;
}

setInitialState = props => {
return {
serverSideError: '',
family: {
...defaultFamily,
...props.family,
},
};
};

onChange = e => {
this.setState({
serverSideError: '',
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/operations/families/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NOT_LOADED, LOADED } from 'js/constants';
import { getFamilies } from 'js/reducers/index';

class FamiliesHomeContainer extends Component {
componentWillMount() {
componentDidMount() {
if (this.props.status !== LOADED) {
this.props.loadFamiliesList();
}
Expand Down
10 changes: 7 additions & 3 deletions src/js/components/shared/pagination/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ class Pagination extends Component {
currentPage: Number(targetPage),
});
}
componentWillReceiveProps({ itemEls, itemsPerPage }) {
const { currentPage } = this.state;

static getDerivedStateFromProps({ itemEls, itemsPerPage }, previousState) {
const { currentPage } = previousState;
const pageMax = Math.ceil(itemEls.length / itemsPerPage) || 1;
if (currentPage > pageMax) this.goToPage(pageMax);
if (currentPage > pageMax) {
return { currentPage: Number(pageMax) };
}
return null;
}

render() {
Expand Down

0 comments on commit 48e8d32

Please sign in to comment.