Skip to content

Commit

Permalink
refactor(RHIF-125): Apply useChrome everywhere (#1801)
Browse files Browse the repository at this point in the history
Implements https://issues.redhat.com/browse/RHIF-125.

This replaces the remaining occurrences of direct call to insights.chrome
with useChrome hook.
  • Loading branch information
gkarat authored Mar 22, 2023
1 parent fc6d49b commit a816087
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/components/GeneralInfo/EditButton/EditButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';

import { usePermissionsWithContext } from '@redhat-cloud-services/frontend-components-utilities/RBACHook';
import useChrome from '@redhat-cloud-services/frontend-components/useChrome';

import { PencilAltIcon } from '@patternfly/react-icons';

Expand Down Expand Up @@ -46,7 +47,9 @@ EditButtonUnknownPermissions.propTypes = {
};

const EditButtonWrapper = ({ writePermissions, ...props }) => {
if (insights.chrome.isProd || writePermissions || permissionsCache) {
const { isProd } = useChrome();

if (isProd?.() || writePermissions || permissionsCache) {
return <InnerButton {...props} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PencilAltIcon } from '@patternfly/react-icons';
import EditButton from './EditButton';

jest.mock('@redhat-cloud-services/frontend-components-utilities/RBACHook', () => ({
esModule: true,
__esModule: true,
usePermissionsWithContext: () => ({ hasAccess: false })
}));

Expand Down Expand Up @@ -39,21 +39,6 @@ describe('EditButton with no access', () => {
expect(wrapper.find('a')).toHaveLength(0);
});

it('render on production', () => {
const tmp = insights.chrome.isProd;
insights.chrome.isProd = true;

const wrapper = mount(<EditButton
onClick={onClick}
link={link}
/>);

expect(wrapper.find(PencilAltIcon)).toHaveLength(1);
expect(wrapper.find('a').props().href).toEqual('http://localhost:5000//some-link');

insights.chrome.isProd = tmp;
});

it('render when write permissions are set to true', () => {
const wrapper = mount(<EditButton
onClick={onClick}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { mount } from 'enzyme';
import { PencilAltIcon } from '@patternfly/react-icons';

import EditButton from './EditButton';

jest.mock('@redhat-cloud-services/frontend-components-utilities/RBACHook', () => ({
__esModule: true,
usePermissionsWithContext: () => ({ hasAccess: false })
}));

jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({
__esModule: true,
default: () => ({
isProd: () => true
})
}));

describe('EditButton with no access', () => {
let onClick;
let link;

beforeEach(() => {
onClick = jest.fn();
link = 'some-link';
});

it('render on production', () => {
const wrapper = mount(<EditButton
onClick={onClick}
link={link}
/>);

expect(wrapper.find(PencilAltIcon)).toHaveLength(1);
expect(wrapper.find('a').props().href).toEqual('http://localhost:5000//some-link');
});
});
2 changes: 1 addition & 1 deletion src/routes/InventoryDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const Inventory = () => {
}

useEffect(() => {
insights?.chrome?.appObjectId?.(entity?.id);
chrome?.appObjectId?.(entity?.id);
}, [entity?.id]);

const onTabSelect = useCallback((_, activeApp, appName) => {
Expand Down

0 comments on commit a816087

Please sign in to comment.