CommonInlineEdit.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { fireEvent, waitFor } from '@testing-library/vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import CommonInlineEdit, {
  5. type Props,
  6. } from '#desktop/components/CommonInlineEdit/CommonInlineEdit.vue'
  7. const renderInlineEdit = (props: Partial<Props> = {}) => {
  8. return renderComponent(CommonInlineEdit, {
  9. props: {
  10. name: 'inlineEditTest',
  11. value: 'test value',
  12. label: 'Inline Edit Label',
  13. submitLabel: 'Submit',
  14. cancelLabel: 'Cancel',
  15. ...props,
  16. },
  17. form: true,
  18. router: true,
  19. })
  20. }
  21. describe('CommonInlineEdit', () => {
  22. it('shows by default non editable node', () => {
  23. const wrapper = renderInlineEdit()
  24. expect(wrapper.getByText('test value')).toBeInTheDocument()
  25. expect(wrapper.queryByDisplayValue('test value')).not.toBeInTheDocument()
  26. })
  27. it('supports placeholder on edit input', async () => {
  28. const wrapper = renderInlineEdit({ placeholder: 'test placeholder' })
  29. await wrapper.events.click(wrapper.getByRole('button'))
  30. expect(
  31. await wrapper.findByPlaceholderText('test placeholder'),
  32. ).toBeInTheDocument()
  33. })
  34. it('submits edit on button click and enter', async () => {
  35. const submitEditCallbackSpy = vi.fn()
  36. const wrapper = renderInlineEdit({
  37. onSubmitEdit: (newValue: string) => submitEditCallbackSpy(newValue),
  38. })
  39. await wrapper.events.click(wrapper.getByRole('button'))
  40. await wrapper.events.type(wrapper.getByRole('textbox'), ' update')
  41. await waitFor(() =>
  42. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  43. )
  44. await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
  45. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update')
  46. expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  47. })
  48. it('submits edit on enter key', async () => {
  49. const submitEditCallbackSpy = vi.fn()
  50. const wrapper = renderInlineEdit({
  51. onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
  52. })
  53. await wrapper.events.click(wrapper.getByRole('button'))
  54. await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
  55. await waitFor(() =>
  56. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  57. )
  58. await wrapper.events.keyboard('{enter}')
  59. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  60. })
  61. it('submits on background click', async () => {
  62. const submitEditCallbackSpy = vi.fn()
  63. const wrapper = renderInlineEdit({
  64. onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
  65. })
  66. await wrapper.events.click(wrapper.getByRole('button'))
  67. await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
  68. await waitFor(() =>
  69. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  70. )
  71. await fireEvent.click(document.body)
  72. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  73. })
  74. it('do not stop edit mode when submit promise failed', async () => {
  75. const wrapper = renderInlineEdit({
  76. onSubmitEdit: (): Promise<void> => {
  77. return new Promise((resolve, reject) => {
  78. reject()
  79. })
  80. },
  81. })
  82. await wrapper.events.click(wrapper.getByRole('button'))
  83. await wrapper.events.type(wrapper.getByRole('textbox'), ' update')
  84. await waitFor(() =>
  85. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  86. )
  87. await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
  88. expect(wrapper.getByRole('textbox')).toBeInTheDocument()
  89. })
  90. it('focuses field on edit', async () => {
  91. const wrapper = renderInlineEdit()
  92. await wrapper.events.click(wrapper.getByRole('button'))
  93. await waitFor(() =>
  94. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  95. )
  96. expect(wrapper.getByRole('textbox')).toHaveFocus()
  97. })
  98. it('cancels on button click', async () => {
  99. const wrapper = renderInlineEdit()
  100. await wrapper.events.click(wrapper.getByRole('button'))
  101. await wrapper.events.click(wrapper.getByRole('button', { name: 'Cancel' }))
  102. expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  103. expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  104. })
  105. it('cancels on escape key', async () => {
  106. const wrapper = renderInlineEdit()
  107. await wrapper.events.click(wrapper.getByRole('button'))
  108. await waitFor(() =>
  109. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  110. )
  111. await wrapper.events.keyboard('{esc}')
  112. expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  113. })
  114. it('disables submit if input is incorrect and required is true', async () => {
  115. const wrapper = renderInlineEdit({ required: true })
  116. await wrapper.events.click(wrapper.getByRole('button'))
  117. await waitFor(() =>
  118. expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
  119. )
  120. await wrapper.events.clear(wrapper.getByRole('textbox'))
  121. expect(wrapper.emitted()['submit-edit']).toBeFalsy()
  122. expect(wrapper.getByRole('button', { name: 'Submit' })).toBeDisabled()
  123. expect(wrapper.getByRole('textbox')).toBeInTheDocument()
  124. })
  125. it('supports adding attributes on label', () => {
  126. const wrapper = renderInlineEdit({
  127. labelAttrs: {
  128. role: 'heading',
  129. 'aria-level': '1',
  130. },
  131. })
  132. expect(wrapper.getByRole('heading', { level: 1 })).toBeInTheDocument()
  133. })
  134. it('allows inline edit to be take up full width if set to block', () => {
  135. const wrapper = renderInlineEdit({
  136. block: true,
  137. })
  138. expect(wrapper.getByRole('button')).toHaveClass('w-full')
  139. })
  140. it('disables input if disabled prop is true', async () => {
  141. const wrapper = renderInlineEdit({ disabled: true })
  142. expect(wrapper.getByText('test value')).toBeInTheDocument()
  143. await wrapper.events.click(wrapper.getByText('test value'))
  144. expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  145. })
  146. it('detects links if set to true and renders it as link only in label', async () => {
  147. const wrapper = renderInlineEdit({
  148. detectLinks: true,
  149. value: 'https://zammad.com/en',
  150. })
  151. expect(
  152. wrapper.getByRole('link', { name: 'https://zammad.com/en' }),
  153. ).toBeInTheDocument()
  154. })
  155. it('displays initial edit value if editing got activated', async () => {
  156. const wrapper = renderInlineEdit({
  157. initialEditValue: 'initial Value',
  158. value: 'default Value',
  159. })
  160. await wrapper.events.click(wrapper.getByText('default Value'))
  161. expect(wrapper.getByRole('textbox')).toHaveValue('initial Value')
  162. })
  163. it('support loading', async () => {
  164. const wrapper = renderInlineEdit({
  165. initialEditValue: 'initial Value',
  166. value: 'default Value',
  167. loading: true,
  168. })
  169. await wrapper.events.click(wrapper.getByText('default Value'))
  170. expect(wrapper.getByRole('textbox')).toBeDisabled()
  171. })
  172. it('supports adding alternative background color', async () => {
  173. const wrapper = renderInlineEdit({
  174. alternativeBackground: true,
  175. })
  176. await wrapper.events.click(wrapper.getByText('test value'))
  177. expect(wrapper.html()).toContain(
  178. 'before:bg-neutral-50 before:dark:bg-gray-500',
  179. )
  180. await wrapper.rerender({
  181. alternativeBackground: false,
  182. })
  183. expect(wrapper.html()).toContain(
  184. 'before:bg-blue-200 before:dark:bg-gray-700',
  185. )
  186. expect(wrapper.html()).not.toContain(
  187. 'before:bg-neutral-50 before:dark:bg-gray-500',
  188. )
  189. })
  190. })