|
| 1 | +import React from 'react'; |
| 2 | +import FieldErrors from './field-errors'; |
| 3 | +import { render } from '../../test-utils'; |
| 4 | + |
| 5 | +describe('errorTypes', () => { |
| 6 | + it('should export errorTypes', () => { |
| 7 | + expect(FieldErrors.errorTypes.MISSING).toBe('missing'); |
| 8 | + expect(FieldErrors.errorTypes.NEGATIVE).toBe('negative'); |
| 9 | + expect(FieldErrors.errorTypes.FRACTIONS).toBe('fractions'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should render the "missing" error', () => { |
| 13 | + const { container } = render( |
| 14 | + <FieldErrors |
| 15 | + errors={{ [FieldErrors.errorTypes.MISSING]: true }} |
| 16 | + isVisible={true} |
| 17 | + /> |
| 18 | + ); |
| 19 | + expect(container).toHaveTextContent(/field is required/i); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should render the "negative" error', () => { |
| 23 | + const { container } = render( |
| 24 | + <FieldErrors |
| 25 | + errors={{ [FieldErrors.errorTypes.NEGATIVE]: true }} |
| 26 | + isVisible={true} |
| 27 | + /> |
| 28 | + ); |
| 29 | + expect(container).toHaveTextContent(/negative/i); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should render the "fractions" error', () => { |
| 33 | + const { container } = render( |
| 34 | + <FieldErrors |
| 35 | + errors={{ [FieldErrors.errorTypes.FRACTIONS]: true }} |
| 36 | + isVisible={true} |
| 37 | + /> |
| 38 | + ); |
| 39 | + expect(container).toHaveTextContent(/whole number/i); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('when not visible', () => { |
| 44 | + it('should render no errors', () => { |
| 45 | + const { container } = render( |
| 46 | + <FieldErrors |
| 47 | + errors={{ [FieldErrors.errorTypes.MISSING]: true }} |
| 48 | + isVisible={false} |
| 49 | + /> |
| 50 | + ); |
| 51 | + expect(container).toBeEmpty(); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('error renderers', () => { |
| 56 | + it('should give highest importance to renderError', () => { |
| 57 | + const { container } = render( |
| 58 | + <FieldErrors |
| 59 | + errors={{ [FieldErrors.errorTypes.MISSING]: true }} |
| 60 | + isVisible={true} |
| 61 | + renderError={key => |
| 62 | + key === FieldErrors.errorTypes.MISSING ? 'RENDER_ERROR' : null |
| 63 | + } |
| 64 | + renderDefaultError={key => |
| 65 | + key === FieldErrors.errorTypes.MISSING ? 'RENDER_DEFAULT_ERROR' : null |
| 66 | + } |
| 67 | + /> |
| 68 | + ); |
| 69 | + expect(container).toHaveTextContent('RENDER_ERROR'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should give second highest importance to renderDefaultError', () => { |
| 73 | + const { container } = render( |
| 74 | + <FieldErrors |
| 75 | + errors={{ [FieldErrors.errorTypes.MISSING]: true }} |
| 76 | + isVisible={true} |
| 77 | + renderError={() => null} |
| 78 | + renderDefaultError={key => |
| 79 | + key === FieldErrors.errorTypes.MISSING ? 'RENDER_DEFAULT_ERROR' : null |
| 80 | + } |
| 81 | + /> |
| 82 | + ); |
| 83 | + expect(container).toHaveTextContent('RENDER_DEFAULT_ERROR'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should fall back to internal error handling', () => { |
| 87 | + const { container } = render( |
| 88 | + <FieldErrors |
| 89 | + errors={{ [FieldErrors.errorTypes.MISSING]: true }} |
| 90 | + isVisible={true} |
| 91 | + renderError={() => null} |
| 92 | + renderDefaultError={() => null} |
| 93 | + /> |
| 94 | + ); |
| 95 | + expect(container).toHaveTextContent(/required/i); |
| 96 | + }); |
| 97 | +}); |
0 commit comments