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.

SelectList-test.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 { waitAndUpdate } from '../../../helpers/testUtils';
  23. import SelectList, { SelectListFilter } from '../SelectList';
  24. const elements = ['foo', 'bar', 'baz'];
  25. const selectedElements = [elements[0]];
  26. const disabledElements = [elements[1]];
  27. it('should display properly with basics features', async () => {
  28. const wrapper = shallowRender();
  29. await waitAndUpdate(wrapper);
  30. expect(wrapper.instance().mounted).toBe(true);
  31. expect(wrapper).toMatchSnapshot();
  32. wrapper.instance().componentWillUnmount();
  33. expect(wrapper.instance().mounted).toBe(false);
  34. });
  35. it('should display properly with advanced features', async () => {
  36. const wrapper = shallowRender({
  37. allowBulkSelection: true,
  38. elementsTotalCount: 125,
  39. pageSize: 10,
  40. readOnly: true,
  41. withPaging: true
  42. });
  43. await waitAndUpdate(wrapper);
  44. expect(wrapper).toMatchSnapshot();
  45. });
  46. it('should display a loader when searching', async () => {
  47. const wrapper = shallowRender();
  48. await waitAndUpdate(wrapper);
  49. expect(wrapper.state().loading).toBe(false);
  50. wrapper.instance().search({});
  51. expect(wrapper.state().loading).toBe(true);
  52. expect(wrapper).toMatchSnapshot();
  53. await waitAndUpdate(wrapper);
  54. expect(wrapper.state().loading).toBe(false);
  55. });
  56. it('should cancel filter selection when search is active', async () => {
  57. const spy = jest.fn().mockResolvedValue({});
  58. const wrapper = shallowRender({ onSearch: spy });
  59. wrapper.instance().changeFilter(SelectListFilter.Unselected);
  60. await waitAndUpdate(wrapper);
  61. expect(spy).toHaveBeenCalledWith({
  62. query: '',
  63. filter: SelectListFilter.Unselected,
  64. page: undefined,
  65. pageSize: undefined
  66. });
  67. expect(wrapper).toMatchSnapshot();
  68. const query = 'test';
  69. wrapper.instance().handleQueryChange(query);
  70. expect(spy).toHaveBeenCalledWith({
  71. query,
  72. filter: SelectListFilter.All,
  73. page: undefined,
  74. pageSize: undefined
  75. });
  76. await waitAndUpdate(wrapper);
  77. expect(wrapper).toMatchSnapshot();
  78. wrapper.instance().handleQueryChange('');
  79. expect(spy).toHaveBeenCalledWith({
  80. query: '',
  81. filter: SelectListFilter.Unselected,
  82. page: undefined,
  83. pageSize: undefined
  84. });
  85. await waitAndUpdate(wrapper);
  86. expect(wrapper).toMatchSnapshot();
  87. });
  88. it('should display pagination element properly and call search method with correct parameters', () => {
  89. const spy = jest.fn().mockResolvedValue({});
  90. const wrapper = shallowRender({ elementsTotalCount: 100, onSearch: spy, withPaging: true });
  91. expect(wrapper).toMatchSnapshot();
  92. expect(spy).toHaveBeenCalledWith({
  93. query: '',
  94. filter: SelectListFilter.Selected,
  95. page: 1,
  96. pageSize: 100
  97. }); // Basic default call
  98. wrapper.instance().onLoadMore();
  99. expect(spy).toHaveBeenCalledWith({
  100. query: '',
  101. filter: SelectListFilter.Selected,
  102. page: 2,
  103. pageSize: 100
  104. }); // Load more call
  105. wrapper.instance().onReload();
  106. expect(spy).toHaveBeenCalledWith({
  107. query: '',
  108. filter: SelectListFilter.Selected,
  109. page: 1,
  110. pageSize: 100
  111. }); // Reload call
  112. wrapper.setProps({ needToReload: true });
  113. expect(wrapper).toMatchSnapshot();
  114. });
  115. function shallowRender(props: Partial<SelectList['props']> = {}) {
  116. return shallow<SelectList>(
  117. <SelectList
  118. disabledElements={disabledElements}
  119. elements={elements}
  120. onSearch={jest.fn(() => Promise.resolve())}
  121. onSelect={jest.fn(() => Promise.resolve())}
  122. onUnselect={jest.fn(() => Promise.resolve())}
  123. renderElement={(foo: string) => foo}
  124. selectedElements={selectedElements}
  125. {...props}
  126. />
  127. );
  128. }