File size: 4,704 Bytes
52efc7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { renderWithProviders } from '../../../test-utils/render.js';
import { EnumSelector } from './EnumSelector.js';
import type { SettingEnumOption } from '../../../config/settingsSchema.js';
import { describe, it, expect } from 'vitest';
import { act } from 'react';
const LANGUAGE_OPTIONS: readonly SettingEnumOption[] = [
{ label: 'English', value: 'en' },
{ label: '中文 (简体)', value: 'zh' },
{ label: 'Español', value: 'es' },
{ label: 'Français', value: 'fr' },
];
const NUMERIC_OPTIONS: readonly SettingEnumOption[] = [
{ label: 'Low', value: 1 },
{ label: 'Medium', value: 2 },
{ label: 'High', value: 3 },
];
describe('<EnumSelector />', () => {
it('renders with string options and matches snapshot', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="en"
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders with numeric options and matches snapshot', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={NUMERIC_OPTIONS}
currentValue={2}
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders inactive state and matches snapshot', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="zh"
isActive={false}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders with single option and matches snapshot', async () => {
const singleOption: readonly SettingEnumOption[] = [
{ label: 'Only Option', value: 'only' },
];
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={singleOption}
currentValue="only"
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders nothing when no options are provided', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={[]}
currentValue=""
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame({ allowEmpty: true })).toBe('');
unmount();
});
it('handles currentValue not found in options', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="invalid"
isActive={true}
onValueChange={async () => {}}
/>,
);
// Should default to first option
expect(lastFrame()).toContain('English');
unmount();
});
it('updates when currentValue changes externally', async () => {
const { rerender, lastFrame, waitUntilReady, unmount } =
await renderWithProviders(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="en"
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toContain('English');
await act(async () => {
rerender(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="zh"
isActive={true}
onValueChange={async () => {}}
/>,
);
});
await waitUntilReady();
expect(lastFrame()).toContain('中文 (简体)');
unmount();
});
it('shows navigation arrows when multiple options available', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={LANGUAGE_OPTIONS}
currentValue="en"
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).toContain('←');
expect(lastFrame()).toContain('→');
unmount();
});
it('hides navigation arrows when single option available', async () => {
const singleOption: readonly SettingEnumOption[] = [
{ label: 'Only Option', value: 'only' },
];
const { lastFrame, unmount } = await renderWithProviders(
<EnumSelector
options={singleOption}
currentValue="only"
isActive={true}
onValueChange={async () => {}}
/>,
);
expect(lastFrame()).not.toContain('←');
expect(lastFrame()).not.toContain('→');
unmount();
});
});
|