CommonInlineEdit.spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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', async () => {
  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(
  43. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  44. ).toBeInTheDocument(),
  45. )
  46. await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
  47. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update')
  48. expect(
  49. wrapper.queryByRole('textbox', { name: 'Inline Edit Label' }),
  50. ).not.toBeInTheDocument()
  51. })
  52. it('submits edit on enter key', async () => {
  53. const submitEditCallbackSpy = vi.fn()
  54. const wrapper = renderInlineEdit({
  55. onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
  56. })
  57. await wrapper.events.click(wrapper.getByRole('button'))
  58. await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
  59. await waitFor(() =>
  60. expect(
  61. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  62. ).toBeInTheDocument(),
  63. )
  64. await wrapper.events.keyboard('{enter}')
  65. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  66. })
  67. it('submits on background click', async () => {
  68. const submitEditCallbackSpy = vi.fn()
  69. const wrapper = renderInlineEdit({
  70. onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
  71. })
  72. await wrapper.events.click(wrapper.getByRole('button'))
  73. await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
  74. await waitFor(() =>
  75. expect(
  76. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  77. ).toBeInTheDocument(),
  78. )
  79. await fireEvent.click(document.body)
  80. expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
  81. })
  82. it('do not stop edit mode when submit promise failed', async () => {
  83. const wrapper = renderInlineEdit({
  84. onSubmitEdit: (): Promise<void> => {
  85. return new Promise((resolve, reject) => {
  86. reject()
  87. })
  88. },
  89. })
  90. await wrapper.events.click(wrapper.getByRole('button'))
  91. await wrapper.events.type(wrapper.getByRole('textbox'), ' update')
  92. await waitFor(() =>
  93. expect(
  94. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  95. ).toBeInTheDocument(),
  96. )
  97. await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
  98. expect(
  99. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  100. ).toBeInTheDocument()
  101. })
  102. it('focuses field on edit', async () => {
  103. const wrapper = renderInlineEdit()
  104. await wrapper.events.click(wrapper.getByRole('button'))
  105. await waitFor(() =>
  106. expect(
  107. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  108. ).toBeInTheDocument(),
  109. )
  110. expect(
  111. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  112. ).toHaveFocus()
  113. })
  114. it('cancels on button click', async () => {
  115. const wrapper = renderInlineEdit()
  116. await wrapper.events.click(wrapper.getByRole('button'))
  117. await wrapper.events.click(wrapper.getByRole('button', { name: 'Cancel' }))
  118. expect(
  119. wrapper.queryByRole('textbox', { name: 'Inline Edit Label' }),
  120. ).not.toBeInTheDocument()
  121. expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  122. })
  123. it('cancels on escape key', async () => {
  124. const wrapper = renderInlineEdit()
  125. await wrapper.events.click(wrapper.getByRole('button'))
  126. await waitFor(() =>
  127. expect(
  128. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  129. ).toBeInTheDocument(),
  130. )
  131. await wrapper.events.keyboard('{esc}')
  132. expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
  133. })
  134. it('disables submit if input is incorrect and required is true', async () => {
  135. const wrapper = renderInlineEdit({ required: true })
  136. await wrapper.events.click(wrapper.getByRole('button'))
  137. await waitFor(() =>
  138. expect(
  139. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  140. ).toBeInTheDocument(),
  141. )
  142. await wrapper.events.clear(wrapper.getByRole('textbox'))
  143. expect(wrapper.emitted()['submit-edit']).toBeFalsy()
  144. expect(wrapper.getByRole('button', { name: 'Submit' })).toBeDisabled()
  145. expect(
  146. wrapper.getByRole('textbox', { name: 'Inline Edit Label' }),
  147. ).toBeInTheDocument()
  148. })
  149. it('supports adding attributes on label', () => {
  150. const wrapper = renderInlineEdit({
  151. labelAttrs: {
  152. role: 'heading',
  153. 'aria-level': '1',
  154. },
  155. })
  156. expect(wrapper.getByRole('heading', { level: 1 })).toBeInTheDocument()
  157. })
  158. it('allows inline edit to be take up full width if set to block', () => {
  159. const wrapper = renderInlineEdit({
  160. block: true,
  161. })
  162. expect(wrapper.getByRole('button')).toHaveClass('w-full')
  163. })
  164. it('disables input if disabled prop is true', async () => {
  165. const wrapper = renderInlineEdit({ disabled: true })
  166. expect(wrapper.getByText('test value')).toBeInTheDocument()
  167. await wrapper.events.click(wrapper.getByText('test value'))
  168. expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
  169. })
  170. it('detects links if set to true and renders it as link only in label', async () => {
  171. const wrapper = renderInlineEdit({
  172. detectLinks: true,
  173. value: 'https://zammad.com/en',
  174. })
  175. expect(
  176. wrapper.getByRole('link', { name: 'https://zammad.com/en' }),
  177. ).toBeInTheDocument()
  178. })
  179. it('displays initial edit value if editing got activated', async () => {
  180. const wrapper = renderInlineEdit({
  181. initialEditValue: 'initial Value',
  182. value: 'default Value',
  183. })
  184. await wrapper.events.click(wrapper.getByText('default Value'))
  185. expect(wrapper.getByRole('textbox')).toHaveValue('initial Value')
  186. })
  187. it('supports adding alternative background color', async () => {
  188. const wrapper = renderInlineEdit({
  189. alternativeBackground: true,
  190. })
  191. await wrapper.events.click(wrapper.getByText('test value'))
  192. expect(wrapper.html()).toContain(
  193. 'before:bg-neutral-50 before:dark:bg-gray-500',
  194. )
  195. await wrapper.rerender({
  196. alternativeBackground: false,
  197. })
  198. expect(wrapper.html()).toContain(
  199. 'before:bg-blue-200 before:dark:bg-gray-700',
  200. )
  201. expect(wrapper.html()).not.toContain(
  202. 'before:bg-neutral-50 before:dark:bg-gray-500',
  203. )
  204. })
  205. })