|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { useFormKitSchema } from '../../src/runtime/composables/useFormKitSchema' |
| 3 | + |
| 4 | +interface FormKitComponent { |
| 5 | + $cmp?: string |
| 6 | + props?: Record<string, unknown> |
| 7 | + validation?: string |
| 8 | + validationVisibility?: string |
| 9 | + validationLabel?: string |
| 10 | + if?: string |
| 11 | + [key: string]: unknown |
| 12 | +} |
| 13 | + |
| 14 | +interface FormKitElement { |
| 15 | + $el: string |
| 16 | + attrs?: { |
| 17 | + class?: string |
| 18 | + style?: string |
| 19 | + [key: string]: unknown |
| 20 | + } |
| 21 | + children?: unknown[] | string |
| 22 | + validation?: string |
| 23 | + validationVisibility?: string |
| 24 | + if?: string |
| 25 | +} |
| 26 | + |
| 27 | +interface FormKitGroup { |
| 28 | + $formkit: string |
| 29 | + name?: string |
| 30 | + children?: object[] |
| 31 | + if?: string |
| 32 | + for?: string[] |
| 33 | + key?: string |
| 34 | + index?: string |
| 35 | + [key: string]: unknown |
| 36 | +} |
| 37 | + |
| 38 | +interface FormKitList { |
| 39 | + $formkit: string |
| 40 | + name: string |
| 41 | + dynamic?: boolean |
| 42 | + children?: object[] |
| 43 | + if?: string |
| 44 | + [key: string]: unknown |
| 45 | +} |
| 46 | + |
| 47 | +it('add list group', () => { |
| 48 | + const { addListGroup } = useFormKitSchema() |
| 49 | + |
| 50 | + const listGroup = addListGroup() |
| 51 | + expect(listGroup?.$formkit).toBe('group') |
| 52 | + expect(listGroup?.if).toBe('true') |
| 53 | + expect(listGroup?.for).toEqual(['item', 'index', '$items']) |
| 54 | + expect(listGroup?.key).toBe('$item') |
| 55 | + expect(listGroup?.index).toBe('$index') |
| 56 | +}) |
| 57 | + |
| 58 | +it('add element with non-boolean render value', () => { |
| 59 | + const { addElement } = useFormKitSchema() |
| 60 | + const element = addElement('div', [], {}, '$context.visible') |
| 61 | + expect((element as FormKitElement)?.if).toBe('$context.visible') |
| 62 | +}) |
| 63 | + |
| 64 | +it('add component with props', () => { |
| 65 | + const { addComponent } = useFormKitSchema() |
| 66 | + const props = { label: 'Save', severity: 'primary' } |
| 67 | + const component = addComponent('Button', props) as FormKitComponent |
| 68 | + expect(component?.props).toEqual(props) |
| 69 | +}) |
| 70 | + |
| 71 | +describe('addElementsInOuterDiv', () => { |
| 72 | + it('creates default structure with minimal params', () => { |
| 73 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 74 | + const outerDiv = addElementsInOuterDiv() as FormKitElement |
| 75 | + |
| 76 | + expect(outerDiv?.$el).toBe('div') |
| 77 | + expect(outerDiv?.attrs?.class).toBe('formkit-outer ') |
| 78 | + |
| 79 | + const wrapperDiv = (outerDiv?.children as FormKitElement[])?.[0] |
| 80 | + expect(wrapperDiv?.$el).toBe('div') |
| 81 | + expect(wrapperDiv?.attrs?.class).toBe('formkit-wrapper') |
| 82 | + |
| 83 | + const labelDiv = (wrapperDiv?.children as FormKitElement[])?.[0] |
| 84 | + expect(labelDiv?.$el).toBe('label') |
| 85 | + expect(labelDiv?.children).toEqual(['']) |
| 86 | + |
| 87 | + const innerDiv = (wrapperDiv?.children as FormKitElement[])?.[1] |
| 88 | + expect(innerDiv?.$el).toBe('div') |
| 89 | + expect(innerDiv?.attrs?.class).toBe('formkit-inner ') |
| 90 | + }) |
| 91 | + |
| 92 | + it('applies custom classes', () => { |
| 93 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 94 | + const outerDiv = addElementsInOuterDiv([], 'custom-inner', 'custom-outer') as FormKitElement |
| 95 | + |
| 96 | + expect(outerDiv?.attrs?.class).toBe('formkit-outer custom-outer') |
| 97 | + |
| 98 | + const wrapperDiv = (outerDiv?.children as FormKitElement[])?.[0] |
| 99 | + const innerDiv = (wrapperDiv?.children as FormKitElement[])?.[1] |
| 100 | + expect(innerDiv?.attrs?.class).toBe('formkit-inner custom-inner') |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +it('combines formKitAttrs with element properties', () => { |
| 105 | + const { addElement } = useFormKitSchema() |
| 106 | + const formKitAttrs = { validation: 'required', validationVisibility: 'dirty' } |
| 107 | + const element = addElement('div', [], {}, true, formKitAttrs) as FormKitElement |
| 108 | + |
| 109 | + expect(element?.$el).toBe('div') |
| 110 | + expect(element?.validation).toBe('required') |
| 111 | + expect(element?.validationVisibility).toBe('dirty') |
| 112 | +}) |
| 113 | + |
| 114 | +it('combines formKitAttrs with component properties', () => { |
| 115 | + const { addComponent } = useFormKitSchema() |
| 116 | + const formKitAttrs = { validation: 'required', validationLabel: 'Button' } |
| 117 | + const component = addComponent('Button', {}, true, formKitAttrs) as FormKitComponent |
| 118 | + |
| 119 | + expect(component?.$cmp).toBe('Button') |
| 120 | + expect(component?.validation).toBe('required') |
| 121 | + expect(component?.validationLabel).toBe('Button') |
| 122 | +}) |
| 123 | + |
| 124 | +describe('addGroup', () => { |
| 125 | + it('creates a group with default values', () => { |
| 126 | + const { addGroup } = useFormKitSchema() |
| 127 | + const group = addGroup('myGroup') as FormKitGroup |
| 128 | + |
| 129 | + expect(group?.$formkit).toBe('group') |
| 130 | + expect(group?.name).toBe('myGroup') |
| 131 | + expect(group?.children).toEqual([]) |
| 132 | + expect(group?.if).toBe('true') |
| 133 | + }) |
| 134 | + |
| 135 | + it('creates a group with children', () => { |
| 136 | + const { addGroup, addElement } = useFormKitSchema() |
| 137 | + const child = addElement('div', ['test']) |
| 138 | + const group = addGroup('myGroup', [child]) as FormKitGroup |
| 139 | + |
| 140 | + expect(group?.$formkit).toBe('group') |
| 141 | + expect(group?.name).toBe('myGroup') |
| 142 | + expect(group?.children).toHaveLength(1) |
| 143 | + expect(group?.children?.[0]).toBe(child) |
| 144 | + }) |
| 145 | + |
| 146 | + it('creates a group with custom render condition', () => { |
| 147 | + const { addGroup } = useFormKitSchema() |
| 148 | + const group = addGroup('myGroup', [], '$value.enabled') as FormKitGroup |
| 149 | + |
| 150 | + expect(group?.if).toBe('$value.enabled') |
| 151 | + }) |
| 152 | + |
| 153 | + it('creates a group with formKitAttrs', () => { |
| 154 | + const { addGroup } = useFormKitSchema() |
| 155 | + const formKitAttrs = { validation: 'required', id: 'custom-id' } |
| 156 | + const group = addGroup('myGroup', [], true, formKitAttrs) as FormKitGroup |
| 157 | + |
| 158 | + expect(group?.validation).toBe('required') |
| 159 | + expect(group?.id).toBe('custom-id') |
| 160 | + }) |
| 161 | + |
| 162 | + it('creates a group with render=false', () => { |
| 163 | + const { addGroup } = useFormKitSchema() |
| 164 | + const group = addGroup('myGroup', [], false) as FormKitGroup |
| 165 | + |
| 166 | + expect(group?.if).toBe('false') |
| 167 | + }) |
| 168 | +}) |
| 169 | + |
| 170 | +describe('addList', () => { |
| 171 | + it('creates a list with default values', () => { |
| 172 | + const { addList } = useFormKitSchema() |
| 173 | + const list = addList('myList') as FormKitList |
| 174 | + |
| 175 | + expect(list?.$formkit).toBe('list') |
| 176 | + expect(list?.name).toBe('myList') |
| 177 | + expect(list?.children).toEqual([]) |
| 178 | + expect(list?.dynamic).toBe(true) |
| 179 | + expect(list?.if).toBe('true') |
| 180 | + }) |
| 181 | + |
| 182 | + it('creates a list with children', () => { |
| 183 | + const { addList, addElement } = useFormKitSchema() |
| 184 | + const child = addElement('div', ['test']) |
| 185 | + const list = addList('myList', [child]) as FormKitList |
| 186 | + |
| 187 | + expect(list?.$formkit).toBe('list') |
| 188 | + expect(list?.name).toBe('myList') |
| 189 | + expect(list?.children).toHaveLength(1) |
| 190 | + expect(list?.children?.[0]).toBe(child) |
| 191 | + }) |
| 192 | + |
| 193 | + it('creates a static list', () => { |
| 194 | + const { addList } = useFormKitSchema() |
| 195 | + const list = addList('myList', [], false) as FormKitList |
| 196 | + |
| 197 | + expect(list?.dynamic).toBe(false) |
| 198 | + }) |
| 199 | + |
| 200 | + it('creates a list with custom render condition', () => { |
| 201 | + const { addList } = useFormKitSchema() |
| 202 | + const list = addList('myList', [], true, '$value.showList') as FormKitList |
| 203 | + |
| 204 | + expect(list?.if).toBe('$value.showList') |
| 205 | + }) |
| 206 | + |
| 207 | + it('creates a list with formKitAttrs', () => { |
| 208 | + const { addList } = useFormKitSchema() |
| 209 | + const formKitAttrs = { validation: 'min:1', help: 'Add at least one item' } |
| 210 | + const list = addList('myList', [], true, true, formKitAttrs) as FormKitList |
| 211 | + |
| 212 | + expect(list?.validation).toBe('min:1') |
| 213 | + expect(list?.help).toBe('Add at least one item') |
| 214 | + }) |
| 215 | + |
| 216 | + it('creates a list with render=false', () => { |
| 217 | + const { addList } = useFormKitSchema() |
| 218 | + const list = addList('myList', [], true, false) as FormKitList |
| 219 | + |
| 220 | + expect(list?.if).toBe('false') |
| 221 | + }) |
| 222 | +}) |
| 223 | + |
| 224 | +describe('addComponent', () => { |
| 225 | + it('creates a component with default values', () => { |
| 226 | + const { addComponent } = useFormKitSchema() |
| 227 | + const component = addComponent() as FormKitComponent |
| 228 | + |
| 229 | + expect(component?.$cmp).toBe('UButton') |
| 230 | + expect(component?.props).toEqual({}) |
| 231 | + expect(component?.if).toBe('true') |
| 232 | + }) |
| 233 | + |
| 234 | + it('creates a component with render=false', () => { |
| 235 | + const { addComponent } = useFormKitSchema() |
| 236 | + const component = addComponent('Button', {}, false) as FormKitComponent |
| 237 | + |
| 238 | + expect(component?.if).toBe('false') |
| 239 | + }) |
| 240 | +}) |
| 241 | + |
| 242 | +describe('addElement', () => { |
| 243 | + it('creates an element with default values', () => { |
| 244 | + const { addElement } = useFormKitSchema() |
| 245 | + const element = addElement() as FormKitElement |
| 246 | + |
| 247 | + expect(element?.$el).toBe('div') |
| 248 | + expect(element?.children).toEqual([]) |
| 249 | + expect(element?.attrs).toEqual({}) |
| 250 | + expect(element?.if).toBe('true') |
| 251 | + }) |
| 252 | + |
| 253 | + it('creates an element with string children', () => { |
| 254 | + const { addElement } = useFormKitSchema() |
| 255 | + const element = addElement('span', 'Hello World') as FormKitElement |
| 256 | + |
| 257 | + expect(element?.$el).toBe('span') |
| 258 | + expect(element?.children).toBe('Hello World') |
| 259 | + }) |
| 260 | + |
| 261 | + it('creates an element with render=false', () => { |
| 262 | + const { addElement } = useFormKitSchema() |
| 263 | + const element = addElement('div', [], {}, false) as FormKitElement |
| 264 | + |
| 265 | + expect(element?.if).toBe('false') |
| 266 | + }) |
| 267 | + |
| 268 | + it('creates an element with custom attrs', () => { |
| 269 | + const { addElement } = useFormKitSchema() |
| 270 | + const attrs = { 'class': 'custom-class', 'id': 'test-id', 'data-test': 'value' } |
| 271 | + const element = addElement('div', [], attrs) as FormKitElement |
| 272 | + |
| 273 | + expect(element?.attrs).toEqual(attrs) |
| 274 | + }) |
| 275 | +}) |
| 276 | + |
| 277 | +describe('addElementsInOuterDiv', () => { |
| 278 | + it('creates structure with label and help text', () => { |
| 279 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 280 | + const outerDiv = addElementsInOuterDiv([], '', '', 'My Label', 'Help text') as FormKitElement |
| 281 | + |
| 282 | + const wrapperDiv = (outerDiv?.children as FormKitElement[])?.[0] |
| 283 | + const labelDiv = (wrapperDiv?.children as FormKitElement[])?.[0] |
| 284 | + expect(labelDiv?.children).toEqual(['My Label']) |
| 285 | + |
| 286 | + const helpDiv = (outerDiv?.children as FormKitElement[])?.[1] |
| 287 | + expect(helpDiv?.attrs?.class).toBe('formkit-help') |
| 288 | + expect(helpDiv?.children).toEqual(['Help text']) |
| 289 | + }) |
| 290 | + |
| 291 | + it('creates structure with custom render condition', () => { |
| 292 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 293 | + const outerDiv = addElementsInOuterDiv([], '', '', '', '', '$value.visible') as FormKitElement |
| 294 | + |
| 295 | + expect(outerDiv?.if).toBe('$value.visible') |
| 296 | + }) |
| 297 | + |
| 298 | + it('includes inner div with position relative style', () => { |
| 299 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 300 | + const outerDiv = addElementsInOuterDiv() as FormKitElement |
| 301 | + |
| 302 | + const wrapperDiv = (outerDiv?.children as FormKitElement[])?.[0] |
| 303 | + const innerDiv = (wrapperDiv?.children as FormKitElement[])?.[1] |
| 304 | + expect(innerDiv?.attrs?.style).toContain('position: relative') |
| 305 | + }) |
| 306 | + |
| 307 | + it('includes outer div with position relative style', () => { |
| 308 | + const { addElementsInOuterDiv } = useFormKitSchema() |
| 309 | + const outerDiv = addElementsInOuterDiv() as FormKitElement |
| 310 | + |
| 311 | + expect(outerDiv?.attrs?.style).toContain('position: relative') |
| 312 | + }) |
| 313 | +}) |
0 commit comments