16
16
17
17
import '@testing-library/jest-dom' ;
18
18
import React from 'react' ;
19
- import { render , screen , waitFor } from '@testing-library/react' ;
19
+ import { queryByText , render , screen , waitFor } from '@testing-library/react' ;
20
20
import userEvent from '@testing-library/user-event' ;
21
21
import Overview from './OverviewTab' ;
22
22
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' ;
25
27
26
28
jest . mock ( './ConfigStatus' , ( ) => ( ) => < div > MockedConfigStatusComponent</ div > ) ;
27
29
jest . mock ( './Examples' , ( ) => ( ) => < div > MockedExamplesComponent</ div > ) ;
28
30
jest . mock ( './Analytics' , ( ) => ( ) => < div > MockedAnalyticsComponent</ div > ) ;
29
31
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 ( )
33
34
} ) ) ;
35
+
34
36
describe ( 'OverviewTab' , ( ) => {
35
- beforeEach ( ( ) => {
36
- jest . clearAllMocks ( ) ;
37
+ beforeEach ( ( ) => {
38
+ ( hueConfigModule . getLastKnownConfig as jest . Mock ) . mockReturnValue ( {
39
+ hue_config : { is_admin : true }
37
40
} ) ;
41
+ } ) ;
42
+ afterEach ( ( ) => {
43
+ jest . clearAllMocks ( ) ;
44
+ } ) ;
45
+
38
46
test ( 'renders the Tabs with the correct tab labels' , ( ) => {
39
47
render ( < Overview /> ) ;
40
48
expect ( screen . getByText ( 'ConfigStatus' ) ) . toBeInTheDocument ( ) ;
@@ -56,34 +64,69 @@ describe('OverviewTab', () => {
56
64
) . toBeInTheDocument ( ) ;
57
65
} ) ;
58
66
59
- jest . mock ( '../../../config/hueConfig' , ( ) => ( {
60
- getLastKnownConfig : ( ) => ( { hue_config : { is_admin : false } } )
61
- } ) ) ;
62
67
63
68
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
+ } ) ;
66
72
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 ( ) ;
68
76
} ) ;
69
77
70
- //verify table contents
71
78
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
+ // });
72
107
// 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
+ // });
77
111
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
+ // });
79
117
80
118
// expect(checkbox).not.toBeChecked();
81
- // userEvent.click(checkbox);
119
+ // await userEvent.click(checkbox);
82
120
// 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);
85
126
// 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 });
87
130
// });
88
131
// });
89
132
} ) ;
0 commit comments