InfoTooltipWithTrigger.test.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import '@testing-library/jest-dom';
  20. import { fireEvent, render } from '@superset-ui/core/spec';
  21. import { InfoTooltip, InfoTooltipProps } from '@superset-ui/core/components';
  22. jest.mock('@superset-ui/core/components/Tooltip', () => ({
  23. Tooltip: ({ children }: { children: React.ReactNode }) => (
  24. <div data-test="mock-tooltip">{children}</div>
  25. ),
  26. }));
  27. const defaultProps = {};
  28. const setup = (props: Partial<InfoTooltipProps> = {}) =>
  29. render(<InfoTooltip {...defaultProps} {...props} />);
  30. test('renders a tooltip', () => {
  31. const { getAllByTestId } = setup({
  32. label: 'test',
  33. tooltip: 'this is a test',
  34. });
  35. expect(getAllByTestId('mock-tooltip').length).toEqual(1);
  36. });
  37. test('responds to keydown events', () => {
  38. const clickHandler = jest.fn();
  39. const { getByRole } = setup({
  40. label: 'test',
  41. tooltip: 'this is a test',
  42. onClick: clickHandler,
  43. });
  44. fireEvent.keyDown(getByRole('button'), {
  45. key: 'Tab',
  46. code: 9,
  47. charCode: 9,
  48. });
  49. expect(clickHandler).toHaveBeenCalledTimes(0);
  50. fireEvent.keyDown(getByRole('button'), {
  51. key: 'Enter',
  52. code: 13,
  53. charCode: 13,
  54. });
  55. expect(clickHandler).toHaveBeenCalledTimes(1);
  56. fireEvent.keyDown(getByRole('button'), {
  57. key: ' ',
  58. code: 32,
  59. charCode: 32,
  60. });
  61. expect(clickHandler).toHaveBeenCalledTimes(2);
  62. });
  63. test('finds the info circle icon inside info variant', () => {
  64. const { container } = setup({
  65. type: 'info',
  66. });
  67. const iconSpan = container.querySelector('svg[data-icon="info-circle"]');
  68. expect(iconSpan).toBeInTheDocument();
  69. });
  70. test('finds the warning icon inside warning variant', () => {
  71. const { container } = setup({
  72. type: 'warning',
  73. });
  74. const iconSpan = container.querySelector('svg[data-icon="warning"]');
  75. expect(iconSpan).toBeInTheDocument();
  76. });
  77. test('finds the close circle icon inside error variant', () => {
  78. const { container } = setup({
  79. type: 'error',
  80. });
  81. const iconSpan = container.querySelector('svg[data-icon="close-circle"]');
  82. expect(iconSpan).toBeInTheDocument();
  83. });
  84. test('finds the question circle icon inside question variant', () => {
  85. const { container } = setup({
  86. type: 'question',
  87. });
  88. const iconSpan = container.querySelector('svg[data-icon="question-circle"]');
  89. expect(iconSpan).toBeInTheDocument();
  90. });
  91. test('finds the thunderbolt icon inside notice variant', () => {
  92. const { container } = setup({
  93. type: 'notice',
  94. });
  95. const iconSpan = container.querySelector('svg[data-icon="thunderbolt"]');
  96. expect(iconSpan).toBeInTheDocument();
  97. });