Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
ConnectButton: fix multiple bugs
Browse files Browse the repository at this point in the history
* Fixed the code parsing a connection error message.

* Ensure error message is a string.

* Ensure isConnected() and isSaved() returns false when status code is
  500.

* Added tests.

Closes #342
  • Loading branch information
n-riesco committed May 18, 2018
1 parent 71fe981 commit bf60ecc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/components/Settings/ConnectButton/ConnectButton.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ export default class ConnectButton extends Component {
}

/**
* @returns {boolean} true if waiting for a response to a connection request
*/
isConnecting() {
return this.props.connectRequest.status === 'loading';
* @returns {boolean} true if waiting for a response to a connection request
*/
isConnecting() {
return this.props.connectRequest.status === 'loading';
}

/**
* @returns {boolean} true if successfully connected to database
*/
isConnected() {
const status = Number(this.props.connectRequest.status);
return (status >= 200 || status < 300);
return (status >= 200 && status < 300);
}

/**
Expand All @@ -56,7 +56,7 @@ export default class ConnectButton extends Component {
*/
isSaved() {
const status = Number(this.props.saveConnectionsRequest.status);
return (status >= 200 || status < 300);
return (status >= 200 && status < 300);
}

/**
Expand Down Expand Up @@ -89,18 +89,19 @@ export default class ConnectButton extends Component {
buttonClick = connect;

const connectErrorMessage = pathOr(
null, ['content', 'error'], connectRequest
null, ['content', 'error', 'message'], connectRequest
);
const saveErrorMessage = pathOr(
null, ['content', 'error', 'message'], saveConnectionsRequest
);
const genericErrorMessage = 'Hm... had trouble connecting.';
const errorMessage = connectErrorMessage || saveErrorMessage || genericErrorMessage;
const errorMessage = String(connectErrorMessage || saveErrorMessage || genericErrorMessage);
error = <div className={'errorMessage'}>{errorMessage}</div>;

} else if (this.isConnected() && this.isSaved()) {
buttonText = 'Save changes';
buttonClick = connect;

} else {
buttonText = 'Connect';
buttonClick = connect;
Expand Down
60 changes: 60 additions & 0 deletions test/app/components/Settings/ConnectButton/ConnectButton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,66 @@ describe('Connect Button Test', () => {
configure({ adapter: new Adapter() });
});

it('should handle malformatted Connection Request Error', () => {
const connect = function() {};
const connectRequest = {
status: 500,
content: {error: {message: {}}}
};
const saveConnectionsRequest = {
};
const editMode = true;

const button = mount(<ConnectButton
connect={connect}
connectRequest={connectRequest}
saveConnectionsRequest={saveConnectionsRequest}
editMode={editMode}
/>);

expect(button.find('.errorMessage').length).toBe(1);
});

it('should handle malformatted Save Connections Request Error', () => {
const connect = function() {};
const connectRequest = {
status: 200
};
const saveConnectionsRequest = {
status: 500,
content: {error: {message: {}}}
};
const editMode = true;

const button = mount(<ConnectButton
connect={connect}
connectRequest={connectRequest}
saveConnectionsRequest={saveConnectionsRequest}
editMode={editMode}
/>);

expect(button.find('.errorMessage').length).toBe(1);
});

it('should handle Connection Request Error Status 500', () => {
const connect = function() {};
const connectRequest = {
status: 500
};
const saveConnectionsRequest = {
};
const editMode = true;

const button = mount(<ConnectButton
connect={connect}
connectRequest={connectRequest}
saveConnectionsRequest={saveConnectionsRequest}
editMode={editMode}
/>);

expect(button.instance().connectionFailed()).toBe(true);
});

it('should verify Connection Request Error', () => {
const connect = function() {};
const connectRequest = {
Expand Down

0 comments on commit bf60ecc

Please sign in to comment.