APG Patterns
English GitHub
English GitHub

Combobox

リストオートコンプリート機能を持つ編集可能なコンボボックス。ユーザーは入力してオプションをフィルタリングしたり、キーボードやマウスでポップアップリストボックスから選択したりできます。

🤖 AI Implementation Guide

デモ

デモのみを開く →

ネイティブ HTML

まずネイティブ HTML を検討してください

カスタムコンボボックスを使用する前に、ネイティブ HTML の代替手段を検討してください。 これらは組み込みのセマンティクスを持ち、JavaScript なしで動作し、ブラウザネイティブのサポートがあります。

<!-- シンプルなドロップダウン選択 -->
<label for="fruit">フルーツを選択</label>
<select id="fruit">
  <option value="apple">りんご</option>
  <option value="banana">バナナ</option>
</select>

<!-- 基本的なオートコンプリート -->
<label for="browser">ブラウザを選択</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
</datalist>

カスタムスタイル、複雑なフィルタリングロジック、リッチなオプション表示、またはネイティブ要素でサポートされていない動作が必要な場合にのみカスタムコンボボックスを使用してください。

ユースケース ネイティブ HTML カスタム実装
シンプルなドロップダウン選択 <select> 推奨 不要
基本的なオートコンプリート候補 <datalist> 推奨 不要
JavaScript 無効時のサポート ネイティブで動作 フォールバックが必要
カスタムオプション表示(アイコン、説明) 非対応 完全制御可能
カスタムフィルタリングロジック 基本的な前方一致のみ カスタムアルゴリズム
ブラウザ間で一貫したスタイル 限定的(特に datalist) 完全制御可能
キーボードナビゲーションのカスタマイズ ブラウザデフォルトのみ カスタマイズ可能
無効なオプション <select> のみ 完全対応

ネイティブ <select> 要素は優れたアクセシビリティ、フォーム送信サポート、JavaScript 不要で動作します。<datalist> 要素は基本的なオートコンプリート機能を提供しますが、ブラウザ間で見た目が大きく異なり、無効なオプションやカスタム表示には対応していません。

<datalist> のアクセシビリティ上の問題

<datalist> 要素には、以下のアクセシビリティ上の問題が知られています:

  • テキストズーム非対応:datalist のオプションのフォントサイズはページをズームしても拡大されず、テキスト拡大に依存するユーザーに問題を引き起こします。
  • CSS スタイリングの制限:オプションをハイコントラストモード向けにスタイリングできないため、視覚障害のあるユーザーへの対応が困難です。
  • スクリーンリーダーの互換性:一部のスクリーンリーダーとブラウザの組み合わせ(例:NVDA と Firefox)では、オートサジェストポップアップの内容が読み上げられません。

出典: MDN Web Docs - <datalist>: Accessibility

アクセシビリティ

WAI-ARIA ロール

ロール 対象要素 説明
combobox Input(<input> ユーザーが入力するテキスト入力要素
listbox ポップアップ(<ul> 選択可能なオプションを含むポップアップ
option 各アイテム(<li> 個々の選択可能なオプション

WAI-ARIA combobox role (opens in new tab)

WAI-ARIA プロパティ(Input)

属性 必須 説明
role="combobox" - はい 入力をコンボボックスとして識別
aria-controls ID参照 はい リストボックスポップアップを参照(閉じている時も)
aria-expanded true | false はい ポップアップが開いているかどうかを示す
aria-autocomplete list | none | both はい オートコンプリートの動作を説明
aria-activedescendant ID参照 | 空 はい ポップアップ内で現在フォーカスされているオプションを参照
aria-labelledby ID参照 はい* ラベル要素を参照

WAI-ARIA プロパティ(Listbox & Options)

属性 対象 必須 説明
aria-labelledby listbox ID参照 はい ラベル要素を参照
aria-selected option true | false はい 現在フォーカスされているオプションを示す
aria-disabled option true いいえ オプションが無効であることを示す

キーボードサポート

Input(ポップアップ閉時)

キー アクション
Down Arrow ポップアップを開き、最初のオプションにフォーカス
Up Arrow ポップアップを開き、最後のオプションにフォーカス
Alt + Down Arrow フォーカス位置を変更せずにポップアップを開く
文字入力 オプションをフィルタリングしてポップアップを開く

Input(ポップアップ開時)

キー アクション
Down Arrow 次の有効なオプションにフォーカスを移動(折り返しなし)
Up Arrow 前の有効なオプションにフォーカスを移動(折り返しなし)
Home 最初の有効なオプションにフォーカスを移動
End 最後の有効なオプションにフォーカスを移動
Enter フォーカス中のオプションを選択しポップアップを閉じる
Escape ポップアップを閉じ、以前の入力値を復元
Alt + Up Arrow フォーカス中のオプションを選択しポップアップを閉じる
Tab ポップアップを閉じ、次のフォーカス可能な要素に移動

フォーカス管理

このコンポーネントは仮想フォーカス管理に aria-activedescendant を使用します:

  • DOM フォーカスは常に input に留まる
  • aria-activedescendant が視覚的にフォーカスされているオプションを参照
  • 矢印キーが DOM フォーカスを移動せずに aria-activedescendant を更新
  • 無効化されたオプションはナビゲーション中にスキップされる
  • ポップアップが閉じるか、フィルタ結果が空になると aria-activedescendant がクリア

オートコンプリートモード

モード 動作
list オプションは入力値に基づいてフィルタリングされる(デフォルト)
none 入力値に関係なくすべてのオプションを表示
both オプションをフィルタリングし、最初の一致を入力にオートコンプリート

非表示状態

閉じている時、リストボックスは hidden 属性を使用して:

  • ポップアップを視覚的に非表示にする
  • ポップアップをアクセシビリティツリーから削除する
  • リストボックス要素は DOM に残り、aria-controls の参照が有効なまま

ソースコード

Combobox.vue
<template>
  <div ref="containerRef" :class="cn('apg-combobox', className)">
    <label :id="labelId" :for="inputId" class="apg-combobox-label">
      {{ label }}
    </label>
    <div class="apg-combobox-input-wrapper">
      <input
        ref="inputRef"
        :id="inputId"
        type="text"
        role="combobox"
        class="apg-combobox-input"
        :aria-autocomplete="autocomplete"
        :aria-expanded="isOpen"
        :aria-controls="listboxId"
        :aria-labelledby="labelId"
        :aria-activedescendant="activeDescendantId || undefined"
        :value="currentInputValue"
        :placeholder="placeholder"
        :disabled="disabled"
        v-bind="$attrs"
        @input="handleInput"
        @keydown="handleKeyDown"
        @focus="handleFocus"
        @compositionstart="handleCompositionStart"
        @compositionend="handleCompositionEnd"
      />
      <span class="apg-combobox-caret" aria-hidden="true">
        <svg viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
          <path
            fill-rule="evenodd"
            d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z"
            clip-rule="evenodd"
          />
        </svg>
      </span>
    </div>
    <ul
      :id="listboxId"
      role="listbox"
      :aria-labelledby="labelId"
      class="apg-combobox-listbox"
      :hidden="!isOpen || undefined"
    >
      <li v-if="filteredOptions.length === 0" class="apg-combobox-no-results" role="status">
        {{ noResultsMessage }}
      </li>
      <li
        v-for="(option, index) in filteredOptions"
        :key="option.id"
        :id="getOptionId(option.id)"
        role="option"
        class="apg-combobox-option"
        :aria-selected="index === activeIndex"
        :aria-disabled="option.disabled || undefined"
        :data-selected="option.id === currentSelectedId || undefined"
        @click="handleOptionClick(option)"
        @mouseenter="handleOptionHover(option)"
      >
        <span class="apg-combobox-option-icon" aria-hidden="true">
          <svg viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
            <path
              d="M10.28 2.28a.75.75 0 00-1.06-1.06L4.5 5.94 2.78 4.22a.75.75 0 00-1.06 1.06l2.25 2.25a.75.75 0 001.06 0l5.25-5.25z"
            />
          </svg>
        </span>
        {{ option.label }}
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { cn } from '@/lib/utils';
import { computed, onUnmounted, ref, useId, watch } from 'vue';

export interface ComboboxOption {
  id: string;
  label: string;
  disabled?: boolean;
}

export interface ComboboxProps {
  options: ComboboxOption[];
  selectedOptionId?: string;
  defaultSelectedOptionId?: string;
  inputValue?: string;
  defaultInputValue?: string;
  label: string;
  placeholder?: string;
  disabled?: boolean;
  autocomplete?: 'none' | 'list' | 'both';
  noResultsMessage?: string;
  className?: string;
}

const props = withDefaults(defineProps<ComboboxProps>(), {
  defaultInputValue: '',
  disabled: false,
  autocomplete: 'list',
  noResultsMessage: 'No results found',
  className: '',
});

const emit = defineEmits<{
  select: [option: ComboboxOption];
  inputChange: [value: string];
  openChange: [isOpen: boolean];
}>();

defineOptions({
  inheritAttrs: false,
});

// Refs
const containerRef = ref<HTMLDivElement>();
const inputRef = ref<HTMLInputElement>();
const instanceId = useId();

// State
const isOpen = ref(false);
const activeIndex = ref(-1);
const isComposing = ref(false);
const valueBeforeOpen = ref('');
const isSearching = ref(false);

// Internal state for uncontrolled mode
const internalInputValue = ref(() => {
  if (props.defaultSelectedOptionId) {
    const option = props.options.find(({ id }) => id === props.defaultSelectedOptionId);
    return option?.label ?? props.defaultInputValue;
  }
  return props.defaultInputValue;
});
const internalSelectedId = ref<string | undefined>(props.defaultSelectedOptionId);

// Computed
const inputId = computed(() => `${instanceId}-input`);
const labelId = computed(() => `${instanceId}-label`);
const listboxId = computed(() => `${instanceId}-listbox`);

const currentInputValue = computed(() => {
  if (props.inputValue !== undefined) {
    return props.inputValue;
  }
  // Handle both function ref (from initialization) and string value
  const value = internalInputValue.value;
  return typeof value === 'function' ? value() : value;
});

const currentSelectedId = computed(() => props.selectedOptionId ?? internalSelectedId.value);

// Get selected option's label
const selectedLabel = computed(() => {
  if (!currentSelectedId.value) {
    return '';
  }
  const option = props.options.find(({ id }) => id === currentSelectedId.value);
  return option?.label ?? '';
});

const filteredOptions = computed(() => {
  const { autocomplete, options } = props;
  const inputValue = currentInputValue.value;

  // Don't filter if autocomplete is none
  if (autocomplete === 'none') {
    return options;
  }

  // Don't filter if input is empty
  if (!inputValue) {
    return options;
  }

  // Don't filter if not in search mode AND input matches selected label
  if (!isSearching.value && inputValue === selectedLabel.value) {
    return options;
  }

  const lowerInputValue = inputValue.toLowerCase();

  return options.filter(({ label }) => label.toLowerCase().includes(lowerInputValue));
});

const enabledOptions = computed(() => filteredOptions.value.filter(({ disabled }) => !disabled));

const activeDescendantId = computed(() => {
  if (activeIndex.value < 0 || activeIndex.value >= filteredOptions.value.length) {
    return undefined;
  }
  const option = filteredOptions.value[activeIndex.value];
  return option ? getOptionId(option.id) : undefined;
});

// Helper functions
const getOptionId = (optionId: string) => `${instanceId}-option-${optionId}`;

const updateInputValue = (value: string) => {
  if (props.inputValue === undefined) {
    internalInputValue.value = value;
  }
  emit('inputChange', value);
};

const openPopup = (focusPosition?: 'first' | 'last') => {
  if (isOpen.value) {
    return;
  }

  valueBeforeOpen.value = currentInputValue.value;
  isOpen.value = true;
  emit('openChange', true);

  if (!focusPosition || enabledOptions.value.length === 0) {
    return;
  }

  const targetOption =
    focusPosition === 'first'
      ? enabledOptions.value[0]
      : enabledOptions.value[enabledOptions.value.length - 1];
  const { id: targetId } = targetOption;
  const targetIndex = filteredOptions.value.findIndex(({ id }) => id === targetId);
  activeIndex.value = targetIndex;
};

const closePopup = (restore = false) => {
  isOpen.value = false;
  activeIndex.value = -1;
  isSearching.value = false;
  emit('openChange', false);

  if (restore) {
    updateInputValue(valueBeforeOpen.value);
  }
};

const selectOption = ({ id, label, disabled }: ComboboxOption) => {
  if (disabled) {
    return;
  }

  if (props.selectedOptionId === undefined) {
    internalSelectedId.value = id;
  }

  isSearching.value = false;
  updateInputValue(label);
  emit('select', { id, label, disabled });
  closePopup();
};

const findEnabledIndex = (
  startIndex: number,
  direction: 'next' | 'prev' | 'first' | 'last'
): number => {
  if (enabledOptions.value.length === 0) {
    return -1;
  }

  if (direction === 'first') {
    const { id: firstId } = enabledOptions.value[0];
    return filteredOptions.value.findIndex(({ id }) => id === firstId);
  }

  if (direction === 'last') {
    const { id: lastId } = enabledOptions.value[enabledOptions.value.length - 1];
    return filteredOptions.value.findIndex(({ id }) => id === lastId);
  }

  const currentOption = filteredOptions.value[startIndex];
  const currentEnabledIndex = currentOption
    ? enabledOptions.value.findIndex(({ id }) => id === currentOption.id)
    : -1;

  if (direction === 'next') {
    if (currentEnabledIndex < 0) {
      const { id: firstId } = enabledOptions.value[0];
      return filteredOptions.value.findIndex(({ id }) => id === firstId);
    }

    if (currentEnabledIndex >= enabledOptions.value.length - 1) {
      return startIndex;
    }

    const { id: nextId } = enabledOptions.value[currentEnabledIndex + 1];
    return filteredOptions.value.findIndex(({ id }) => id === nextId);
  }

  // direction === 'prev'
  if (currentEnabledIndex < 0) {
    const { id: lastId } = enabledOptions.value[enabledOptions.value.length - 1];
    return filteredOptions.value.findIndex(({ id }) => id === lastId);
  }

  if (currentEnabledIndex <= 0) {
    return startIndex;
  }

  const { id: prevId } = enabledOptions.value[currentEnabledIndex - 1];
  return filteredOptions.value.findIndex(({ id }) => id === prevId);
};

// Event handlers
const handleInput = (event: Event) => {
  const target = event.target as HTMLInputElement;
  const value = target.value;
  isSearching.value = true;
  updateInputValue(value);

  if (!isOpen.value && !isComposing.value) {
    valueBeforeOpen.value = currentInputValue.value;
    isOpen.value = true;
    emit('openChange', true);
  }

  activeIndex.value = -1;
};

const handleKeyDown = (event: KeyboardEvent) => {
  if (isComposing.value) {
    return;
  }

  const { key, altKey } = event;

  switch (key) {
    case 'ArrowDown': {
      event.preventDefault();

      if (altKey) {
        if (isOpen.value) {
          return;
        }

        valueBeforeOpen.value = currentInputValue.value;
        isOpen.value = true;
        emit('openChange', true);
        return;
      }

      if (!isOpen.value) {
        openPopup('first');
        return;
      }

      const nextIndex = findEnabledIndex(activeIndex.value, 'next');

      if (nextIndex >= 0) {
        activeIndex.value = nextIndex;
      }
      break;
    }
    case 'ArrowUp': {
      event.preventDefault();

      if (altKey) {
        if (!isOpen.value || activeIndex.value < 0) {
          return;
        }

        const option = filteredOptions.value[activeIndex.value];

        if (option === undefined || option.disabled) {
          return;
        }

        selectOption(option);
        return;
      }

      if (!isOpen.value) {
        openPopup('last');
        return;
      }

      const prevIndex = findEnabledIndex(activeIndex.value, 'prev');

      if (prevIndex >= 0) {
        activeIndex.value = prevIndex;
      }
      break;
    }
    case 'Home': {
      if (!isOpen.value) {
        return;
      }

      event.preventDefault();

      const firstIndex = findEnabledIndex(0, 'first');

      if (firstIndex >= 0) {
        activeIndex.value = firstIndex;
      }
      break;
    }
    case 'End': {
      if (!isOpen.value) {
        return;
      }

      event.preventDefault();

      const lastIndex = findEnabledIndex(0, 'last');

      if (lastIndex >= 0) {
        activeIndex.value = lastIndex;
      }
      break;
    }
    case 'Enter': {
      if (!isOpen.value || activeIndex.value < 0) {
        return;
      }

      event.preventDefault();

      const option = filteredOptions.value[activeIndex.value];

      if (option === undefined || option.disabled) {
        return;
      }

      selectOption(option);
      break;
    }
    case 'Escape': {
      if (!isOpen.value) {
        return;
      }

      event.preventDefault();
      closePopup(true);
      break;
    }
    case 'Tab': {
      if (isOpen.value) {
        closePopup();
      }
      break;
    }
  }
};

const handleOptionClick = (option: ComboboxOption) => {
  if (option.disabled) {
    return;
  }

  selectOption(option);
};

const handleOptionHover = ({ id }: ComboboxOption) => {
  const index = filteredOptions.value.findIndex((option) => option.id === id);

  if (index < 0) {
    return;
  }

  activeIndex.value = index;
};

const handleCompositionStart = () => {
  isComposing.value = true;
};

const handleCompositionEnd = () => {
  isComposing.value = false;
};

// Handle focus - open popup when input receives focus
const handleFocus = () => {
  if (isOpen.value || props.disabled) {
    return;
  }

  openPopup();
};

// Click outside handler
const handleClickOutside = (event: MouseEvent) => {
  const { value: container } = containerRef;

  if (container === undefined) {
    return;
  }

  if (!container.contains(event.target as Node)) {
    closePopup();
  }
};

watch(
  () => isOpen.value,
  (newIsOpen) => {
    if (newIsOpen) {
      document.addEventListener('mousedown', handleClickOutside);
    } else {
      document.removeEventListener('mousedown', handleClickOutside);
    }
  }
);

// Clear active index when filtered options change
watch(
  () => filteredOptions.value.length,
  (newLength) => {
    if (activeIndex.value >= 0 && activeIndex.value >= newLength) {
      activeIndex.value = -1;
    }
  }
);

// Reset search mode when input value matches selected label or becomes empty
watch(
  () => currentInputValue.value,
  (newValue) => {
    if (newValue === '' || newValue === selectedLabel.value) {
      isSearching.value = false;
    }
  }
);

onUnmounted(() => {
  document.removeEventListener('mousedown', handleClickOutside);
});
</script>

使い方

Example
<script setup>
import Combobox from './Combobox.vue';

const options = [
  { id: 'apple', label: 'りんご' },
  { id: 'banana', label: 'バナナ' },
  { id: 'cherry', label: 'さくらんぼ' },
];

function handleSelect(option) {
  console.log('選択:', option);
}

function handleInputChange(value) {
  console.log('入力:', value);
}

function handleOpenChange(isOpen) {
  console.log('開閉:', isOpen);
}
</script>

<template>
  <!-- 基本的な使い方 -->
  <Combobox
    :options="options"
    label="お気に入りのフルーツ"
    placeholder="検索..."
  />

  <!-- デフォルト値あり -->
  <Combobox
    :options="options"
    label="フルーツ"
    default-selected-option-id="banana"
  />

  <!-- 無効なオプションあり -->
  <Combobox
    :options="[
      { id: 'a', label: 'オプション A' },
      { id: 'b', label: 'オプション B', disabled: true },
      { id: 'c', label: 'オプション C' },
    ]"
    label="オプションを選択"
  />

  <!-- フィルタなし (autocomplete="none") -->
  <Combobox
    :options="options"
    label="選択"
    autocomplete="none"
  />

  <!-- コールバック付き -->
  <Combobox
    :options="options"
    label="フルーツ"
    @select="handleSelect"
    @inputchange="handleInputChange"
    @openchange="handleOpenChange"
  />
</template>

API

プロパティ デフォルト 説明
options ComboboxOption[] 必須 id、label、オプションの disabled を持つオプションの配列
label string 必須 表示されるラベルテキスト
placeholder string - 入力のプレースホルダーテキスト
defaultInputValue string "" デフォルトの入力値
defaultSelectedOptionId string - 初期選択されるオプションの ID
autocomplete "none" | "list" | "both" "list" オートコンプリートの動作
disabled boolean false コンボボックスを無効にするかどうか

イベント

イベント ペイロード 説明
@select ComboboxOption オプション選択時に発火
@inputchange string 入力値変更時に発火
@openchange boolean ポップアップ開閉時に発火

テスト

テストは ARIA 属性、キーボード操作、フィルタリング動作、アクセシビリティ要件に対する APG 準拠を検証します。Combobox コンポーネントは2層のテスト戦略を採用しています。

テスト戦略

ユニットテスト (Testing Library)

フレームワーク固有のテストライブラリを使用してコンポーネントの出力を検証します。正しい HTML 構造と ARIA 属性を確認します。

  • ARIA 属性(role、aria-controls、aria-expanded など)
  • キーボード操作(矢印キー、Enter、Escape など)
  • フィルタリング動作とオプションのレンダリング
  • jest-axe によるアクセシビリティ検証

E2E テスト (Playwright)

実際のブラウザ環境で全フレームワークのコンポーネント動作を検証します。インタラクションと クロスフレームワークの一貫性をカバーします。

  • キーボードナビゲーションと選択
  • マウス操作(クリック、ホバー)
  • ライブブラウザでの ARIA 構造
  • aria-activedescendant によるフォーカス管理
  • axe-core によるアクセシビリティスキャン
  • クロスフレームワーク一貫性チェック

テストカテゴリ

高優先度: ARIA 属性 (Unit + E2E)

テスト 説明
role="combobox" Input 要素に combobox ロールがある
role="listbox" ポップアップ要素に listbox ロールがある
role="option" 各オプションに option ロールがある
aria-controls Input が listbox ID を参照(常に存在)
aria-expanded ポップアップの開閉状態を反映
aria-autocomplete "list"、"none"、"both" のいずれかに設定
aria-activedescendant 現在フォーカスされているオプションを参照
aria-selected 現在ハイライトされているオプションを示す
aria-disabled 無効なオプションを示す

高優先度: アクセシブル名 (Unit + E2E)

テスト 説明
aria-labelledby Input が可視ラベル要素を参照
aria-labelledby (listbox) Listbox もラベルを参照

高優先度: キーボード操作 - ポップアップ閉時 (Unit + E2E)

テスト 説明
Down Arrow ポップアップを開き、最初のオプションにフォーカス
Up Arrow ポップアップを開き、最後のオプションにフォーカス
Alt + Down Arrow フォーカスを変更せずにポップアップを開く
入力 ポップアップを開き、オプションをフィルタリング

高優先度: キーボード操作 - ポップアップ開時 (Unit + E2E)

テスト 説明
Down Arrow 次の有効なオプションに移動(折り返しなし)
Up Arrow 前の有効なオプションに移動(折り返しなし)
Home 最初の有効なオプションに移動
End 最後の有効なオプションに移動
Enter フォーカス中のオプションを選択しポップアップを閉じる
Escape ポップアップを閉じ、以前の値を復元
Alt + Up Arrow フォーカス中のオプションを選択しポップアップを閉じる
Tab ポップアップを閉じ、次のフォーカス可能な要素に移動

高優先度: フォーカス管理 (Unit + E2E)

テスト 説明
Input に DOM フォーカス DOM フォーカスは常に input に留まる
aria-activedescendant による仮想フォーカス 視覚的フォーカスは aria-activedescendant で制御
閉じた時にクリア ポップアップが閉じると aria-activedescendant がクリア
無効なオプションをスキップ ナビゲーションが無効なオプションをスキップ

中優先度: フィルタリング (Unit)

テスト 説明
入力時にフィルタ ユーザーが入力するとオプションがフィルタリング
大文字小文字を区別しない フィルタリングは大文字小文字を区別しない
フィルタなし (autocomplete="none") 入力に関係なくすべてのオプションを表示
空の結果 一致がない場合 aria-activedescendant がクリア

中優先度: マウス操作 (E2E)

テスト 説明
オプションをクリック オプションを選択しポップアップを閉じる
オプションにホバー ホバー時に aria-activedescendant を更新
無効なオプションをクリック 無効なオプションは選択不可
外側をクリック 選択せずにポップアップを閉じる

中優先度: IME 入力 (Unit)

テスト 説明
変換中 IME 変換中はキーボードナビゲーションをブロック
変換完了時 変換完了後にフィルタリングを更新

中優先度: コールバック (Unit)

テスト 説明
onSelect 選択時にオプションデータと共に呼び出し
onInputChange 入力値の変更時に呼び出し
onOpenChange ポップアップの開閉時に呼び出し

低優先度: HTML 属性継承 (Unit)

テスト 説明
className コンテナにカスタムクラスが適用される
placeholder プレースホルダーテキストが input に表示
disabled 状態 disabled prop が設定されるとコンポーネントが無効化

低優先度: クロスフレームワーク一貫性 (E2E)

テスト 説明
全フレームワークにコンボボックス React、Vue、Svelte、Astro すべてがコンボボックス要素をレンダリング
クリックで選択 すべてのフレームワークがクリックで正しく選択
一貫した ARIA すべてのフレームワークが一貫した ARIA 構造を持つ

テストツール

詳細なドキュメントは testing-strategy.md (opens in new tab) を参照してください。

Combobox.test.vue.ts
import { render, screen } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import { axe } from 'jest-axe';
import { describe, expect, it, vi } from 'vitest';
import Combobox from './Combobox.vue';

// Default test options
const defaultOptions = [
  { id: 'apple', label: 'Apple' },
  { id: 'banana', label: 'Banana' },
  { id: 'cherry', label: 'Cherry' },
];

// Options with disabled item
const optionsWithDisabled = [
  { id: 'apple', label: 'Apple' },
  { id: 'banana', label: 'Banana', disabled: true },
  { id: 'cherry', label: 'Cherry' },
];

// Options with first item disabled
const optionsWithFirstDisabled = [
  { id: 'apple', label: 'Apple', disabled: true },
  { id: 'banana', label: 'Banana' },
  { id: 'cherry', label: 'Cherry' },
];

// Options with last item disabled
const optionsWithLastDisabled = [
  { id: 'apple', label: 'Apple' },
  { id: 'banana', label: 'Banana' },
  { id: 'cherry', label: 'Cherry', disabled: true },
];

// All disabled options
const allDisabledOptions = [
  { id: 'apple', label: 'Apple', disabled: true },
  { id: 'banana', label: 'Banana', disabled: true },
  { id: 'cherry', label: 'Cherry', disabled: true },
];

describe('Combobox (Vue)', () => {
  // 🔴 High Priority: APG ARIA Attributes
  describe('APG: ARIA Attributes', () => {
    it('input has role="combobox"', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      expect(screen.getByRole('combobox')).toBeInTheDocument();
    });

    it('has accessible name via aria-labelledby', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Select a fruit' },
      });
      const input = screen.getByRole('combobox');
      expect(input).toHaveAccessibleName('Select a fruit');
    });

    it('has aria-controls pointing to listbox', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      const input = screen.getByRole('combobox');
      const listboxId = input.getAttribute('aria-controls');

      expect(listboxId).toBeTruthy();
      expect(document.getElementById(listboxId!)).toHaveAttribute('role', 'listbox');
    });

    it('aria-controls points to existing listbox even when closed', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      const input = screen.getByRole('combobox');
      const listboxId = input.getAttribute('aria-controls');

      expect(listboxId).toBeTruthy();
      const listbox = document.getElementById(listboxId!);
      expect(listbox).toBeInTheDocument();
      expect(listbox).toHaveAttribute('hidden');
    });

    it('has aria-expanded="false" when closed', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      expect(screen.getByRole('combobox')).toHaveAttribute('aria-expanded', 'false');
    });

    it('has aria-expanded="true" when opened', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      expect(input).toHaveAttribute('aria-expanded', 'true');
    });

    it('has aria-autocomplete="list"', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      expect(screen.getByRole('combobox')).toHaveAttribute('aria-autocomplete', 'list');
    });

    it('has aria-autocomplete="none" when autocomplete is none', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', autocomplete: 'none' },
      });
      expect(screen.getByRole('combobox')).toHaveAttribute('aria-autocomplete', 'none');
    });

    it('has aria-activedescendant when option focused', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      expect(input).toHaveAttribute('aria-activedescendant');
      const activeId = input.getAttribute('aria-activedescendant');
      expect(activeId).toBeTruthy();
      expect(document.getElementById(activeId!)).toHaveTextContent('Apple');
    });

    it('clears aria-activedescendant when closed', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(input.getAttribute('aria-activedescendant')).toBeTruthy();

      await user.keyboard('{Escape}');
      expect(input.getAttribute('aria-activedescendant')).toBeFalsy();
    });

    it('listbox has role="listbox"', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      expect(screen.getByRole('listbox')).toBeInTheDocument();
    });

    it('options have role="option"', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const options = screen.getAllByRole('option');
      expect(options).toHaveLength(3);
    });

    it('focused option has aria-selected="true"', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const firstOption = screen.getByRole('option', { name: 'Apple' });
      expect(firstOption).toHaveAttribute('aria-selected', 'true');
    });

    it('disabled option has aria-disabled="true"', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const disabledOption = screen.getByRole('option', { name: 'Banana' });
      expect(disabledOption).toHaveAttribute('aria-disabled', 'true');
    });
  });

  // 🔴 High Priority: APG Keyboard Interaction (Input)
  describe('APG: Keyboard Interaction (Input)', () => {
    it('opens popup and focuses first enabled option on ArrowDown', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      expect(input).toHaveAttribute('aria-expanded', 'true');
      const activeId = input.getAttribute('aria-activedescendant');
      expect(document.getElementById(activeId!)).toHaveTextContent('Apple');
    });

    it('opens popup and focuses last enabled option on ArrowUp', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowUp}');

      expect(input).toHaveAttribute('aria-expanded', 'true');
      const activeId = input.getAttribute('aria-activedescendant');
      expect(document.getElementById(activeId!)).toHaveTextContent('Cherry');
    });

    it('skips disabled first option on ArrowDown', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithFirstDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const activeId = input.getAttribute('aria-activedescendant');
      expect(document.getElementById(activeId!)).toHaveTextContent('Banana');
    });

    it('skips disabled last option on ArrowUp', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithLastDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowUp}');

      const activeId = input.getAttribute('aria-activedescendant');
      expect(document.getElementById(activeId!)).toHaveTextContent('Banana');
    });

    it('closes popup on Escape', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(input).toHaveAttribute('aria-expanded', 'true');

      await user.keyboard('{Escape}');
      expect(input).toHaveAttribute('aria-expanded', 'false');
    });

    it('selects option and closes popup on Enter', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Enter}');

      expect(onSelect).toHaveBeenCalledWith(defaultOptions[0]);
      expect(input).toHaveAttribute('aria-expanded', 'false');
      expect(input).toHaveValue('Apple');
    });

    it('closes popup on Tab', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(input).toHaveAttribute('aria-expanded', 'true');

      await user.keyboard('{Tab}');
      expect(input).toHaveAttribute('aria-expanded', 'false');
    });

    it('opens popup on typing', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.type(input, 'a');

      expect(input).toHaveAttribute('aria-expanded', 'true');
    });

    it('Alt+ArrowDown opens without changing focus position', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{Alt>}{ArrowDown}{/Alt}');

      expect(input).toHaveAttribute('aria-expanded', 'true');
      expect(input.getAttribute('aria-activedescendant')).toBeFalsy();
    });

    it('Alt+ArrowUp commits selection and closes', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Alt>}{ArrowUp}{/Alt}');

      expect(onSelect).toHaveBeenCalledWith(defaultOptions[1]);
      expect(input).toHaveAttribute('aria-expanded', 'false');
    });
  });

  // 🔴 High Priority: APG Keyboard Interaction (Listbox Navigation)
  describe('APG: Keyboard Interaction (Listbox)', () => {
    it('moves to next enabled option on ArrowDown', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');

      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Banana');
    });

    it('moves to previous enabled option on ArrowUp', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Banana');

      await user.keyboard('{ArrowUp}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');
    });

    it('skips disabled option on ArrowDown', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');

      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Cherry');
    });

    it('skips disabled option on ArrowUp', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowUp}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Cherry');

      await user.keyboard('{ArrowUp}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');
    });

    it('moves to first enabled option on Home', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithFirstDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowUp}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Cherry');

      await user.keyboard('{Home}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Banana');
    });

    it('moves to last enabled option on End', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: optionsWithLastDisabled, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');

      await user.keyboard('{End}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Banana');
    });

    it('does not wrap on ArrowDown at last option', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Cherry');

      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Cherry');
    });

    it('does not wrap on ArrowUp at first option', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');

      await user.keyboard('{ArrowUp}');
      expect(
        document.getElementById(input.getAttribute('aria-activedescendant')!)
      ).toHaveTextContent('Apple');
    });
  });

  // 🔴 High Priority: Focus Management
  describe('APG: Focus Management', () => {
    it('keeps DOM focus on input when navigating', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');

      expect(input).toHaveFocus();
    });

    it('updates aria-activedescendant on navigation', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const firstActiveId = input.getAttribute('aria-activedescendant');
      expect(firstActiveId).toBeTruthy();

      await user.keyboard('{ArrowDown}');

      const secondActiveId = input.getAttribute('aria-activedescendant');
      expect(secondActiveId).toBeTruthy();
      expect(secondActiveId).not.toBe(firstActiveId);
    });

    it('maintains focus on input after selection', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Enter}');

      expect(input).toHaveFocus();
    });
  });

  // 🔴 High Priority: Autocomplete
  describe('Autocomplete', () => {
    it('filters options based on input', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.type(input, 'app');

      const options = screen.getAllByRole('option');
      expect(options).toHaveLength(1);
      expect(options[0]).toHaveTextContent('Apple');
    });

    it('shows all options when input is empty', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const options = screen.getAllByRole('option');
      expect(options).toHaveLength(3);
    });

    it('updates input value on selection', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Enter}');

      expect(input).toHaveValue('Banana');
    });

    it('does not filter when autocomplete="none"', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', autocomplete: 'none' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.type(input, 'xyz');

      const options = screen.getAllByRole('option');
      expect(options).toHaveLength(3);
    });
  });

  // 🔴 High Priority: Disabled Options
  describe('Disabled Options', () => {
    it('does not select disabled option on Enter', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: optionsWithFirstDisabled, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{ArrowUp}');
      await user.keyboard('{Enter}');

      expect(onSelect).toHaveBeenCalledWith(optionsWithFirstDisabled[1]);
    });

    it('does not select disabled option on click', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: optionsWithDisabled, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const disabledOption = screen.getByRole('option', { name: 'Banana' });
      await user.click(disabledOption);

      expect(onSelect).not.toHaveBeenCalled();
      expect(input).toHaveAttribute('aria-expanded', 'true');
    });
  });

  // 🔴 High Priority: Mouse Interaction
  describe('Mouse Interaction', () => {
    it('selects option on click', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const option = screen.getByRole('option', { name: 'Banana' });
      await user.click(option);

      expect(onSelect).toHaveBeenCalledWith(defaultOptions[1]);
      expect(input).toHaveValue('Banana');
    });

    it('closes popup on option click', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(input).toHaveAttribute('aria-expanded', 'true');

      const option = screen.getByRole('option', { name: 'Banana' });
      await user.click(option);

      expect(input).toHaveAttribute('aria-expanded', 'false');
    });

    it('closes popup on outside click', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(input).toHaveAttribute('aria-expanded', 'true');

      await user.click(document.body);
      expect(input).toHaveAttribute('aria-expanded', 'false');
    });

    it('updates aria-selected on hover', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const bananaOption = screen.getByRole('option', { name: 'Banana' });
      await user.hover(bananaOption);

      expect(bananaOption).toHaveAttribute('aria-selected', 'true');
      expect(screen.getByRole('option', { name: 'Apple' })).toHaveAttribute(
        'aria-selected',
        'false'
      );
    });
  });

  // 🟡 Medium Priority: Accessibility Validation
  describe('Accessibility', () => {
    it('has no axe violations when closed', async () => {
      const { container } = render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });
      const results = await axe(container);
      expect(results).toHaveNoViolations();
    });

    it('has no axe violations when open', async () => {
      const user = userEvent.setup();
      const { container } = render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      const results = await axe(container);
      expect(results).toHaveNoViolations();
    });

    it('has no axe violations with selection', async () => {
      const user = userEvent.setup();
      const { container } = render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Enter}');

      const results = await axe(container);
      expect(results).toHaveNoViolations();
    });
  });

  // 🟢 Low Priority: Props & Behavior
  describe('Props & Behavior', () => {
    it('calls onSelect when option selected', async () => {
      const user = userEvent.setup();
      const onSelect = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onSelect },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      await user.keyboard('{Enter}');

      expect(onSelect).toHaveBeenCalledWith(defaultOptions[0]);
      expect(onSelect).toHaveBeenCalledTimes(1);
    });

    it('calls onInputChange when typing', async () => {
      const user = userEvent.setup();
      const onInputChange = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onInputChange },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.type(input, 'app');

      expect(onInputChange).toHaveBeenCalledWith('a');
      expect(onInputChange).toHaveBeenCalledWith('ap');
      expect(onInputChange).toHaveBeenCalledWith('app');
    });

    it('calls onOpenChange when popup toggles', async () => {
      const user = userEvent.setup();
      const onOpenChange = vi.fn();
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', onOpenChange },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');
      expect(onOpenChange).toHaveBeenCalledWith(true);

      await user.keyboard('{Escape}');
      expect(onOpenChange).toHaveBeenCalledWith(false);
    });

    it('applies className to container', () => {
      const { container } = render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', className: 'custom-class' },
      });

      expect(container.querySelector('.apg-combobox')).toHaveClass('custom-class');
    });

    it('supports disabled state on combobox', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', disabled: true },
      });

      const input = screen.getByRole('combobox');
      expect(input).toBeDisabled();
    });

    it('supports placeholder', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', placeholder: 'Choose a fruit...' },
      });

      const input = screen.getByRole('combobox');
      expect(input).toHaveAttribute('placeholder', 'Choose a fruit...');
    });

    it('supports defaultInputValue', () => {
      render(Combobox, {
        props: { options: defaultOptions, label: 'Fruit', defaultInputValue: 'Ban' },
      });

      const input = screen.getByRole('combobox');
      expect(input).toHaveValue('Ban');
    });
  });

  // Edge Cases
  describe('Edge Cases', () => {
    it('handles empty options array', () => {
      expect(() => {
        render(Combobox, {
          props: { options: [], label: 'Fruit' },
        });
      }).not.toThrow();

      expect(screen.getByRole('combobox')).toBeInTheDocument();
    });

    it('when all options are disabled, popup opens but no focus set', async () => {
      const user = userEvent.setup();
      render(Combobox, {
        props: { options: allDisabledOptions, label: 'Fruit' },
      });

      const input = screen.getByRole('combobox');
      await user.click(input);
      await user.keyboard('{ArrowDown}');

      expect(input).toHaveAttribute('aria-expanded', 'true');
      expect(input.getAttribute('aria-activedescendant')).toBeFalsy();
    });
  });
});

リソース