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.

Checkbox-test.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 { click } from '../../../helpers/testUtils';
  23. import Checkbox from '../Checkbox';
  24. it('should render', () => {
  25. const checkbox = shallow(<Checkbox checked={true} onCheck={() => {}} title="Title value" />);
  26. expect(checkbox).toMatchSnapshot();
  27. });
  28. it('should render unchecked', () => {
  29. const checkbox = shallow(<Checkbox checked={false} onCheck={() => true} />);
  30. expect(checkbox.is('.icon-checkbox-checked')).toBe(false);
  31. expect(checkbox.prop('aria-checked')).toBe(false);
  32. });
  33. it('should render checked', () => {
  34. const checkbox = shallow(<Checkbox checked={true} onCheck={() => true} />);
  35. expect(checkbox.is('.icon-checkbox-checked')).toBe(true);
  36. expect(checkbox.prop('aria-checked')).toBe(true);
  37. });
  38. it('should render disabled', () => {
  39. const checkbox = shallow(<Checkbox checked={true} disabled={true} onCheck={() => true} />);
  40. expect(checkbox.is('.icon-checkbox-disabled')).toBe(true);
  41. });
  42. it('should render unchecked third state', () => {
  43. const checkbox = shallow(<Checkbox checked={false} onCheck={() => true} thirdState={true} />);
  44. expect(checkbox.is('.icon-checkbox-single')).toBe(true);
  45. expect(checkbox.is('.icon-checkbox-checked')).toBe(false);
  46. });
  47. it('should render checked third state', () => {
  48. const checkbox = shallow(<Checkbox checked={true} onCheck={() => true} thirdState={true} />);
  49. expect(checkbox.is('.icon-checkbox-single')).toBe(true);
  50. expect(checkbox.is('.icon-checkbox-checked')).toBe(true);
  51. });
  52. it('should render with a spinner', () => {
  53. const checkbox = shallow(<Checkbox checked={false} loading={true} onCheck={() => true} />);
  54. expect(checkbox.find('DeferredSpinner').exists()).toBe(true);
  55. });
  56. it('should render children', () => {
  57. const checkbox = shallow(
  58. <Checkbox checked={false} onCheck={() => true}>
  59. <span>foo</span>
  60. </Checkbox>
  61. );
  62. expect(checkbox.hasClass('link-checkbox')).toBe(true);
  63. expect(checkbox.find('span').exists()).toBe(true);
  64. });
  65. it('should render children with a spinner', () => {
  66. const checkbox = shallow(
  67. <Checkbox checked={false} loading={true} onCheck={() => true}>
  68. <span>foo</span>
  69. </Checkbox>
  70. );
  71. expect(checkbox.hasClass('link-checkbox')).toBe(true);
  72. expect(checkbox.find('span').exists()).toBe(true);
  73. expect(checkbox.find('DeferredSpinner').exists()).toBe(true);
  74. });
  75. it('should call onCheck', () => {
  76. const onCheck = jest.fn();
  77. const checkbox = shallow(<Checkbox checked={false} onCheck={onCheck} />);
  78. click(checkbox);
  79. expect(onCheck).toBeCalledWith(true, undefined);
  80. });
  81. it('should not call onCheck when disabled', () => {
  82. const onCheck = jest.fn();
  83. const checkbox = shallow(<Checkbox checked={false} disabled={true} onCheck={onCheck} />);
  84. click(checkbox);
  85. expect(onCheck).toHaveBeenCalledTimes(0);
  86. });
  87. it('should call onCheck with id as second parameter', () => {
  88. const onCheck = jest.fn();
  89. const checkbox = shallow(<Checkbox checked={false} id="foo" onCheck={onCheck} />);
  90. click(checkbox);
  91. expect(onCheck).toBeCalledWith(true, 'foo');
  92. });
  93. it('should apply custom class', () => {
  94. const checkbox = shallow(
  95. <Checkbox checked={true} className="customclass" onCheck={() => true} />
  96. );
  97. expect(checkbox.is('.customclass')).toBe(true);
  98. });
  99. it('should render the checkbox on the right', () => {
  100. expect(shallow(<Checkbox checked={true} onCheck={() => true} right={true} />)).toMatchSnapshot();
  101. });