Repository: element-plus/element-plus
File: packages/components/autocomplete/__tests__/autocomplete.test.tsx (TypeScript)

Code before:
    await wrapper.setProps({ highlightFirstItem: true })

    await wrapper.find('input').trigger('focus')
    vi.runAllTimers()
    await nextTick()

    expect(document.body.querySelector('.highlighted')).toBeDefined()
  })

  test('keyboard navigation should loop when loopNavigation is true', async () => {
    const wrapper = _mount({ debounce: 10, loopNavigation: true }, 'arr')
    await nextTick()

    const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
      typeof Autocomplete
    >
    const input = wrapper.find('input')

    await input.trigger('focus')
    vi.runAllTimers()
    await nextTick()

    const length = target.suggestions.length

    for (let i = 0; i < length; i++) {
      await input.trigger('keydown', { key: EVENT_CODE.down })
      expect(target.highlightedIndex).toBe(i)
    }

    await input.trigger('keydown', { key: EVENT_CODE.down })
    expect(target.highlightedIndex).toBe(0)

    await input.trigger('keydown', { key: EVENT_CODE.up })
    expect(target.highlightedIndex).toBe(length - 1)
  })

  test('keyboard navigation should not loop when loopNavigation is false', async () => {
    const wrapper = _mount({ debounce: 10, loopNavigation: false }, 'arr')
    await nextTick()

    const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
      typeof Autocomplete
    >
    const input = wrapper.find('input')

    await input.trigger('focus')
    vi.runAllTimers()
    await nextTick()

    const length = target.suggestions.length


Review comment (suggestion):
```suggestion
      await input.trigger('keydown', { code: EVENT_CODE.down })
```

Code after:
    await wrapper.setProps({ highlightFirstItem: true })

    await wrapper.find('input').trigger('focus')
    vi.runAllTimers()
    await nextTick()

    expect(document.body.querySelector('.highlighted')).toBeDefined()
  })

  test('keyboard navigation should loop when loopNavigation is true', async () => {
    const wrapper = _mount({ debounce: 10, loopNavigation: true }, 'arr')
    await nextTick()

    const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
      typeof Autocomplete
    >
    const input = wrapper.find('input')

    await input.trigger('focus')
    vi.runAllTimers()
    await nextTick()

    const length = target.suggestions.length

    for (let i = 0; i < length; i++) {
      await input.trigger('keydown', { code: EVENT_CODE.down })
      expect(target.highlightedIndex).toBe(i)
    }

    await input.trigger('keydown', { code: EVENT_CODE.down })
    expect(target.highlightedIndex).toBe(0)

    await input.trigger('keydown', { code: EVENT_CODE.up })
    expect(target.highlightedIndex).toBe(length - 1)
  })

  test('keyboard navigation should not loop when loopNavigation is false', async () => {
    const wrapper = _mount({ debounce: 10, loopNavigation: false }, 'arr')
    await nextTick()

    const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
      typeof Autocomplete
    >
    const input = wrapper.find('input')

    await input.trigger('focus')
    vi.runAllTimers()
    await nextTick()

    const length = target.suggestions.length