|
| 1 | +import { ref, nextTick } from 'vue'; |
| 2 | + |
| 3 | +import { useFieldValidation } from './validation'; |
| 4 | + |
| 5 | +describe('validation composables', () => { |
| 6 | + describe('useFieldValidation', () => { |
| 7 | + let model; |
| 8 | + let rule; |
| 9 | + let rules; |
| 10 | + let instance; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + model = ref(''); |
| 14 | + rule = jest.fn().mockReturnValue(true); |
| 15 | + rules = [rule]; |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns the required properties', () => { |
| 19 | + const { isValid, errorMessages, errorMessagesString, validateField } = useFieldValidation( |
| 20 | + model, |
| 21 | + rules, |
| 22 | + ); |
| 23 | + |
| 24 | + expect(isValid.value).toEqual(true); |
| 25 | + expect(errorMessages.value).toEqual([]); |
| 26 | + expect(errorMessagesString.value).toEqual(''); |
| 27 | + expect(validateField).toEqual(expect.any(Function)); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('validateField function', () => { |
| 31 | + beforeEach(() => { |
| 32 | + instance = useFieldValidation(model, rules); |
| 33 | + }); |
| 34 | + |
| 35 | + it('validates the model value against the rules', () => { |
| 36 | + instance.validateField('foo bar baz'); |
| 37 | + |
| 38 | + expect(rule).toHaveBeenCalledWith('foo bar baz'); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('if the validation is successful', () => { |
| 42 | + beforeEach(() => { |
| 43 | + rule.mockReturnValue(true); |
| 44 | + |
| 45 | + instance.validateField('foo bar baz'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('sets isValid to true', () => { |
| 49 | + expect(instance.isValid.value).toEqual(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('sets errorMessages to an empty array', () => { |
| 53 | + expect(instance.errorMessages.value).toEqual([]); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('if the validation fails', () => { |
| 58 | + beforeEach(() => { |
| 59 | + rule.mockReturnValue('You failed miserably'); |
| 60 | + |
| 61 | + instance.validateField('foo bar baz'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('sets isValid to false', () => { |
| 65 | + expect(instance.isValid.value).toEqual(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it('sets errorMessages as an array of all failure messages', () => { |
| 69 | + expect(instance.errorMessages.value).toEqual(['You failed miserably']); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('when the model value changes', () => { |
| 75 | + beforeEach(() => { |
| 76 | + instance = useFieldValidation(model, rules); |
| 77 | + }); |
| 78 | + |
| 79 | + it('validates the model value against the rules', async () => { |
| 80 | + model.value = 'foo bar baz'; |
| 81 | + await nextTick(); |
| 82 | + |
| 83 | + expect(rule).toHaveBeenCalledWith('foo bar baz'); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('if the validation is successful', () => { |
| 87 | + beforeEach(async () => { |
| 88 | + rule.mockReturnValue(true); |
| 89 | + |
| 90 | + model.value = 'foo bar baz'; |
| 91 | + await nextTick(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('sets isValid to true', () => { |
| 95 | + expect(instance.isValid.value).toEqual(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sets errorMessages to an empty array', () => { |
| 99 | + expect(instance.errorMessages.value).toEqual([]); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('if the validation fails', () => { |
| 104 | + beforeEach(async () => { |
| 105 | + rule.mockReturnValue('You failed miserably'); |
| 106 | + |
| 107 | + model.value = 'foo bar baz'; |
| 108 | + await nextTick(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('sets isValid to false', () => { |
| 112 | + expect(instance.isValid.value).toEqual(false); |
| 113 | + }); |
| 114 | + |
| 115 | + it('sets errorMessages as an array of all failure messages', () => { |
| 116 | + expect(instance.errorMessages.value).toEqual(['You failed miserably']); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('errorMessagesString computed', () => { |
| 122 | + let instance; |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + instance = useFieldValidation(model, rules); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns an empty string if errorMessages is empty', async () => { |
| 129 | + instance.errorMessages.value = []; |
| 130 | + await nextTick(); |
| 131 | + |
| 132 | + expect(instance.errorMessagesString.value).toEqual(''); |
| 133 | + }); |
| 134 | + |
| 135 | + it('returns the joined messages in errorMessages otherwise', async () => { |
| 136 | + instance.errorMessages.value = ['Bad value', 'Big mistake here']; |
| 137 | + await nextTick(); |
| 138 | + |
| 139 | + expect(instance.errorMessagesString.value).toEqual('Bad value. Big mistake here.'); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments