Skip to content

Commit d545a19

Browse files
Fixing isAdmin check
1 parent 264d42c commit d545a19

File tree

2 files changed

+72
-38
lines changed

2 files changed

+72
-38
lines changed

desktop/core/src/desktop/js/apps/admin/Overview/OverviewTab.test.tsx

+67-24
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,33 @@
1616

1717
import '@testing-library/jest-dom';
1818
import React from 'react';
19-
import { render, screen, waitFor } from '@testing-library/react';
19+
import { queryByText, render, screen, waitFor } from '@testing-library/react';
2020
import userEvent from '@testing-library/user-event';
2121
import Overview from './OverviewTab';
2222
import Analytics from './Analytics';
23-
// import ApiHelper from '../../../api/apiHelper';
24-
// import saveCollectUsagePreference from
23+
import saveCollectUsagePreference from '../Overview/Analytics';
24+
import * as hueConfigModule from '../../../config/hueConfig';
25+
26+
import { post } from '../../../api/utils';
2527

2628
jest.mock('./ConfigStatus', () => () => <div>MockedConfigStatusComponent</div>);
2729
jest.mock('./Examples', () => () => <div>MockedExamplesComponent</div>);
2830
jest.mock('./Analytics', () => () => <div>MockedAnalyticsComponent</div>);
2931

30-
jest.mock('../../../api/apiHelper', () => ({
31-
...jest.requireActual('../../../api/apiHelper'),
32-
updatePreferences: jest.fn(() => Promise.resolve({ status: 0, data: 'Success message' }))
32+
jest.mock('../../../config/hueConfig', () => ({
33+
getLastKnownConfig: jest.fn()
3334
}));
35+
3436
describe('OverviewTab', () => {
35-
beforeEach(() => {
36-
jest.clearAllMocks();
37+
beforeEach(() => {
38+
(hueConfigModule.getLastKnownConfig as jest.Mock).mockReturnValue({
39+
hue_config: { is_admin: true }
3740
});
41+
});
42+
afterEach(() => {
43+
jest.clearAllMocks();
44+
});
45+
3846
test('renders the Tabs with the correct tab labels', () => {
3947
render(<Overview />);
4048
expect(screen.getByText('ConfigStatus')).toBeInTheDocument();
@@ -56,34 +64,69 @@ describe('OverviewTab', () => {
5664
).toBeInTheDocument();
5765
});
5866

59-
jest.mock('../../../config/hueConfig', () => ({
60-
getLastKnownConfig: () => ({ hue_config: { is_admin: false } })
61-
}));
6267

6368
test('it should not render Overview for non-admin users', () => {
64-
const { queryByTestId } = render(<Overview />);
65-
const overviewComponent = queryByTestId('overview-component');
69+
(hueConfigModule.getLastKnownConfig as jest.Mock).mockReturnValue({
70+
hue_config: { is_admin: false }
71+
});
6672

67-
expect(overviewComponent).toBeNull();
73+
const { queryByText } = render(<Overview />);
74+
const trademarkText = queryByText('Hue and the Hue logo are trademarks of Cloudera, Inc.');
75+
expect(trademarkText).toBeNull();
6876
});
6977

70-
//verify table contents
7178

79+
// jest.mock('../Overview/Analytics', () => ({
80+
// __esModule: true,
81+
// saveCollectUsagePreference: jest.fn(() => Promise.resolve({ status: 0 }))
82+
// }));
83+
// jest.mock('../../../api/utils', () => ({
84+
// post: jest.fn(() => Promise.resolve({ status: 0 }))
85+
// }));
86+
87+
// describe('Analytics Component', () => {
88+
// beforeEach(() => {
89+
// jest.clearAllMocks();
90+
// });
91+
// test('renders Analytics tab and can interact with the checkbox', async () => {
92+
// render(<Analytics />);
93+
// const checkbox = screen.getByRole('checkbox', {
94+
// name: /help improve hue with anonymous usage analytics\./i
95+
// });
96+
97+
// expect(checkbox).not.toBeChecked();
98+
// await userEvent.click(checkbox);
99+
// await waitFor(() => expect(checkbox).toBeChecked());
100+
// expect(saveCollectUsagePreference).toHaveBeenCalledWith(true);
101+
102+
// await userEvent.click(checkbox);
103+
// await waitFor(() => expect(checkbox).not.toBeChecked());
104+
// expect(saveCollectUsagePreference).toHaveBeenCalledWith(false);
105+
// });
106+
// });
72107
// describe('Analytics Component', () => {
73-
// test('renders Analytics tab and can interact with the checkbox', async () => {
74-
// render(<Overview />);
75-
// const analyticsTabButton = screen.getByText('Analytics');
76-
// userEvent.click(analyticsTabButton);
108+
// beforeEach(() => {
109+
// jest.clearAllMocks();
110+
// });
77111

78-
// const checkbox = await screen.findByTitle('Check to enable usage analytics');
112+
// test('renders Analytics tab and can interact with the checkbox', async () => {
113+
// render(<Analytics />);
114+
// const checkbox = screen.getByRole('checkbox', {
115+
// name: /help improve hue with anonymous usage analytics\./i
116+
// });
79117

80118
// expect(checkbox).not.toBeChecked();
81-
// userEvent.click(checkbox);
119+
// await userEvent.click(checkbox);
82120
// await waitFor(() => expect(checkbox).toBeChecked());
83-
// expect(ApiHelper.updatePreferences).toHaveBeenCalledWith({ collect_usage: 'on' });
84-
// userEvent.click(checkbox);
121+
122+
// // Now that we are mocking 'post', we assert that the post API call was made
123+
// expect(post).toHaveBeenCalledWith('/about/update_preferences', { collect_usage: 'on' });
124+
125+
// await userEvent.click(checkbox);
85126
// await waitFor(() => expect(checkbox).not.toBeChecked());
86-
// expect(ApiHelper.updatePreferences).toHaveBeenCalledWith({ collect_usage: null });
127+
128+
// // Again we check that the 'post' was called with the new state ('null' for off)
129+
// expect(post).toHaveBeenCalledWith('/about/update_preferences', { collect_usage: null });
87130
// });
88131
// });
89132
});

desktop/core/src/desktop/js/apps/admin/Overview/OverviewTab.tsx

+5-14
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ import Examples from './Examples';
2020
import ConfigStatus from './ConfigStatus';
2121
import Analytics from './Analytics';
2222
import { i18nReact } from '../../../utils/i18nReact';
23-
// import { getLastKnownConfig } from '../../../config/hueConfig';
23+
import { getLastKnownConfig } from '../../../config/hueConfig';
2424
import './Overview.scss';
2525

2626
const Overview = (): JSX.Element | null => {
2727
const { t } = i18nReact.useTranslation();
28-
// const [isAdmin, setIsAdmin] = useState(false);
29-
30-
// useEffect(() => {
31-
// const config = getLastKnownConfig();
32-
// setIsAdmin(config?.hue_config.is_admin ?? false);
33-
// }, []);
3428

29+
const config = getLastKnownConfig();
30+
const isAdmin = config?.hue_config.is_admin ?? false;
3531
const items = [
3632
{
3733
label: t('ConfigStatus'),
@@ -50,19 +46,14 @@ const Overview = (): JSX.Element | null => {
5046
}
5147
];
5248

53-
// if (!isAdmin) {
54-
// // console.log('User is not admin, rendering null.');
55-
// return null;
56-
// }
57-
58-
return (
49+
return isAdmin ? (
5950
<div className="hue-overview-component">
6051
<Tabs tabPosition="left" items={items} />
6152
<div className="config__trademark-text">
6253
Hue and the Hue logo are trademarks of Cloudera, Inc.
6354
</div>
6455
</div>
65-
);
56+
) : null;
6657
};
6758

6859
export default Overview;

0 commit comments

Comments
 (0)