-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from JPinkney/override-params2
Add banner that triggers when websockets arent available
- Loading branch information
Showing
5 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/components/WebSocketBannerAlert/__tests__/WebSocketBannerAlert.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2018-2020 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import React from 'react'; | ||
import { container } from '../../../inversify.config'; | ||
import WebSocketBannerAlert from '../'; | ||
import { CheWorkspaceClient } from '../../../services/cheWorkspaceClient'; | ||
import { Provider } from 'react-redux'; | ||
import { FakeStoreBuilder } from '../../../store/__mocks__/storeBuilder'; | ||
import { BrandingData } from '../../../services/bootstrap/branding.constant'; | ||
import { render, RenderResult } from '@testing-library/react'; | ||
|
||
const failingWebSocketName = 'Failing websocket'; | ||
const failingMessage = 'WebSocket connections are failing'; | ||
|
||
class mockCheWorkspaceClient extends CheWorkspaceClient { | ||
get failingWebSockets() { return [failingWebSocketName]; } | ||
} | ||
|
||
const store = new FakeStoreBuilder().withBranding({ | ||
docs: { | ||
webSocketTroubleshooting: 'http://sample_documentation' | ||
} | ||
} as BrandingData).build(); | ||
|
||
describe('WebSocketBannerAlert component', () => { | ||
it('should show error message when error found before mounting', () => { | ||
container.rebind(CheWorkspaceClient).to(mockCheWorkspaceClient).inSingletonScope(); | ||
const component = renderComponent(<WebSocketBannerAlert />); | ||
container.rebind(CheWorkspaceClient).to(CheWorkspaceClient).inSingletonScope(); | ||
expect(component.getAllByText(failingMessage, { | ||
exact: false | ||
}).length).toEqual(1); | ||
}); | ||
|
||
it('should show error message when error found after mounting', () => { | ||
const comp = ( | ||
<Provider store={store}> | ||
<WebSocketBannerAlert /> | ||
</Provider> | ||
); | ||
const component = renderComponent(comp); | ||
expect(component.queryAllByText(failingMessage, { | ||
exact: false | ||
})).toEqual([]); | ||
container.rebind(CheWorkspaceClient).to(mockCheWorkspaceClient).inSingletonScope(); | ||
component.rerender(comp); | ||
container.rebind(CheWorkspaceClient).to(CheWorkspaceClient).inSingletonScope(); | ||
expect(component.getAllByText(failingMessage, { | ||
exact: false | ||
}).length).toEqual(1); | ||
}); | ||
|
||
it('should not show error message if none is present', () => { | ||
const component = renderComponent(<WebSocketBannerAlert />); | ||
expect(component.queryAllByText(failingMessage, { | ||
exact: false | ||
})).toEqual([]); | ||
}); | ||
|
||
}); | ||
|
||
function renderComponent( | ||
component: React.ReactElement | ||
): RenderResult { | ||
return render( | ||
<Provider store={store}> | ||
{component} | ||
</Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2018-2020 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { Banner } from '@patternfly/react-core'; | ||
import React from 'react'; | ||
import { connect, ConnectedProps } from 'react-redux'; | ||
import { container } from '../../inversify.config'; | ||
import { CheWorkspaceClient } from '../../services/cheWorkspaceClient'; | ||
import { AppState } from '../../store'; | ||
|
||
type Props = MappedProps & {}; | ||
|
||
type State = { | ||
erroringWebSockets: string[]; | ||
}; | ||
|
||
class WebSocketBannerAlert extends React.PureComponent<Props, State> { | ||
private readonly cheWorkspaceClient: CheWorkspaceClient; | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
this.cheWorkspaceClient = container.get(CheWorkspaceClient); | ||
this.state = { | ||
erroringWebSockets: this.cheWorkspaceClient.failingWebSockets, | ||
}; | ||
} | ||
|
||
public componentWillUnmount() { | ||
this.cheWorkspaceClient.removeWebSocketFailedListener(); | ||
} | ||
|
||
public componentDidMount() { | ||
this.cheWorkspaceClient.onWebSocketFailed(() => { | ||
this.setState({ | ||
erroringWebSockets: this.cheWorkspaceClient.failingWebSockets, | ||
}); | ||
}); | ||
} | ||
|
||
render() { | ||
if (this.state.erroringWebSockets.length === 0) { | ||
return null; | ||
} | ||
|
||
const webSocketTroubleshootingDocs = this.props.brandingStore.data.docs | ||
.webSocketTroubleshooting; | ||
return ( | ||
<Banner className="pf-u-text-align-center" variant="warning"> | ||
WebSocket connections are failing. Refer to " | ||
<a href={webSocketTroubleshootingDocs} rel="noreferrer" target="_blank"> | ||
Network Troubleshooting | ||
</a>" | ||
in the user guide. | ||
</Banner> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: AppState) => ({ | ||
brandingStore: state.branding, | ||
}); | ||
|
||
const connector = connect(mapStateToProps); | ||
|
||
type MappedProps = ConnectedProps<typeof connector>; | ||
export default connector(WebSocketBannerAlert); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters