extendDataAttributes.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type {
  3. FormKitExtendableSchemaRoot,
  4. FormKitFrameworkContext,
  5. } from '@formkit/core'
  6. import { createNode, getNode } from '@formkit/core'
  7. import { FormKit } from '@formkit/vue'
  8. import { renderComponent } from '#tests/support/components/index.ts'
  9. import { waitForNextTick } from '#tests/support/utils.ts'
  10. import extendDataAttribues from '../global/extendDataAttributes.ts'
  11. const wrapperParameters = {
  12. form: true,
  13. formField: true,
  14. }
  15. const renderKit = (props: any = {}) => {
  16. const kit = renderComponent(FormKit, {
  17. ...wrapperParameters,
  18. props: {
  19. name: 'text',
  20. type: 'text',
  21. id: 'text',
  22. label: 'text',
  23. ...props,
  24. },
  25. })
  26. return {
  27. ...kit,
  28. getOuterKit: () => kit.container.querySelector('.formkit-outer'),
  29. }
  30. }
  31. describe('extendDataAttributes - data-populated', () => {
  32. describe('renders on output', () => {
  33. const originalSchema = vi.fn()
  34. const inputNode = createNode({
  35. type: 'input',
  36. value: 'Test node',
  37. props: {
  38. definition: { type: 'input', schema: originalSchema },
  39. },
  40. })
  41. inputNode.context = {
  42. fns: {},
  43. } as FormKitFrameworkContext
  44. beforeEach(() => {
  45. originalSchema.mockReset()
  46. })
  47. it('applies schema on input', () => {
  48. extendDataAttribues(inputNode)
  49. const schema = (inputNode.props.definition?.schema ||
  50. (() => ({}))) as FormKitExtendableSchemaRoot
  51. schema({})
  52. expect(originalSchema.mock.calls[0][0]).toHaveProperty(
  53. 'outer.attrs.data-populated',
  54. )
  55. })
  56. it('skips non-inputs', () => {
  57. extendDataAttribues({
  58. ...inputNode,
  59. type: 'list',
  60. })
  61. expect(originalSchema).not.toHaveBeenCalled()
  62. })
  63. })
  64. describe('check output', () => {
  65. it('adds value populate data attribute', () => {
  66. const kit = renderKit()
  67. expect(kit.getOuterKit()).not.toHaveAttribute('data-populated')
  68. })
  69. it('is data attribute true when input has a value', async () => {
  70. const kit = renderKit()
  71. const input = kit.getByLabelText('text')
  72. expect(kit.getOuterKit()).not.toHaveAttribute('data-populated')
  73. await kit.events.type(input, 'input')
  74. expect(kit.getOuterKit()).toHaveAttribute('data-populated')
  75. await kit.events.clear(input)
  76. expect(kit.getOuterKit()).not.toHaveAttribute('data-populated')
  77. })
  78. it('is data attribute true when input has an initial value', async () => {
  79. const kit = renderKit({
  80. value: 'abc',
  81. })
  82. expect(kit.getOuterKit()).toHaveAttribute('data-populated')
  83. })
  84. it('is data attribute true for number input with zero', async () => {
  85. const kit = renderKit({
  86. name: 'number',
  87. type: 'number',
  88. id: 'number',
  89. label: 'number',
  90. })
  91. const node = getNode('number')
  92. node?.input(0)
  93. await waitForNextTick(true)
  94. expect(kit.getOuterKit()).toHaveAttribute('data-populated')
  95. })
  96. it('is data attribute true for a false boolean value', async () => {
  97. const kit = renderKit({
  98. name: 'checkbox',
  99. type: 'checkbox',
  100. id: 'checkbox',
  101. label: 'checkbox',
  102. })
  103. const node = getNode('checkbox')
  104. node?.input(false)
  105. await waitForNextTick(true)
  106. expect(kit.getOuterKit()).toHaveAttribute('data-populated')
  107. })
  108. })
  109. })
  110. describe('extendDataAttributes - data-required', () => {
  111. it('has data-required if field is required', () => {
  112. const kit = renderKit({
  113. required: true,
  114. })
  115. expect(kit.getOuterKit()).toHaveAttribute('data-required')
  116. })
  117. })