import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FeatureFlagIndicator } from '../FeatureFlagIndicator';
import { FlagKey, FLAGS, FLAGS_STATUS } from '../flags';
import { useFeatureFlags } from '../hooks';
const readyIconColor = 'var(--pf-t--global--color--status--success--default)';
const warningIconColor = 'var(--pf-t--global--color--status--warning--default)';
jest.mock('../flags', () => {
{
const original = jest.requireActual('../flags');
return {
...original,
FLAGS: {
'dark-theme': {
description:
'Enable the theme switcher in the header to toggle between light and dark modes.',
},
'stable-feature': {
key: 'stable-feature',
description: 'A stable feature flag',
defaultEnabled: true,
status: 'ready',
},
'wip-feature': {
key: 'wip-feature',
description: 'A work-in-progress feature flag',
defaultEnabled: false,
status: 'wip',
},
},
};
}
});
jest.mock('../hooks', () => ({
useFeatureFlags: jest.fn(() => [
{
'dark-theme': true,
'stable-feature': true,
'wip-feature': true,
},
]),
}));
const useFeatureFlagsMock = useFeatureFlags as jest.Mock;
const getTriggerButton = () => screen.getByRole('button', { name: 'Feature flag information' });
const getTriggerIcon = () => {
const icon = getTriggerButton().querySelector('svg');
if (!icon) {
throw new Error('Expected icon inside feature flag trigger button');
}
return icon;
};
describe('FeatureFlagIndicator', () => {
afterEach(() => {
useFeatureFlagsMock.mockReturnValue([
{
'dark-theme': true,
'stable-feature': true,
'wip-feature': true,
},
]);
});
it('renders nothing when unknown flags are provided', () => {
const { container } = render(
,
);
expect(container.firstChild).toBeNull();
});
it('renders icon-only trigger by default', () => {
render();
expect(getTriggerButton()).toBeInTheDocument();
expect(getTriggerIcon()).toBeInTheDocument();
});
it('renders full label when fullLabel is true', () => {
render();
expect(getTriggerButton()).toBeInTheDocument();
expect(screen.getByText(FLAGS_STATUS.ready)).toBeInTheDocument();
});
it('shows popover with descriptions for all flags on click', async () => {
render();
await userEvent.click(getTriggerButton());
expect(screen.getByText(FLAGS['dark-theme' as FlagKey].description)).toBeInTheDocument();
expect(screen.getByText(FLAGS['wip-feature' as FlagKey].description)).toBeInTheDocument();
});
it('uses success icon color when all active flags are ready', () => {
useFeatureFlagsMock.mockReturnValue([{ 'stable-feature': true }]);
render();
expect(getTriggerIcon()).toHaveStyle({ color: readyIconColor });
});
it('uses warning icon color when any active flag is wip', () => {
useFeatureFlagsMock.mockReturnValue([{ 'wip-feature': true }]);
render();
expect(getTriggerIcon()).toHaveStyle({ color: warningIconColor });
});
it('uses warning icon color when mixing ready and wip flags', () => {
useFeatureFlagsMock.mockReturnValue([{ 'stable-feature': true, 'wip-feature': true }]);
render(
,
);
expect(getTriggerIcon()).toHaveStyle({ color: warningIconColor });
});
});