You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiValueInput-test.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { shallow, ShallowWrapper } from 'enzyme';
  21. import * as React from 'react';
  22. import { click } from '../../../../../helpers/testUtils';
  23. import { SettingCategoryDefinition, SettingType } from '../../../../../types/settings';
  24. import { DefaultSpecializedInputProps } from '../../../utils';
  25. import MultiValueInput from '../MultiValueInput';
  26. import PrimitiveInput from '../PrimitiveInput';
  27. const settingValue = {
  28. key: 'example',
  29. hasValue: true
  30. };
  31. const settingDefinition: SettingCategoryDefinition = {
  32. category: 'general',
  33. fields: [],
  34. key: 'example',
  35. multiValues: true,
  36. options: [],
  37. subCategory: 'Branches',
  38. type: SettingType.STRING
  39. };
  40. const assertValues = (inputs: ShallowWrapper<any>, values: string[]) => {
  41. values.forEach((value, index) => {
  42. const input = inputs.at(index);
  43. expect(input.prop('value')).toBe(value);
  44. });
  45. };
  46. it('should render one value', () => {
  47. const multiValueInput = shallowRender();
  48. const stringInputs = multiValueInput.find(PrimitiveInput);
  49. expect(stringInputs.length).toBe(1 + 1);
  50. assertValues(stringInputs, ['foo', '']);
  51. });
  52. it('should render several values', () => {
  53. const multiValueInput = shallowRender({ value: ['foo', 'bar', 'baz'] });
  54. const stringInputs = multiValueInput.find(PrimitiveInput);
  55. expect(stringInputs.length).toBe(3 + 1);
  56. assertValues(stringInputs, ['foo', 'bar', 'baz', '']);
  57. });
  58. it('should remove value', () => {
  59. const onChange = jest.fn();
  60. const multiValueInput = shallowRender({ onChange, value: ['foo', 'bar', 'baz'] });
  61. click(multiValueInput.find('.js-remove-value').at(1));
  62. expect(onChange).toBeCalledWith(['foo', 'baz']);
  63. });
  64. it('should change existing value', () => {
  65. const onChange = jest.fn();
  66. const multiValueInput = shallowRender({ onChange, value: ['foo', 'bar', 'baz'] });
  67. multiValueInput
  68. .find(PrimitiveInput)
  69. .at(1)
  70. .prop('onChange')('qux');
  71. expect(onChange).toBeCalledWith(['foo', 'qux', 'baz']);
  72. });
  73. it('should add new value', () => {
  74. const onChange = jest.fn();
  75. const multiValueInput = shallowRender({ onChange });
  76. multiValueInput
  77. .find(PrimitiveInput)
  78. .at(1)
  79. .prop('onChange')('bar');
  80. expect(onChange).toBeCalledWith(['foo', 'bar']);
  81. });
  82. function shallowRender(props: Partial<DefaultSpecializedInputProps> = {}) {
  83. return shallow(
  84. <MultiValueInput
  85. isDefault={true}
  86. name="bar"
  87. onChange={jest.fn()}
  88. setting={{ ...settingValue, definition: settingDefinition }}
  89. value={['foo']}
  90. {...props}
  91. />
  92. );
  93. }