From a0d084029fb95226fa23270b85ad6e194443b3a9 Mon Sep 17 00:00:00 2001 From: Nicolas Riesco Date: Fri, 18 May 2018 19:21:04 +0100 Subject: [PATCH] ConnectButton: fix multiple bugs * 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 --- .../ConnectButton/ConnectButton.react.js | 17 +++--- .../ConnectButton/ConnectButton.test.js | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/components/Settings/ConnectButton/ConnectButton.react.js b/app/components/Settings/ConnectButton/ConnectButton.react.js index d8038b37a..58c3cd355 100644 --- a/app/components/Settings/ConnectButton/ConnectButton.react.js +++ b/app/components/Settings/ConnectButton/ConnectButton.react.js @@ -23,10 +23,10 @@ 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'; } /** @@ -34,7 +34,7 @@ export default class ConnectButton extends Component { */ isConnected() { const status = Number(this.props.connectRequest.status); - return (status >= 200 || status < 300); + return (status >= 200 && status < 300); } /** @@ -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); } /** @@ -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 =
{errorMessage}
; } else if (this.isConnected() && this.isSaved()) { buttonText = 'Save changes'; buttonClick = connect; + } else { buttonText = 'Connect'; buttonClick = connect; diff --git a/test/app/components/Settings/ConnectButton/ConnectButton.test.js b/test/app/components/Settings/ConnectButton/ConnectButton.test.js index 1646d2045..3476df133 100644 --- a/test/app/components/Settings/ConnectButton/ConnectButton.test.js +++ b/test/app/components/Settings/ConnectButton/ConnectButton.test.js @@ -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(); + + 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(); + + 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(); + + expect(button.instance().connectionFailed()).toBe(true); + }); + it('should verify Connection Request Error', () => { const connect = function() {}; const connectRequest = {