|
1 | 1 | import type { DOMWrapper, VueWrapper } from '@vue/test-utils' |
2 | 2 | import { mount } from '@vue/test-utils' |
3 | | -import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
4 | 4 | import { axe } from 'vitest-axe' |
5 | 5 | import { defineComponent, h, nextTick, ref } from 'vue' |
6 | 6 | import { useKbd } from '@/shared' |
7 | 7 | import { handleSubmit } from '@/test' |
8 | | -import { ListboxContent, ListboxItem, ListboxRoot, ListboxVirtualizer } from '.' |
| 8 | +import { ListboxContent, ListboxFilter, ListboxItem, ListboxRoot, ListboxVirtualizer } from '.' |
9 | 9 | import Listbox from './story/_Listbox.vue' |
10 | 10 |
|
11 | 11 | describe('given default Listbox', () => { |
@@ -538,3 +538,78 @@ describe('given Listbox in a form', async () => { |
538 | 538 | }) |
539 | 539 | }) |
540 | 540 | }) |
| 541 | + |
| 542 | +describe('given Listbox with ListboxFilter handling IME composition', () => { |
| 543 | + let wrapper: VueWrapper |
| 544 | + let input: DOMWrapper<HTMLInputElement> |
| 545 | + let updates: string[] |
| 546 | + |
| 547 | + window.HTMLElement.prototype.scrollIntoView = vi.fn() |
| 548 | + |
| 549 | + const ANDROID_UA = 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36' |
| 550 | + |
| 551 | + beforeEach(() => { |
| 552 | + document.body.innerHTML = '' |
| 553 | + updates = [] |
| 554 | + const search = ref('') |
| 555 | + wrapper = mount(defineComponent({ |
| 556 | + setup() { |
| 557 | + return () => h(ListboxRoot, {}, { |
| 558 | + default: () => [ |
| 559 | + h(ListboxFilter, { |
| 560 | + 'modelValue': search.value, |
| 561 | + 'onUpdate:modelValue': (v: string) => { |
| 562 | + updates.push(v) |
| 563 | + search.value = v |
| 564 | + }, |
| 565 | + }), |
| 566 | + h(ListboxContent, {}, { |
| 567 | + default: () => ['Apple', 'Banana'].map(i => h(ListboxItem, { value: i }, { default: () => i })), |
| 568 | + }), |
| 569 | + ], |
| 570 | + }) |
| 571 | + }, |
| 572 | + }), { attachTo: document.body }) |
| 573 | + input = wrapper.find('input') |
| 574 | + }) |
| 575 | + |
| 576 | + afterEach(() => { |
| 577 | + delete (window.navigator as { userAgent?: string }).userAgent |
| 578 | + }) |
| 579 | + |
| 580 | + it('should not update modelValue during plain-text composition off Android (desktop Pinyin preedit)', async () => { |
| 581 | + await input.trigger('compositionstart') |
| 582 | + input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'xiang', bubbles: true })) |
| 583 | + input.element.value = 'xiang' |
| 584 | + await input.trigger('input') |
| 585 | + |
| 586 | + expect(updates).toEqual([]) |
| 587 | + }) |
| 588 | + |
| 589 | + it('should update modelValue live during plain-text (autocorrect) composition on Android', async () => { |
| 590 | + Object.defineProperty(window.navigator, 'userAgent', { value: ANDROID_UA, configurable: true }) |
| 591 | + |
| 592 | + await input.trigger('compositionstart') |
| 593 | + input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'Br', bubbles: true })) |
| 594 | + input.element.value = 'Br' |
| 595 | + await input.trigger('input') |
| 596 | + |
| 597 | + expect(updates).toEqual(['Br']) |
| 598 | + }) |
| 599 | + |
| 600 | + it('should not update modelValue during CJK IME composition on Android until compositionend', async () => { |
| 601 | + Object.defineProperty(window.navigator, 'userAgent', { value: ANDROID_UA, configurable: true }) |
| 602 | + |
| 603 | + await input.trigger('compositionstart') |
| 604 | + input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'かんじ', bubbles: true })) |
| 605 | + input.element.value = 'かんじ' |
| 606 | + await input.trigger('input') |
| 607 | + |
| 608 | + expect(updates).toEqual([]) |
| 609 | + |
| 610 | + await input.trigger('compositionend') |
| 611 | + await nextTick() |
| 612 | + |
| 613 | + expect(updates).toEqual(['かんじ']) |
| 614 | + }) |
| 615 | +}) |
0 commit comments