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.

Definition-test.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 } from 'enzyme';
  21. import * as React from 'react';
  22. import { mockSetting } from '../../../../helpers/mocks/settings';
  23. import { waitAndUpdate } from '../../../../helpers/testUtils';
  24. import { Definition } from '../Definition';
  25. import DefinitionActions from '../DefinitionActions';
  26. import Input from '../inputs/Input';
  27. const setting = mockSetting();
  28. beforeAll(() => {
  29. jest.useFakeTimers();
  30. });
  31. afterAll(() => {
  32. jest.useRealTimers();
  33. });
  34. it('should render correctly', () => {
  35. const wrapper = shallowRender();
  36. expect(wrapper).toMatchSnapshot();
  37. });
  38. it('should correctly handle change of value', () => {
  39. const changeValue = jest.fn();
  40. const checkValue = jest.fn();
  41. const wrapper = shallowRender({ changeValue, checkValue });
  42. wrapper.find(Input).prop<Function>('onChange')(5);
  43. expect(changeValue).toHaveBeenCalledWith(setting.definition.key, 5);
  44. expect(checkValue).toHaveBeenCalledWith(setting.definition.key);
  45. });
  46. it('should correctly cancel value change', () => {
  47. const cancelChange = jest.fn();
  48. const passValidation = jest.fn();
  49. const wrapper = shallowRender({ cancelChange, passValidation });
  50. wrapper.find(Input).prop<Function>('onCancel')();
  51. expect(cancelChange).toHaveBeenCalledWith(setting.definition.key);
  52. expect(passValidation).toHaveBeenCalledWith(setting.definition.key);
  53. });
  54. it('should correctly save value change', async () => {
  55. const saveValue = jest.fn().mockResolvedValue({});
  56. const wrapper = shallowRender({ changedValue: 10, saveValue });
  57. wrapper.find(DefinitionActions).prop('onSave')();
  58. await waitAndUpdate(wrapper);
  59. expect(saveValue).toHaveBeenCalledWith(setting.definition.key, undefined);
  60. expect(wrapper.find('AlertSuccessIcon').exists()).toBe(true);
  61. expect(wrapper.state().success).toBe(true);
  62. jest.runAllTimers();
  63. expect(wrapper.state().success).toBe(false);
  64. });
  65. it('should correctly reset', async () => {
  66. const cancelChange = jest.fn();
  67. const resetValue = jest.fn().mockResolvedValue({});
  68. const wrapper = shallowRender({ cancelChange, changedValue: 10, resetValue });
  69. wrapper.find(DefinitionActions).prop('onReset')();
  70. await waitAndUpdate(wrapper);
  71. expect(resetValue).toHaveBeenCalledWith(setting.definition.key, undefined);
  72. expect(cancelChange).toHaveBeenCalledWith(setting.definition.key);
  73. expect(wrapper.state().success).toBe(true);
  74. jest.runAllTimers();
  75. expect(wrapper.state().success).toBe(false);
  76. });
  77. function shallowRender(props: Partial<Definition['props']> = {}) {
  78. return shallow<Definition>(
  79. <Definition
  80. cancelChange={jest.fn()}
  81. changeValue={jest.fn()}
  82. changedValue={null}
  83. checkValue={jest.fn()}
  84. loading={false}
  85. passValidation={jest.fn()}
  86. resetValue={jest.fn().mockResolvedValue({})}
  87. saveValue={jest.fn().mockResolvedValue({})}
  88. setting={setting}
  89. {...props}
  90. />
  91. );
  92. }