|
| 1 | +import validate from '../src/required_if'; |
| 2 | + |
| 3 | +test('validates required_if with array params', () => { |
| 4 | + // target has a matching value, field is empty -> invalid |
| 5 | + expect(validate('', ['Online Banking', 'Online Banking'])).toBe(false); |
| 6 | + expect(validate(null, ['Online Banking', 'Online Banking'])).toBe(false); |
| 7 | + expect(validate(undefined, ['Online Banking', 'Online Banking'])).toBe(false); |
| 8 | + expect(validate([], ['Online Banking', 'Online Banking'])).toBe(false); |
| 9 | + |
| 10 | + // target has a matching value, field is filled -> valid |
| 11 | + expect(validate('Nabil', ['Online Banking', 'Online Banking'])).toBe(true); |
| 12 | + |
| 13 | + // target does not match any value, field is empty -> valid (not required) |
| 14 | + expect(validate('', ['Cash', 'Online Banking'])).toBe(true); |
| 15 | + expect(validate(null, ['Cash', 'Online Banking'])).toBe(true); |
| 16 | + expect(validate(undefined, ['Cash', 'Online Banking'])).toBe(true); |
| 17 | +}); |
| 18 | + |
| 19 | +test('validates required_if with object params', () => { |
| 20 | + // target matches, field is empty -> invalid |
| 21 | + expect(validate('', { target: 'Online Banking', values: ['Online Banking'] })).toBe(false); |
| 22 | + |
| 23 | + // target matches, field is filled -> valid |
| 24 | + expect(validate('Nabil', { target: 'Online Banking', values: ['Online Banking'] })).toBe(true); |
| 25 | + |
| 26 | + // target does not match, field is empty -> valid |
| 27 | + expect(validate('', { target: 'Cash', values: ['Online Banking'] })).toBe(true); |
| 28 | +}); |
| 29 | + |
| 30 | +test('validates required_if with multiple comparison values', () => { |
| 31 | + // target matches one of the values |
| 32 | + expect(validate('', ['Online Banking', 'Online Banking', 'Wire Transfer'])).toBe(false); |
| 33 | + expect(validate('', ['Wire Transfer', 'Online Banking', 'Wire Transfer'])).toBe(false); |
| 34 | + expect(validate('Nabil', ['Online Banking', 'Online Banking', 'Wire Transfer'])).toBe(true); |
| 35 | + |
| 36 | + // target matches none |
| 37 | + expect(validate('', ['Cash', 'Online Banking', 'Wire Transfer'])).toBe(true); |
| 38 | +}); |
| 39 | + |
| 40 | +test('validates required_if without comparison values (truthy target)', () => { |
| 41 | + // target is truthy, no comparison values -> field is required |
| 42 | + expect(validate('', ['some value'])).toBe(false); |
| 43 | + expect(validate('filled', ['some value'])).toBe(true); |
| 44 | + |
| 45 | + // target is empty/falsy, no comparison values -> field is not required |
| 46 | + expect(validate('', [''])).toBe(true); |
| 47 | + expect(validate('', [null])).toBe(true); |
| 48 | + expect(validate('', [undefined])).toBe(true); |
| 49 | +}); |
| 50 | + |
| 51 | +test('validates required_if with object params and no values array', () => { |
| 52 | + // target is truthy -> required |
| 53 | + expect(validate('', { target: 'truthy' })).toBe(false); |
| 54 | + expect(validate('filled', { target: 'truthy' })).toBe(true); |
| 55 | + |
| 56 | + // target is falsy -> not required |
| 57 | + expect(validate('', { target: '' })).toBe(true); |
| 58 | + expect(validate('', { target: null })).toBe(true); |
| 59 | + expect(validate('', { target: undefined })).toBe(true); |
| 60 | +}); |
| 61 | + |
| 62 | +test('uses loose equality for value comparison', () => { |
| 63 | + // '1' == 1 should be true with loose equality |
| 64 | + expect(validate('', ['1', 1])).toBe(false); |
| 65 | + expect(validate('', [1, '1'])).toBe(false); |
| 66 | +}); |
0 commit comments