Input.test.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react';
  2. import renderer from 'react-test-renderer';
  3. import { shallow } from 'enzyme';
  4. import { Input, EventsWithValidation } from './Input';
  5. import { ValidationEvents } from 'app/types';
  6. const TEST_ERROR_MESSAGE = 'Value must be empty or less than 3 chars';
  7. const testBlurValidation: ValidationEvents = {
  8. [EventsWithValidation.onBlur]: [
  9. {
  10. rule: (value: string) => {
  11. if (!value || value.length < 3) {
  12. return true;
  13. }
  14. return false;
  15. },
  16. errorMessage: TEST_ERROR_MESSAGE,
  17. },
  18. ],
  19. };
  20. describe('Input', () => {
  21. it('renders correctly', () => {
  22. const tree = renderer.create(<Input />).toJSON();
  23. expect(tree).toMatchSnapshot();
  24. });
  25. it('should validate with error onBlur', () => {
  26. const wrapper = shallow(<Input validationEvents={testBlurValidation} />);
  27. const evt = {
  28. persist: jest.fn,
  29. target: {
  30. value: 'I can not be more than 2 chars',
  31. },
  32. };
  33. wrapper.find('input').simulate('blur', evt);
  34. expect(wrapper.state('error')).toBe(TEST_ERROR_MESSAGE);
  35. });
  36. it('should validate without error onBlur', () => {
  37. const wrapper = shallow(<Input validationEvents={testBlurValidation} />);
  38. const evt = {
  39. persist: jest.fn,
  40. target: {
  41. value: 'Hi',
  42. },
  43. };
  44. wrapper.find('input').simulate('blur', evt);
  45. expect(wrapper.state('error')).toBe(null);
  46. });
  47. });