ValueMappings.test.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import ValueMappings from './ValueMappings';
  4. import { defaultProps, OptionModuleProps } from './module';
  5. import { MappingType } from 'app/types';
  6. const setup = (propOverrides?: object) => {
  7. const props: OptionModuleProps = {
  8. onChange: jest.fn(),
  9. options: {
  10. ...defaultProps.options,
  11. mappings: [
  12. { id: 1, operator: '', type: MappingType.ValueToText, value: '20', text: 'Ok' },
  13. { id: 2, operator: '', type: MappingType.RangeToText, from: '21', to: '30', text: 'Meh' },
  14. ],
  15. },
  16. };
  17. Object.assign(props, propOverrides);
  18. const wrapper = shallow(<ValueMappings {...props} />);
  19. const instance = wrapper.instance() as ValueMappings;
  20. return {
  21. instance,
  22. wrapper,
  23. };
  24. };
  25. describe('Render', () => {
  26. it('should render component', () => {
  27. const { wrapper } = setup();
  28. expect(wrapper).toMatchSnapshot();
  29. });
  30. });
  31. describe('On remove mapping', () => {
  32. it('Should remove mapping with id 0', () => {
  33. const { instance } = setup();
  34. instance.onRemoveMapping(1);
  35. expect(instance.state.mappings).toEqual([
  36. { id: 2, operator: '', type: MappingType.RangeToText, from: '21', to: '30', text: 'Meh' },
  37. ]);
  38. });
  39. it('should remove mapping with id 1', () => {
  40. const { instance } = setup();
  41. instance.onRemoveMapping(2);
  42. expect(instance.state.mappings).toEqual([
  43. { id: 1, operator: '', type: MappingType.ValueToText, value: '20', text: 'Ok' },
  44. ]);
  45. });
  46. });
  47. describe('Next id to add', () => {
  48. it('should be 4', () => {
  49. const { instance } = setup();
  50. instance.addMapping();
  51. expect(instance.state.nextIdToAdd).toEqual(4);
  52. });
  53. it('should default to 1', () => {
  54. const { instance } = setup({ options: { ...defaultProps.options } });
  55. expect(instance.state.nextIdToAdd).toEqual(1);
  56. });
  57. });