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