|
| 1 | +import { render, screen, userEvent } from '@/test-utils/rtl'; |
| 2 | + |
| 3 | +import { type PageQueryParamValues } from '@/hooks/use-page-query-params/use-page-query-params.types'; |
| 4 | +import { mockDomainPageQueryParamsValues } from '@/views/domain-page/__fixtures__/domain-page-query-params'; |
| 5 | +import type domainPageQueryParamsConfig from '@/views/domain-page/config/domain-page-query-params.config'; |
| 6 | +import { mockActiveActiveDomain } from '@/views/shared/active-active/__fixtures__/active-active-domain'; |
| 7 | +import { type ActiveActiveDomain } from '@/views/shared/active-active/active-active.types'; |
| 8 | + |
| 9 | +import { PRIMARY_CLUSTER_SCOPE } from '../../domain-page-failovers/domain-page-failovers.constants'; |
| 10 | +import DomainPageFailoversFilters from '../domain-page-failovers-filters'; |
| 11 | + |
| 12 | +describe(DomainPageFailoversFilters.name, () => { |
| 13 | + it('renders both filter comboboxes and reset button', () => { |
| 14 | + setup({}); |
| 15 | + |
| 16 | + expect(screen.getByText('Cluster Attribute Scope')).toBeInTheDocument(); |
| 17 | + expect(screen.getByText('Cluster Attribute Value')).toBeInTheDocument(); |
| 18 | + expect(screen.getByText('Reset filters')).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('displays cluster attribute scope options including primary and domain scopes', async () => { |
| 22 | + const { user } = setup({}); |
| 23 | + |
| 24 | + const comboboxes = screen.getAllByRole('combobox'); |
| 25 | + const scopeCombobox = comboboxes[0]; |
| 26 | + await user.click(scopeCombobox); |
| 27 | + |
| 28 | + expect(screen.getByText(PRIMARY_CLUSTER_SCOPE)).toBeInTheDocument(); |
| 29 | + expect(screen.getByText('region')).toBeInTheDocument(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('disables cluster attribute value combobox when scope is primary', () => { |
| 33 | + setup({ |
| 34 | + queryParamsOverrides: { |
| 35 | + clusterAttributeScope: PRIMARY_CLUSTER_SCOPE, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + const inputs = screen.getAllByRole('textbox'); |
| 40 | + expect(inputs[1]).toBeDisabled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('enables cluster attribute value combobox when scope is not primary', () => { |
| 44 | + setup({ |
| 45 | + queryParamsOverrides: { |
| 46 | + clusterAttributeScope: 'region', |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + const inputs = screen.getAllByRole('textbox'); |
| 51 | + expect(inputs[1]).not.toBeDisabled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('displays cluster attribute values for selected scope', async () => { |
| 55 | + const { user } = setup({ |
| 56 | + queryParamsOverrides: { |
| 57 | + clusterAttributeScope: 'region', |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + const comboboxes = screen.getAllByRole('combobox'); |
| 62 | + const valueCombobox = comboboxes[1]; |
| 63 | + await user.click(valueCombobox); |
| 64 | + |
| 65 | + expect(screen.getByText('region0')).toBeInTheDocument(); |
| 66 | + expect(screen.getByText('region1')).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('calls setQueryParams with new scope and resets value when scope changes', async () => { |
| 70 | + const { user, mockSetQueryParams } = setup({}); |
| 71 | + |
| 72 | + const comboboxes = screen.getAllByRole('combobox'); |
| 73 | + const scopeCombobox = comboboxes[0]; |
| 74 | + await user.click(scopeCombobox); |
| 75 | + await user.click(screen.getByText('region')); |
| 76 | + |
| 77 | + expect(mockSetQueryParams).toHaveBeenCalledWith({ |
| 78 | + clusterAttributeScope: 'region', |
| 79 | + clusterAttributeValue: undefined, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('calls setQueryParams with new value when cluster attribute value changes', async () => { |
| 84 | + const { user, mockSetQueryParams } = setup({ |
| 85 | + queryParamsOverrides: { |
| 86 | + clusterAttributeScope: 'region', |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + const comboboxes = screen.getAllByRole('combobox'); |
| 91 | + const valueCombobox = comboboxes[1]; |
| 92 | + await user.click(valueCombobox); |
| 93 | + await user.click(screen.getByText('region0')); |
| 94 | + |
| 95 | + expect(mockSetQueryParams).toHaveBeenCalledWith({ |
| 96 | + clusterAttributeValue: 'region0', |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('calls setQueryParams with undefined when clearing scope', async () => { |
| 101 | + const { user, mockSetQueryParams } = setup({ |
| 102 | + queryParamsOverrides: { |
| 103 | + clusterAttributeScope: 'region', |
| 104 | + clusterAttributeValue: 'region0', |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + const comboboxes = screen.getAllByRole('combobox'); |
| 109 | + const scopeCombobox = comboboxes[0]; |
| 110 | + await user.click(scopeCombobox); |
| 111 | + |
| 112 | + // Find and click the clear button (BaseUI Combobox clearable) |
| 113 | + const clearButtons = screen.getAllByLabelText('Clear value'); |
| 114 | + await user.click(clearButtons[0]); |
| 115 | + |
| 116 | + expect(mockSetQueryParams).toHaveBeenCalledWith({ |
| 117 | + clusterAttributeScope: undefined, |
| 118 | + clusterAttributeValue: undefined, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('calls setQueryParams with undefined when clearing value', async () => { |
| 123 | + const { user, mockSetQueryParams } = setup({ |
| 124 | + queryParamsOverrides: { |
| 125 | + clusterAttributeScope: 'region', |
| 126 | + clusterAttributeValue: 'region0', |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const comboboxes = screen.getAllByRole('combobox'); |
| 131 | + const valueCombobox = comboboxes[1]; |
| 132 | + await user.click(valueCombobox); |
| 133 | + |
| 134 | + // Find and click the clear button |
| 135 | + const clearButtons = screen.getAllByLabelText('Clear value'); |
| 136 | + await user.click(clearButtons[1]); |
| 137 | + |
| 138 | + expect(mockSetQueryParams).toHaveBeenCalledWith({ |
| 139 | + clusterAttributeValue: undefined, |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it('resets both filters when reset filters button is clicked', async () => { |
| 144 | + const { user, mockSetQueryParams } = setup({ |
| 145 | + queryParamsOverrides: { |
| 146 | + clusterAttributeScope: 'region', |
| 147 | + clusterAttributeValue: 'region0', |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + const resetButton = screen.getByText('Reset filters'); |
| 152 | + await user.click(resetButton); |
| 153 | + |
| 154 | + expect(mockSetQueryParams).toHaveBeenCalledWith({ |
| 155 | + clusterAttributeScope: undefined, |
| 156 | + clusterAttributeValue: undefined, |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('displays current scope value in combobox', () => { |
| 161 | + setup({ |
| 162 | + queryParamsOverrides: { |
| 163 | + clusterAttributeScope: 'region', |
| 164 | + }, |
| 165 | + }); |
| 166 | + |
| 167 | + const inputs = screen.getAllByRole('textbox'); |
| 168 | + expect(inputs[0]).toHaveValue('region'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('displays current value in cluster attribute value combobox', () => { |
| 172 | + setup({ |
| 173 | + queryParamsOverrides: { |
| 174 | + clusterAttributeScope: 'region', |
| 175 | + clusterAttributeValue: 'region0', |
| 176 | + }, |
| 177 | + }); |
| 178 | + |
| 179 | + const inputs = screen.getAllByRole('textbox'); |
| 180 | + expect(inputs[1]).toHaveValue('region0'); |
| 181 | + }); |
| 182 | +}); |
| 183 | + |
| 184 | +function setup({ |
| 185 | + queryParamsOverrides, |
| 186 | + domainDescriptionOverrides, |
| 187 | +}: { |
| 188 | + queryParamsOverrides?: Partial< |
| 189 | + PageQueryParamValues<typeof domainPageQueryParamsConfig> |
| 190 | + >; |
| 191 | + domainDescriptionOverrides?: Partial<ActiveActiveDomain>; |
| 192 | +} = {}) { |
| 193 | + const mockSetQueryParams = jest.fn(); |
| 194 | + |
| 195 | + const domainDescription = { |
| 196 | + ...mockActiveActiveDomain, |
| 197 | + ...domainDescriptionOverrides, |
| 198 | + }; |
| 199 | + |
| 200 | + const queryParams = { |
| 201 | + ...mockDomainPageQueryParamsValues, |
| 202 | + ...queryParamsOverrides, |
| 203 | + }; |
| 204 | + |
| 205 | + render( |
| 206 | + <DomainPageFailoversFilters |
| 207 | + domainDescription={domainDescription} |
| 208 | + queryParams={queryParams} |
| 209 | + setQueryParams={mockSetQueryParams} |
| 210 | + /> |
| 211 | + ); |
| 212 | + |
| 213 | + const user = userEvent.setup(); |
| 214 | + |
| 215 | + return { mockSetQueryParams, user }; |
| 216 | +} |
0 commit comments