|
| 1 | +import { mountFormGenerator, generatePropsSingleField, generateSchemaSingleField } from '@test/_resources/utils.js' |
| 2 | +import { it, describe, expect, beforeAll } from 'vitest' |
| 3 | +import { config, mount } from '@vue/test-utils' |
| 4 | + |
| 5 | +import FieldChecklist from '@/fields/core/FieldChecklist.vue' |
| 6 | + |
| 7 | +const form = generateSchemaSingleField( |
| 8 | + 'checklistTest', |
| 9 | + 'checklistModel', |
| 10 | + 'checklist', |
| 11 | + null, |
| 12 | + 'Test label', |
| 13 | + [], |
| 14 | + { |
| 15 | + options: [ |
| 16 | + { name: 'Test 1', value: 'test1' }, |
| 17 | + { name: 'Test 2', value: 'test2' }, |
| 18 | + { name: 'Test 3', value: 'test3' }, |
| 19 | + { name: 'Test 4', value: 'test4' } |
| 20 | + ] |
| 21 | + } |
| 22 | +) |
| 23 | + |
| 24 | +const props = generatePropsSingleField(form) |
| 25 | + |
| 26 | +beforeAll(() => { |
| 27 | + config.global.components = { FieldChecklist } |
| 28 | +}) |
| 29 | + |
| 30 | +describe('FieldChecklist', () => { |
| 31 | + |
| 32 | + it('Should render correctly', () => { |
| 33 | + const wrapper = mount(FieldChecklist, { props }) |
| 34 | + const checkBoxes = wrapper.findAll('input[type=checkbox]') |
| 35 | + expect(checkBoxes.length).toBe(4) |
| 36 | + |
| 37 | + const labels = wrapper.findAll('label') |
| 38 | + expect(labels.length).toBe(4) |
| 39 | + |
| 40 | + // Ensure each label has the checkbox that belongs to them |
| 41 | + labels.forEach((label, idx) => { |
| 42 | + expect(label.text()).toBe('Test ' + (idx + 1)) |
| 43 | + const checkbox = label.find('input[type=checkbox]') |
| 44 | + expect(checkbox.attributes().value).toBe('test' + (idx + 1)) |
| 45 | + }) |
| 46 | + |
| 47 | + }) |
| 48 | + |
| 49 | + it('Should render correctly inside form generator', () => { |
| 50 | + const formWrapper = mountFormGenerator(form.schema, form.model) |
| 51 | + // The first label should be the label that's specified inside the field's schema. |
| 52 | + expect(formWrapper.find('label').text()).toContain('Test label') |
| 53 | + |
| 54 | + const checklistField = formWrapper.findComponent(FieldChecklist) |
| 55 | + expect(checklistField.exists()).toBe(true) |
| 56 | + |
| 57 | + const labels = checklistField.findAll('label') |
| 58 | + expect(labels.length).toBe(4) |
| 59 | + }) |
| 60 | + |
| 61 | + it('Should emit onInput event', () => { |
| 62 | + const wrapper = mount(FieldChecklist, { props }) |
| 63 | + wrapper.find('input[type=checkbox]').trigger('change') |
| 64 | + expect(wrapper.emitted()).toHaveProperty('onInput') |
| 65 | + }) |
| 66 | + |
| 67 | + it('Should update model value', async () => { |
| 68 | + const formWrapper = mountFormGenerator(form.schema, form.model) |
| 69 | + const checklistField = formWrapper.findComponent(FieldChecklist) |
| 70 | + expect(checklistField.exists()).toBe(true) |
| 71 | + |
| 72 | + const triggerChange = (value) => formWrapper.find(`input[type=checkbox][value=${value}]`).trigger('change') |
| 73 | + |
| 74 | + triggerChange('test2') |
| 75 | + expect(formWrapper.vm.model.checklistModel).toContain('test2') |
| 76 | + triggerChange('test3') |
| 77 | + expect(formWrapper.vm.model.checklistModel).toContain('test3') |
| 78 | + |
| 79 | + // Value should be removed, since it is already present in the model |
| 80 | + triggerChange('test2') |
| 81 | + expect(formWrapper.vm.model.checklistModel).not.toContain('test2') |
| 82 | + }) |
| 83 | + |
| 84 | + it('Should update checked state properly', async () => { |
| 85 | + const formWrapper = mountFormGenerator(form.schema, form.model) |
| 86 | + const checklistField = formWrapper.findComponent(FieldChecklist) |
| 87 | + expect(checklistField.exists()).toBe(true) |
| 88 | + |
| 89 | + const test1Checkbox = () => formWrapper.find('input[type=checkbox][value=test1]') |
| 90 | + const test4Checkbox = () => formWrapper.find('input[type=checkbox][value=test4') |
| 91 | + |
| 92 | + // Value shouldn't be checked by default. |
| 93 | + expect(test1Checkbox().attributes()).not.toHaveProperty('checked') |
| 94 | + test1Checkbox().trigger('change') |
| 95 | + await checklistField.vm.$nextTick() |
| 96 | + expect(checklistField.find('input[type=checkbox][value=test1]').attributes()).toHaveProperty('checked') |
| 97 | + |
| 98 | + // Value shouldn't be checked by default. |
| 99 | + expect(test4Checkbox().attributes()).not.toHaveProperty('checked') |
| 100 | + test4Checkbox().trigger('change') |
| 101 | + await checklistField.vm.$nextTick() |
| 102 | + expect(checklistField.find('input[type=checkbox][value=test4]').attributes()).toHaveProperty('checked') |
| 103 | + }) |
| 104 | + |
| 105 | + it('Should render values present by default, as checked', async () => { |
| 106 | + const model = { checklistModel: [ 'test2', 'test4' ] } |
| 107 | + const formWrapper = mountFormGenerator(form.schema, model) |
| 108 | + |
| 109 | + // Values should be set inside the model of the form generator |
| 110 | + expect(formWrapper.vm.model.checklistModel).toContain('test2') |
| 111 | + expect(formWrapper.vm.model.checklistModel).toContain('test4') |
| 112 | + expect(formWrapper.vm.model.checklistModel).toHaveLength(2) |
| 113 | + |
| 114 | + const checklistField = formWrapper.findComponent(FieldChecklist) |
| 115 | + expect(checklistField.exists()).toBe(true) |
| 116 | + // These values should now be checked, since they are in the model by default |
| 117 | + expect(checklistField.find('input[type=checkbox][value=test2').attributes()).toHaveProperty('checked') |
| 118 | + expect(checklistField.find('input[type=checkbox][value=test4').attributes()).toHaveProperty('checked') |
| 119 | + // These shouldn't be checked, since they are not in the default values |
| 120 | + expect(checklistField.find('input[type=checkbox][value=test1').attributes()).not.toHaveProperty('checked') |
| 121 | + expect(checklistField.find('input[type=checkbox][value=test3').attributes()).not.toHaveProperty('checked') |
| 122 | + }) |
| 123 | + |
| 124 | +}) |
0 commit comments