moving-ticket-overview.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { getAllByTestId, getByTestId, within } from '@testing-library/vue'
  3. import { getByIconName } from '#tests/support/components/iconQueries.ts'
  4. import { visitView } from '#tests/support/components/visitView.ts'
  5. import { mockUserCurrent } from '#tests/support/mock-userCurrent.ts'
  6. import { mockTicketOverviews } from '#tests/support/mocks/ticket-overviews.ts'
  7. import {
  8. NotificationTypes,
  9. useNotifications,
  10. } from '#shared/components/CommonNotifications/index.ts'
  11. import { getTicketOverviewStorage } from '#mobile/entities/ticket/helpers/ticketOverviewStorage.ts'
  12. const actualLocalStorage = window.localStorage
  13. describe('playing with overviews', () => {
  14. beforeEach(() => {
  15. mockUserCurrent({ id: '666' })
  16. mockTicketOverviews()
  17. })
  18. afterEach(() => {
  19. window.localStorage = actualLocalStorage
  20. const { saveOverviews } = getTicketOverviewStorage()
  21. saveOverviews([])
  22. })
  23. it('loading overviews from local storage', async () => {
  24. const { saveOverviews, LOCAL_STORAGE_NAME } = getTicketOverviewStorage()
  25. saveOverviews(['1', '2'])
  26. const view = await visitView('/favorite/ticker-overviews/edit')
  27. const includedOverviewsUtils = within(
  28. await view.findByTestId('includedOverviews'),
  29. )
  30. const includedOverviews =
  31. await includedOverviewsUtils.findAllByTestId('overviewItem')
  32. expect(includedOverviews).toHaveLength(2)
  33. expect(includedOverviews[0]).toHaveTextContent('Overview 1')
  34. expect(includedOverviews[1]).toHaveTextContent('Overview 2')
  35. const excludedOverviewsUtils = within(
  36. await view.findByTestId('excludedOverviews'),
  37. )
  38. const excludedOverviews =
  39. excludedOverviewsUtils.getAllByTestId('overviewItem')
  40. expect(excludedOverviews).toHaveLength(1)
  41. expect(excludedOverviews[0]).toHaveTextContent('Overview 3')
  42. vi.stubGlobal('localStorage', {
  43. setItem: vi.fn(),
  44. })
  45. await view.events.click(view.getByText('Save'))
  46. expect(localStorage.setItem).toHaveBeenCalledWith(
  47. LOCAL_STORAGE_NAME,
  48. '["1","2"]',
  49. )
  50. })
  51. it('removing/adding overviews', async () => {
  52. const { LOCAL_STORAGE_NAME } = getTicketOverviewStorage()
  53. const view = await visitView('/favorite/ticker-overviews/edit')
  54. const buttonsRemove = await view.findAllByIconName('minus')
  55. expect(buttonsRemove).toHaveLength(3)
  56. const [overviewOneButton] = buttonsRemove
  57. await view.events.click(overviewOneButton)
  58. expect(view.getAllByIconName('minus')).toHaveLength(2)
  59. const overviewOneInExcluded = getByTestId(
  60. view.getByTestId('excludedOverviews'),
  61. 'overviewItem',
  62. )
  63. expect(overviewOneInExcluded).toHaveTextContent('Overview 1')
  64. const buttonAdd = getByIconName(overviewOneInExcluded, 'plus')
  65. await view.events.click(buttonAdd)
  66. const includedOverviews = getAllByTestId(
  67. view.getByTestId('includedOverviews'),
  68. 'overviewItem',
  69. )
  70. expect(includedOverviews.at(-1)).toHaveTextContent('Overview 1')
  71. vi.stubGlobal('localStorage', {
  72. setItem: vi.fn(),
  73. })
  74. await view.events.click(view.getByText('Save'))
  75. expect(localStorage.setItem).toHaveBeenCalledWith(
  76. LOCAL_STORAGE_NAME,
  77. '["2","3","1"]',
  78. )
  79. const { notify } = useNotifications()
  80. expect(notify).toHaveBeenCalledWith({
  81. id: 'overview-save',
  82. type: NotificationTypes.Success,
  83. message: 'Ticket Overview settings are saved.',
  84. })
  85. })
  86. it('gives error, when trying to save no overviews', async () => {
  87. const { saveOverviews } = getTicketOverviewStorage()
  88. saveOverviews(['1'])
  89. const view = await visitView('/favorite/ticker-overviews/edit')
  90. const buttonRemove = await view.findByIconName('minus')
  91. await view.events.click(buttonRemove)
  92. await view.events.click(view.getByText('Save'))
  93. const { notify } = useNotifications()
  94. expect(notify).toHaveBeenCalledWith({
  95. id: 'no-overview',
  96. type: NotificationTypes.Error,
  97. message: 'Please select at least one ticket overview',
  98. })
  99. })
  100. })