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.

ListFooter-test.tsx 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { click } from '../../../helpers/testUtils';
  23. import { Button } from '../buttons';
  24. import ListFooter, { ListFooterProps } from '../ListFooter';
  25. it('should render correctly', () => {
  26. expect(shallowRender()).toMatchSnapshot('default');
  27. expect(shallowRender({ loading: true })).toMatchSnapshot('loading');
  28. expect(shallowRender({ needReload: true, reload: jest.fn() })).toMatchSnapshot('reload');
  29. expect(shallowRender({ loading: true, needReload: true, reload: jest.fn() })).toMatchSnapshot(
  30. 'reload, loading'
  31. );
  32. expect(shallowRender({ loadMore: undefined })).toMatchSnapshot(
  33. 'empty if no loadMore nor reload props'
  34. );
  35. expect(shallowRender({ count: 5 })).toMatchSnapshot('empty if everything is loaded');
  36. expect(shallowRender({ total: undefined })).toMatchSnapshot('total undefined');
  37. expect(shallowRender({ total: undefined, count: 60, pageSize: 30 })).toMatchSnapshot(
  38. 'force show load more button if count % pageSize is 0, and total is undefined'
  39. );
  40. });
  41. it.each([
  42. [undefined, 60, 30, true],
  43. [undefined, 45, 30, false],
  44. [undefined, 60, undefined, false],
  45. [60, 60, 30, false]
  46. ])(
  47. 'handle showing load more button based on total, count and pageSize',
  48. (total, count, pageSize, expected) => {
  49. const wrapper = shallowRender({ total, count, pageSize });
  50. expect(wrapper.find(Button).exists()).toBe(expected);
  51. }
  52. );
  53. it('should properly call loadMore', () => {
  54. const loadMore = jest.fn();
  55. const wrapper = shallowRender({ loadMore });
  56. click(wrapper.find(Button));
  57. expect(loadMore).toBeCalled();
  58. });
  59. it('should properly call reload', () => {
  60. const reload = jest.fn();
  61. const wrapper = shallowRender({ needReload: true, reload });
  62. click(wrapper.find(Button));
  63. expect(reload).toBeCalled();
  64. });
  65. function shallowRender(props: Partial<ListFooterProps> = {}) {
  66. return shallow<ListFooterProps>(
  67. <ListFooter count={3} loadMore={jest.fn()} total={5} {...props} />
  68. );
  69. }