Skip to content

Commit

Permalink
Use install properties to check if triggers is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-edouard.breteche authored and tekton-robot committed May 18, 2020
1 parent 2bd56da commit 76b33c1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/containers/App/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
31 changes: 8 additions & 23 deletions src/containers/SideNav/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 (
<CarbonSideNav
Expand Down Expand Up @@ -224,7 +204,7 @@ class SideNav extends Component {
>
TaskRuns
</SideNavMenuItem>
{isTriggersInstalled && (
{this.props.isTriggersInstalled && (
<>
<SideNavMenuItem
element={NavLink}
Expand Down Expand Up @@ -358,10 +338,15 @@ class SideNav extends Component {
}
}

SideNav.defaultProps = {
isTriggersInstalled: false
};

/* istanbul ignore next */
const mapStateToProps = state => ({
extensions: getExtensions(state),
isReadOnly: isReadOnly(state),
isTriggersInstalled: isTriggersInstalled(state),
namespace: getSelectedNamespace(state)
});

Expand Down
16 changes: 4 additions & 12 deletions src/containers/SideNav/SideNav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -62,17 +62,15 @@ 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);
const store = mockStore({
extensions: { byName: {} },
namespaces: { byName: {} }
});
jest
.spyOn(API, 'getCustomResource')
.mockImplementation(() => Promise.resolve());
const { queryByText } = renderWithRouter(
<Provider store={store}>
<SideNavContainer />
Expand Down Expand Up @@ -589,17 +587,14 @@ 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);
const store = mockStore({
extensions: { byName: {} },
namespaces: { byName: {} }
});
jest
.spyOn(API, 'getCustomResource')
.mockImplementation(() => Promise.resolve());
const { queryByText } = renderWithRouter(
<Provider store={store}>
<SideNavContainer />
Expand All @@ -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(
<Provider store={store}>
<SideNavContainer isReadOnly />
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 12 additions & 1 deletion src/reducers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import {
isFetchingSecrets,
isFetchingTaskRuns,
isFetchingTasks,
isReadOnly
isReadOnly,
isTriggersInstalled
} from '.';
import * as clusterTaskSelectors from './clusterTasks';
import * as extensionSelectors from './extensions';
Expand Down Expand Up @@ -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
);
});
4 changes: 4 additions & 0 deletions src/reducers/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ export function isReadOnly(state) {
return state.ReadOnly;
}

export function isTriggersInstalled(state) {
return (state.TriggersNamespace && state.TriggersVersion) || false;
}

export default properties;
1 change: 1 addition & 0 deletions src/reducers/properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ it('INSTALL_PROPERTIES_SUCCESS', () => {

const state = propertiesReducer({}, action);
expect(selectors.isReadOnly(state)).toBe(false);
expect(selectors.isTriggersInstalled(state)).toBe(false);
});

0 comments on commit 76b33c1

Please sign in to comment.