ValueMappingsEditor.test.tsx 1.7 KB

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