previous-searches.spec.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { getByText, getAllByRole } from '@testing-library/vue'
  3. import { visitView } from '#tests/support/components/visitView.ts'
  4. import { clearMockClient } from '#tests/support/mock-apollo-client.ts'
  5. import type { MockGraphQLInstance } from '#tests/support/mock-graphql-api.ts'
  6. import { mockPermissions } from '#tests/support/mock-permissions.ts'
  7. import { nullableMock, waitUntil } from '#tests/support/utils.ts'
  8. import { mockSearchOverview } from '../graphql/mocks/mockSearchOverview.ts'
  9. describe('testing previous searches block', () => {
  10. let mockSearchApi: MockGraphQLInstance
  11. beforeEach(() => {
  12. mockPermissions(['ticket.agent'])
  13. mockSearchApi = mockSearchOverview([])
  14. })
  15. it('previous searches', async () => {
  16. localStorage.clear()
  17. const view = await visitView('/search/user')
  18. const getByTextInLastSearch = (text: string) => {
  19. return getByText(view.getByTestId('lastSearches'), text)
  20. }
  21. const typeInSearch = async (text: string) => {
  22. await view.events.debounced(() =>
  23. view.events.type(view.getByPlaceholderText('Search…'), text),
  24. )
  25. await waitUntil(() => mockSearchApi.calls.resolve)
  26. }
  27. const clearSearch = () => {
  28. return view.events.debounced(() =>
  29. view.events.clear(view.getByPlaceholderText('Search…')),
  30. )
  31. }
  32. expect(getByTextInLastSearch('No previous searches')).toBeInTheDocument()
  33. await typeInSearch('search')
  34. expect(getByTextInLastSearch('search')).toBeInTheDocument()
  35. await typeInSearch('123')
  36. expect(getByTextInLastSearch('search123')).toBeInTheDocument()
  37. await clearSearch()
  38. await typeInSearch('search')
  39. let items = getAllByRole(view.getByTestId('lastSearches'), 'listitem')
  40. expect(items).toHaveLength(2)
  41. expect(items[0]).toHaveTextContent(/^search$/)
  42. expect(items[1]).toHaveTextContent(/^search123$/)
  43. await clearSearch()
  44. await typeInSearch('test 1')
  45. await clearSearch()
  46. await typeInSearch('test 2')
  47. await clearSearch()
  48. await typeInSearch('test 3')
  49. items = getAllByRole(view.getByTestId('lastSearches'), 'listitem')
  50. expect(items).toHaveLength(5)
  51. expect(items[0]).toHaveTextContent(/^test 3$/)
  52. expect(items[1]).toHaveTextContent(/^test 2$/)
  53. expect(items[2]).toHaveTextContent(/^test 1$/)
  54. expect(items[3]).toHaveTextContent(/^search$/)
  55. expect(items[4]).toHaveTextContent(/^search123$/)
  56. await clearSearch()
  57. await typeInSearch('test 4')
  58. items = getAllByRole(view.getByTestId('lastSearches'), 'listitem')
  59. expect(items).toHaveLength(5)
  60. expect(items[0]).toHaveTextContent(/^test 4$/)
  61. expect(items[4]).toHaveTextContent(/^search$/)
  62. await clearSearch()
  63. await typeInSearch('search')
  64. items = getAllByRole(view.getByTestId('lastSearches'), 'listitem')
  65. expect(items).toHaveLength(5)
  66. expect(items[0]).toHaveTextContent(/^search$/)
  67. })
  68. it('clicking previous searches calls api', async () => {
  69. localStorage.clear()
  70. const view = await visitView('/search/user')
  71. const input = view.getByPlaceholderText('Search…')
  72. await view.events.debounced(() => view.events.type(input, 'search'))
  73. await view.events.debounced(() => view.events.type(input, '123'))
  74. await waitUntil(() => mockSearchApi.calls.resolve)
  75. const items = view.getAllByRole('listitem')
  76. expect(items).toHaveLength(2)
  77. expect(items[0]).toHaveTextContent('search123')
  78. expect(items[1]).toHaveTextContent('search')
  79. await view.events.debounced(() => view.events.click(items[1]))
  80. // :TODO check why this fails on ui works?
  81. // items = view.getAllByRole('listitem')
  82. // expect(items[0]).toHaveTextContent('search')
  83. // expect(items[1]).toHaveTextContent('search123')
  84. expect(mockSearchApi.spies.resolve).toHaveBeenNthCalledWith(1, {
  85. onlyIn: 'User',
  86. search: 'search',
  87. limit: 30,
  88. })
  89. expect(mockSearchApi.spies.resolve).toHaveBeenNthCalledWith(2, {
  90. onlyIn: 'User',
  91. search: 'search123',
  92. limit: 30,
  93. })
  94. })
  95. it('emptying out search shows last searches', async () => {
  96. localStorage.clear()
  97. clearMockClient()
  98. mockSearchApi.willResolve({
  99. search: [
  100. nullableMock({
  101. __typename: 'User',
  102. id: '1sdsada',
  103. internalId: 1,
  104. updatedAt: new Date().toISOString(),
  105. firstname: 'Max',
  106. lastname: 'Mustermann',
  107. }),
  108. ],
  109. })
  110. const view = await visitView('/search/user')
  111. const input = view.getByPlaceholderText('Search…')
  112. await view.events.debounced(() => view.events.type(input, 'search'))
  113. await view.events.debounced(() => view.events.type(input, '123'))
  114. await waitUntil(() => mockSearchApi.calls.resolve)
  115. expect(view.container).toHaveTextContent('Max Mustermann')
  116. await view.events.debounced(() => view.events.clear(input))
  117. expect(view.container).not.toHaveTextContent('Max Mustermann')
  118. expect(view.getByTestId('lastSearches')).toBeInTheDocument()
  119. })
  120. it('shows last searches, when openning page', async () => {
  121. localStorage.clear()
  122. localStorage.setItem(
  123. 'lastSearches',
  124. JSON.stringify(['search', 'search123']),
  125. )
  126. const view = await visitView('/search/user')
  127. expect(view.getByRole('button', { name: 'search' })).toBeInTheDocument()
  128. expect(view.getByRole('button', { name: 'search123' })).toBeInTheDocument()
  129. const input = view.getByPlaceholderText('Search…')
  130. await view.events.debounced(() => view.events.type(input, 'search55'))
  131. expect(
  132. view.queryByRole('button', { name: 'search55' }),
  133. 'hides last searches and shows buttons for types',
  134. ).not.toBeInTheDocument()
  135. await view.events.clear(input)
  136. expect(view.getByRole('button', { name: 'search55' })).toBeInTheDocument()
  137. })
  138. })