CommonInputSearch.spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { onMounted, ref } from 'vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import CommonInputSearch, {
  5. type CommonInputSearchExpose,
  6. } from '../CommonInputSearch.vue'
  7. describe('testing input for searching', () => {
  8. it('renders input', async () => {
  9. const view = renderComponent(CommonInputSearch, {
  10. vModel: {
  11. modelValue: '',
  12. },
  13. })
  14. expect(view.getByIconName('search')).toBeInTheDocument()
  15. const search = view.getByRole('searchbox')
  16. expect(search).toHaveAttribute('placeholder', 'Search…')
  17. const clearButton = view.getByIconName('backspace2')
  18. expect(clearButton).toHaveClass('invisible')
  19. await view.events.type(search, 'test')
  20. expect(clearButton).not.toHaveClass('invisible')
  21. await view.events.click(clearButton)
  22. expect(search).toHaveDisplayValue('')
  23. })
  24. it('can focus outside of the component', async () => {
  25. let focus: () => void
  26. const component = {
  27. components: { CommonInputSearch },
  28. template: `<CommonInputSearch ref="searchInput" />`,
  29. setup() {
  30. const searchInput = ref<null | CommonInputSearchExpose>()
  31. onMounted(() => {
  32. ;({ focus } = searchInput.value!)
  33. })
  34. return { searchInput }
  35. },
  36. }
  37. const view = renderComponent(component)
  38. focus!()
  39. expect(view.getByRole('searchbox')).toHaveFocus()
  40. })
  41. it('provides search suggestion', async () => {
  42. const modelValue = ref('')
  43. const view = renderComponent(CommonInputSearch, {
  44. vModel: {
  45. modelValue,
  46. },
  47. })
  48. const search = view.getByRole('searchbox')
  49. expect(search).toHaveDisplayValue('')
  50. expect(view.queryByTestId('suggestion')).not.toBeInTheDocument()
  51. await view.events.type(search, 'foo')
  52. expect(modelValue.value).toBe('foo')
  53. await view.rerender({
  54. suggestion: 'foobar',
  55. })
  56. const suggestion = view.getByTestId('suggestion')
  57. expect(suggestion.firstChild).toHaveTextContent('foo')
  58. expect(suggestion.lastChild).toHaveTextContent('bar')
  59. await view.events.keyboard('{Tab}')
  60. expect(modelValue.value).toBe('foobar')
  61. expect(suggestion).not.toBeInTheDocument()
  62. })
  63. })