From 92d664689a0d15d90ce31aaf3e978812567575cb Mon Sep 17 00:00:00 2001 From: "charles-edouard.breteche" Date: Tue, 12 May 2020 17:50:38 +0200 Subject: [PATCH] Use install properties to check if triggers is installed --- src/containers/App/App.test.js | 1 + src/containers/SideNav/SideNav.js | 31 +++++++------------------- src/containers/SideNav/SideNav.test.js | 16 ++++--------- src/reducers/index.js | 4 ++++ src/reducers/index.test.js | 13 ++++++++++- src/reducers/properties.js | 4 ++++ src/reducers/properties.test.js | 1 + 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/containers/App/App.test.js b/src/containers/App/App.test.js index 388cd4eb8..f990bb6c2 100644 --- a/src/containers/App/App.test.js +++ b/src/containers/App/App.test.js @@ -24,6 +24,7 @@ import * as selectors from '../../reducers'; beforeEach(() => { jest.spyOn(API, 'getPipelines').mockImplementation(() => {}); jest.spyOn(selectors, 'isReadOnly').mockImplementation(() => true); + jest.spyOn(selectors, 'isTriggersInstalled').mockImplementation(() => false); }); it('App renders successfully', () => { diff --git a/src/containers/SideNav/SideNav.js b/src/containers/SideNav/SideNav.js index 61f8c6be4..3f7186858 100644 --- a/src/containers/SideNav/SideNav.js +++ b/src/containers/SideNav/SideNav.js @@ -30,25 +30,19 @@ import { selectNamespace } from '../../actions/namespaces'; import { getExtensions, getSelectedNamespace, - isReadOnly + isReadOnly, + isTriggersInstalled } from '../../reducers'; -import { getCustomResource } from '../../api'; import './SideNav.scss'; class SideNav extends Component { - state = { - isTriggersInstalled: false - }; - componentDidMount() { const { match } = this.props; if (match && match.params.namespace) { this.props.selectNamespace(match.params.namespace); } - - this.checkTriggersInstalled(); } componentDidUpdate(prevProps) { @@ -150,22 +144,8 @@ class SideNav extends Component { history.push('/'); }; - checkTriggersInstalled() { - getCustomResource({ - group: 'apiextensions.k8s.io', - version: 'v1beta1', - type: 'customresourcedefinitions', - name: 'eventlisteners.triggers.tekton.dev' - }) - .then(() => { - this.setState({ isTriggersInstalled: true }); - }) - .catch(() => {}); - } - render() { const { extensions, intl, namespace } = this.props; - const { isTriggersInstalled } = this.state; return ( TaskRuns - {isTriggersInstalled && ( + {this.props.isTriggersInstalled && ( <> ({ extensions: getExtensions(state), isReadOnly: isReadOnly(state), + isTriggersInstalled: isTriggersInstalled(state), namespace: getSelectedNamespace(state) }); diff --git a/src/containers/SideNav/SideNav.test.js b/src/containers/SideNav/SideNav.test.js index 322e818ed..ed33d3497 100644 --- a/src/containers/SideNav/SideNav.test.js +++ b/src/containers/SideNav/SideNav.test.js @@ -20,11 +20,11 @@ import { ALL_NAMESPACES, paths, urls } from '@tektoncd/dashboard-utils'; import { renderWithRouter } from '../../utils/test'; import SideNavContainer, { SideNavWithIntl as SideNav } from './SideNav'; -import * as API from '../../api'; import * as selectors from '../../reducers'; beforeEach(() => { jest.spyOn(selectors, 'isReadOnly').mockImplementation(() => true); + jest.spyOn(selectors, 'isTriggersInstalled').mockImplementation(() => false); }); it('SideNav renders with extensions', () => { @@ -62,7 +62,8 @@ it('SideNav renders with extensions', () => { }); it('SideNav renders with triggers', async () => { - jest.spyOn(selectors, 'isReadOnly').mockImplementation(() => false); + selectors.isReadOnly.mockImplementation(() => false); + selectors.isTriggersInstalled.mockImplementation(() => true); const middleware = [thunk]; const mockStore = configureStore(middleware); @@ -70,9 +71,6 @@ it('SideNav renders with triggers', async () => { extensions: { byName: {} }, namespaces: { byName: {} } }); - jest - .spyOn(API, 'getCustomResource') - .mockImplementation(() => Promise.resolve()); const { queryByText } = renderWithRouter( @@ -589,7 +587,7 @@ it('SideNav updates namespace in URL', async () => { }); it('SideNav renders import in not read-only mode', async () => { - jest.spyOn(selectors, 'isReadOnly').mockImplementation(() => false); + selectors.isReadOnly.mockImplementation(() => false); const middleware = [thunk]; const mockStore = configureStore(middleware); @@ -597,9 +595,6 @@ it('SideNav renders import in not read-only mode', async () => { extensions: { byName: {} }, namespaces: { byName: {} } }); - jest - .spyOn(API, 'getCustomResource') - .mockImplementation(() => Promise.resolve()); const { queryByText } = renderWithRouter( @@ -615,9 +610,6 @@ it('SideNav does not render import in read-only mode', async () => { extensions: { byName: {} }, namespaces: { byName: {} } }); - jest - .spyOn(API, 'getCustomResource') - .mockImplementation(() => Promise.resolve()); const { queryByText } = renderWithRouter( diff --git a/src/reducers/index.js b/src/reducers/index.js index 060b49571..34f86b8c8 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -482,3 +482,7 @@ export function isFetchingEventListeners(state) { export function isReadOnly(state) { return propertiesSelectors.isReadOnly(state.properties); } + +export function isTriggersInstalled(state) { + return propertiesSelectors.isTriggersInstalled(state.properties); +} diff --git a/src/reducers/index.test.js b/src/reducers/index.test.js index 0900da39f..b188bfa2a 100644 --- a/src/reducers/index.test.js +++ b/src/reducers/index.test.js @@ -49,7 +49,8 @@ import { isFetchingSecrets, isFetchingTaskRuns, isFetchingTasks, - isReadOnly + isReadOnly, + isTriggersInstalled } from '.'; import * as clusterTaskSelectors from './clusterTasks'; import * as extensionSelectors from './extensions'; @@ -504,3 +505,13 @@ it('isReadOnly', () => { expect(isReadOnly(state)).toBe(true); expect(propertiesSelectors.isReadOnly).toHaveBeenCalledWith(state.properties); }); + +it('isTriggersInstalled', () => { + jest + .spyOn(propertiesSelectors, 'isTriggersInstalled') + .mockImplementation(() => true); + expect(isTriggersInstalled(state)).toBe(true); + expect(propertiesSelectors.isTriggersInstalled).toHaveBeenCalledWith( + state.properties + ); +}); diff --git a/src/reducers/properties.js b/src/reducers/properties.js index 5a1e84803..c28e3f546 100644 --- a/src/reducers/properties.js +++ b/src/reducers/properties.js @@ -25,4 +25,8 @@ export function isReadOnly(state) { return state.ReadOnly; } +export function isTriggersInstalled(state) { + return (state.TriggersNamespace && state.TriggersVersion) || false; +} + export default properties; diff --git a/src/reducers/properties.test.js b/src/reducers/properties.test.js index 9d0467376..cc2d3b75f 100644 --- a/src/reducers/properties.test.js +++ b/src/reducers/properties.test.js @@ -28,4 +28,5 @@ it('INSTALL_PROPERTIES_SUCCESS', () => { const state = propertiesReducer({}, action); expect(selectors.isReadOnly(state)).toBe(false); + expect(selectors.isTriggersInstalled(state)).toBe(false); });