|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import configureMockStore from 'redux-mock-store'; |
| 5 | +import thunk from 'redux-thunk'; |
1 | 6 | import { testComponentSnapshotsWithFixtures } from '@theforeman/test'; |
2 | | - |
3 | 7 | import PageHeader from '../PageHeader'; |
4 | 8 |
|
| 9 | +// Mock child components to isolate PageHeader testing |
| 10 | +// This prevents child component complexity from affecting our tests |
| 11 | +jest.mock('../components/SettingsWarning', () => () => ( |
| 12 | + <div data-testid="settings-warning">SettingsWarning</div> |
| 13 | +)); |
| 14 | +jest.mock('../PageTitle', () => () => ( |
| 15 | + <div data-testid="page-title">PageTitle</div> |
| 16 | +)); |
| 17 | +jest.mock('../../InventorySettings', () => () => ( |
| 18 | + <div data-testid="inventory-settings">InventorySettings</div> |
| 19 | +)); |
| 20 | +jest.mock('../components/PageDescription', () => () => ( |
| 21 | + <div data-testid="page-description">PageDescription</div> |
| 22 | +)); |
| 23 | +jest.mock('../../InventoryFilter', () => () => ( |
| 24 | + <div data-testid="inventory-filter">InventoryFilter</div> |
| 25 | +)); |
| 26 | +jest.mock('../components/ToolbarButtons', () => () => ( |
| 27 | + <div data-testid="toolbar-buttons">ToolbarButtons</div> |
| 28 | +)); |
| 29 | + |
| 30 | +// Mock the hook since useAPI is mocked in tests and doesn't read from Redux |
| 31 | +jest.mock('../../../../common/Hooks/ConfigHooks', () => ({ |
| 32 | + useAdvisorEngineConfig: jest.fn(() => false), |
| 33 | +})); |
| 34 | + |
| 35 | +const { |
| 36 | + useAdvisorEngineConfig, |
| 37 | +} = require('../../../../common/Hooks/ConfigHooks'); |
| 38 | + |
| 39 | +const middlewares = [thunk]; |
| 40 | +const mockStore = configureMockStore(middlewares); |
| 41 | + |
| 42 | +const baseStoreState = { |
| 43 | + API: { |
| 44 | + INVENTORY_SETTINGS: { |
| 45 | + response: { subscriptionConnectionEnabled: true }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + ForemanRhCloud: { |
| 49 | + inventoryUpload: { |
| 50 | + accountsList: { |
| 51 | + CloudConnectorStatus: {}, |
| 52 | + }, |
| 53 | + syncStatus: {}, |
| 54 | + inventoryFilter: { |
| 55 | + filterTerm: '', |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
5 | 61 | const fixtures = { |
6 | 62 | 'render without Props': {}, |
7 | 63 | /** fixtures, props for the component */ |
8 | 64 | }; |
9 | 65 |
|
10 | 66 | describe('PageHeader', () => { |
| 67 | + beforeEach(() => { |
| 68 | + useAdvisorEngineConfig.mockReturnValue(false); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + jest.clearAllMocks(); |
| 73 | + }); |
| 74 | + |
11 | 75 | describe('rendering', () => |
12 | 76 | testComponentSnapshotsWithFixtures(PageHeader, fixtures)); |
| 77 | + |
| 78 | + describe('component behavior', () => { |
| 79 | + test('renders all components when not in IoP mode', () => { |
| 80 | + const store = mockStore(baseStoreState); |
| 81 | + |
| 82 | + render( |
| 83 | + <Provider store={store}> |
| 84 | + <PageHeader /> |
| 85 | + </Provider> |
| 86 | + ); |
| 87 | + |
| 88 | + // All components should be present when not in IoP mode |
| 89 | + expect(screen.getByTestId('settings-warning')).toBeTruthy(); |
| 90 | + expect(screen.getByTestId('page-title')).toBeTruthy(); |
| 91 | + expect(screen.getByTestId('inventory-settings')).toBeTruthy(); |
| 92 | + expect(screen.getByTestId('page-description')).toBeTruthy(); |
| 93 | + expect(screen.getByTestId('inventory-filter')).toBeTruthy(); |
| 94 | + expect(screen.getByTestId('toolbar-buttons')).toBeTruthy(); |
| 95 | + }); |
| 96 | + |
| 97 | + test('hides inventory settings and description when in IoP mode', () => { |
| 98 | + useAdvisorEngineConfig.mockReturnValue(true); |
| 99 | + const store = mockStore(baseStoreState); |
| 100 | + |
| 101 | + render( |
| 102 | + <Provider store={store}> |
| 103 | + <PageHeader /> |
| 104 | + </Provider> |
| 105 | + ); |
| 106 | + |
| 107 | + // Core components should still be present |
| 108 | + expect(screen.getByTestId('settings-warning')).toBeTruthy(); |
| 109 | + expect(screen.getByTestId('page-title')).toBeTruthy(); |
| 110 | + expect(screen.getByTestId('inventory-filter')).toBeTruthy(); |
| 111 | + expect(screen.getByTestId('toolbar-buttons')).toBeTruthy(); |
| 112 | + |
| 113 | + // These components should be hidden in IoP mode |
| 114 | + expect(screen.queryByTestId('inventory-settings')).toBeNull(); |
| 115 | + expect(screen.queryByTestId('page-description')).toBeNull(); |
| 116 | + }); |
| 117 | + |
| 118 | + test('renders with correct CSS class', () => { |
| 119 | + const store = mockStore(baseStoreState); |
| 120 | + |
| 121 | + const { container } = render( |
| 122 | + <Provider store={store}> |
| 123 | + <PageHeader /> |
| 124 | + </Provider> |
| 125 | + ); |
| 126 | + |
| 127 | + expect(container.querySelector('.inventory-upload-header')).toBeTruthy(); |
| 128 | + }); |
| 129 | + |
| 130 | + test('renders grid layout with correct structure', () => { |
| 131 | + const store = mockStore(baseStoreState); |
| 132 | + |
| 133 | + const { container } = render( |
| 134 | + <Provider store={store}> |
| 135 | + <PageHeader /> |
| 136 | + </Provider> |
| 137 | + ); |
| 138 | + |
| 139 | + // Check for grid row |
| 140 | + const gridRow = container.querySelector('.row'); |
| 141 | + expect(gridRow).toBeTruthy(); |
| 142 | + |
| 143 | + // Check for grid columns with correct classes |
| 144 | + const filterColumn = container.querySelector('.col-xs-4'); |
| 145 | + expect(filterColumn).toBeTruthy(); |
| 146 | + |
| 147 | + const toolbarColumn = container.querySelector('.col-xs-7'); |
| 148 | + expect(toolbarColumn).toBeTruthy(); |
| 149 | + }); |
| 150 | + |
| 151 | + test('renders description section only when not in IoP mode', () => { |
| 152 | + const store = mockStore(baseStoreState); |
| 153 | + |
| 154 | + const { container } = render( |
| 155 | + <Provider store={store}> |
| 156 | + <PageHeader /> |
| 157 | + </Provider> |
| 158 | + ); |
| 159 | + |
| 160 | + // Description section should be present when not in IoP mode |
| 161 | + const descriptionSection = container.querySelector( |
| 162 | + '.inventory-upload-header-description' |
| 163 | + ); |
| 164 | + expect(descriptionSection).toBeTruthy(); |
| 165 | + }); |
| 166 | + |
| 167 | + test('does not render description section when in IoP mode', () => { |
| 168 | + useAdvisorEngineConfig.mockReturnValue(true); |
| 169 | + const store = mockStore(baseStoreState); |
| 170 | + |
| 171 | + const { container } = render( |
| 172 | + <Provider store={store}> |
| 173 | + <PageHeader /> |
| 174 | + </Provider> |
| 175 | + ); |
| 176 | + |
| 177 | + // Description section should not be present in IoP mode |
| 178 | + const descriptionSection = container.querySelector( |
| 179 | + '.inventory-upload-header-description' |
| 180 | + ); |
| 181 | + expect(descriptionSection).toBeNull(); |
| 182 | + }); |
| 183 | + }); |
13 | 184 | }); |
0 commit comments