FieldCheckbox.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. import CheckboxVariant from '@common/types/form/fields'
  3. import { getNode } from '@formkit/core'
  4. import { FormKit } from '@formkit/vue'
  5. import { getAllByRole } from '@testing-library/vue'
  6. import {
  7. ExtendedMountingOptions,
  8. renderComponent,
  9. } from '@tests/support/components'
  10. import { waitForTimeout } from '@tests/support/utils'
  11. const wrapperParameters = {
  12. form: true,
  13. formField: true,
  14. }
  15. const renderCheckbox = (options: ExtendedMountingOptions<unknown> = {}) =>
  16. renderComponent(FormKit, {
  17. ...wrapperParameters,
  18. props: {
  19. name: 'checkbox',
  20. type: 'checkbox',
  21. id: 'checkbox',
  22. label: 'Checkbox',
  23. variant: CheckboxVariant.default,
  24. },
  25. ...options,
  26. })
  27. describe('Form - Field - Checkbox (Formkit-BuildIn)', () => {
  28. it('can render a checkbox', () => {
  29. const view = renderCheckbox()
  30. const checkbox = view.getByLabelText('Checkbox')
  31. expect(checkbox).toHaveAttribute('id', 'checkbox')
  32. expect(checkbox).toHaveAttribute('type', 'checkbox')
  33. const node = getNode('checkbox')
  34. expect(node?.value).toBe(undefined)
  35. })
  36. it('set some props', async () => {
  37. const view = renderCheckbox({
  38. props: {
  39. label: 'Checkbox',
  40. type: 'checkbox',
  41. help: 'This is the help text',
  42. },
  43. })
  44. expect(view.getByLabelText('Checkbox')).toBeInTheDocument()
  45. expect(view.getByText(/This is the help text/)).toBeInTheDocument()
  46. })
  47. it('check for the input event', async () => {
  48. const view = renderCheckbox({
  49. props: {
  50. label: 'Checkbox',
  51. type: 'checkbox',
  52. help: 'This is the help text',
  53. },
  54. })
  55. const checkbox = view.getByLabelText('Checkbox')
  56. await view.events.click(checkbox)
  57. await waitForTimeout()
  58. expect(view.emitted().input).toBeTruthy()
  59. const emittedInput = view.emitted().input as Array<Array<InputEvent>>
  60. expect(emittedInput[0][0]).toBe(true)
  61. })
  62. it('check for the input value when on-value and off-value is used', async () => {
  63. const view = renderCheckbox({
  64. ...wrapperParameters,
  65. props: {
  66. label: 'Checkbox',
  67. name: 'checkbox',
  68. type: 'checkbox',
  69. id: 'checkbox',
  70. onValue: 'yes',
  71. offValue: 'no',
  72. },
  73. })
  74. const checkbox = view.getByLabelText('Checkbox')
  75. await view.events.click(checkbox)
  76. await waitForTimeout()
  77. expect(view.emitted().input).toBeTruthy()
  78. let emittedInput = view.emitted().input as Array<Array<InputEvent>>
  79. expect(emittedInput[0][0]).toBe('yes')
  80. await view.events.click(checkbox)
  81. await waitForTimeout()
  82. emittedInput = view.emitted().input as Array<Array<InputEvent>>
  83. expect(emittedInput[1][0]).toBe('no')
  84. })
  85. it('can use variant', async () => {
  86. const view = renderCheckbox({
  87. ...wrapperParameters,
  88. props: {
  89. label: 'Checkbox',
  90. name: 'checkbox',
  91. type: 'checkbox',
  92. id: 'checkbox',
  93. onValue: 'yes',
  94. offValue: 'no',
  95. variant: CheckboxVariant.switch,
  96. },
  97. })
  98. expect(view.getByLabelText('Checkbox')).toHaveClass('sr-only')
  99. })
  100. it('can be disabled', async () => {
  101. const view = renderCheckbox({
  102. props: {
  103. label: 'Checkbox',
  104. name: 'checkbox',
  105. type: 'checkbox',
  106. id: 'checkbox',
  107. variant: CheckboxVariant.switch,
  108. },
  109. })
  110. const checklist = view.getByLabelText('Checkbox')
  111. expect(checklist).not.toHaveAttribute('disabled')
  112. await view.rerender({
  113. disabled: true,
  114. })
  115. expect(checklist).toHaveAttribute('disabled')
  116. // Rest the disabled state again and check if it's enabled again.
  117. await view.rerender({
  118. disabled: false,
  119. })
  120. expect(checklist).not.toHaveAttribute('disabled')
  121. })
  122. it('options for multiple checkboxes can be used', () => {
  123. const view = renderCheckbox({
  124. props: {
  125. label: 'Multiple Checkbox',
  126. name: 'checkbox-multiple',
  127. type: 'checkbox',
  128. id: 'checkbox-multiple',
  129. options: ['one', 'two', 'three'],
  130. },
  131. })
  132. const fieldset = view.getByRole('group', { name: /Multiple Checkbox/ })
  133. expect(fieldset).toBeInTheDocument()
  134. const inputs = getAllByRole(fieldset, 'checkbox')
  135. expect(inputs).toHaveLength(3)
  136. const node = getNode('checkbox-multiple')
  137. expect(node?.value).toStrictEqual([])
  138. })
  139. it('check for the multiple checkbox input event', async () => {
  140. const view = renderCheckbox({
  141. props: {
  142. label: 'Multiple Checkbox',
  143. name: 'checkbox-multiple',
  144. type: 'checkbox',
  145. id: 'checkbox-multiple',
  146. options: ['one', 'two', 'three'],
  147. },
  148. })
  149. await view.events.click(view.getByLabelText(/one/))
  150. await waitForTimeout()
  151. expect(view.emitted().input).toBeTruthy()
  152. const emittedInput = view.emitted().input as Array<Array<InputEvent>>
  153. expect(emittedInput[0][0]).toStrictEqual(['one'])
  154. await view.events.click(view.getByLabelText(/three/))
  155. await waitForTimeout()
  156. expect(emittedInput[1][0]).toStrictEqual(['one', 'three'])
  157. })
  158. })