|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, waitForDomChange } from '@testing-library/react'; |
| 3 | + |
| 4 | +import Tooltip from './Tooltip'; |
| 5 | + |
| 6 | +const getProps = (props = {}) => ({ |
| 7 | + className: props.className, |
| 8 | + disableFocusListener: props.disableFocusListener, |
| 9 | + disableMouseListener: props.disableMouseListener, |
| 10 | + enterDelay: props.enterDelay !== undefined ? props.enterDelay : 0, |
| 11 | + leaveDelay: props.leaveDelay !== undefined ? props.leaveDelay : 0, |
| 12 | + onBlur: jest.fn(), |
| 13 | + onClose: jest.fn(), |
| 14 | + onFocus: jest.fn(), |
| 15 | + onMouseEnter: jest.fn(), |
| 16 | + onMouseLeave: jest.fn(), |
| 17 | + onOpen: jest.fn(), |
| 18 | + style: props.style, |
| 19 | + testId: 'tip', |
| 20 | + text: 'I am the tooltip' |
| 21 | +}); |
| 22 | + |
| 23 | +const renderTooltip = props => ( |
| 24 | + <Tooltip {...props}> |
| 25 | + <div>Kid</div> |
| 26 | + </Tooltip> |
| 27 | +); |
| 28 | + |
| 29 | +describe('<Tooltip />', () => { |
| 30 | + describe('render', () => { |
| 31 | + it('should render wrapper element', () => { |
| 32 | + const { getByTestId } = render(renderTooltip(getProps())); |
| 33 | + |
| 34 | + const tipWrapper = getByTestId('tipWrapper'); |
| 35 | + |
| 36 | + expect(tipWrapper).toBeInTheDocument(); |
| 37 | + expect(tipWrapper.tagName).toBe('DIV'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should render inner tooltip', () => { |
| 41 | + const { getByTestId } = render(renderTooltip(getProps())); |
| 42 | + |
| 43 | + const tip = getByTestId('tip'); |
| 44 | + |
| 45 | + expect(tip).toBeInTheDocument(); |
| 46 | + expect(tip.tagName).toBe('SPAN'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should render children', () => { |
| 50 | + const { getByText } = render(renderTooltip(getProps())); |
| 51 | + |
| 52 | + const children = getByText('Kid'); |
| 53 | + |
| 54 | + expect(children).toBeInTheDocument(); |
| 55 | + expect(children.tagName).toBe('DIV'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should render tooltip with provided className', () => { |
| 59 | + const { getByTestId } = render( |
| 60 | + renderTooltip( |
| 61 | + getProps({ |
| 62 | + className: 'my-tip' |
| 63 | + }) |
| 64 | + ) |
| 65 | + ); |
| 66 | + |
| 67 | + const tip = getByTestId('tip'); |
| 68 | + |
| 69 | + expect(tip.className.includes('my-tip')).toBeTruthy(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('transition delays', () => { |
| 74 | + beforeEach(() => { |
| 75 | + jest.useFakeTimers(); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + jest.useRealTimers(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should respect enterDelay', async () => { |
| 83 | + const { getByTestId } = render( |
| 84 | + renderTooltip( |
| 85 | + getProps({ |
| 86 | + enterDelay: 5 |
| 87 | + }) |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + const tipWrapper = getByTestId('tipWrapper'); |
| 92 | + |
| 93 | + fireEvent.focus(tipWrapper); |
| 94 | + |
| 95 | + expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 5); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should respect leaveDelay', async () => { |
| 99 | + const { getByTestId } = render( |
| 100 | + renderTooltip( |
| 101 | + getProps({ |
| 102 | + leaveDelay: 6 |
| 103 | + }) |
| 104 | + ) |
| 105 | + ); |
| 106 | + |
| 107 | + const tipWrapper = getByTestId('tipWrapper'); |
| 108 | + |
| 109 | + fireEvent.blur(tipWrapper); |
| 110 | + |
| 111 | + expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 6); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('event callbacks', () => { |
| 116 | + it('should handle onFocus events, and call onOpen', async () => { |
| 117 | + const props = getProps(); |
| 118 | + |
| 119 | + const { getByTestId } = render(renderTooltip(props)); |
| 120 | + |
| 121 | + const tip = getByTestId('tip'); |
| 122 | + const tipWrapper = getByTestId('tipWrapper'); |
| 123 | + |
| 124 | + fireEvent.focus(tipWrapper); |
| 125 | + |
| 126 | + await waitForDomChange({ container: tip }); |
| 127 | + |
| 128 | + expect(props.onFocus).toHaveBeenCalled(); |
| 129 | + expect(props.onOpen).toHaveBeenCalled(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle onBlur events, and call onClose', async () => { |
| 133 | + const props = getProps(); |
| 134 | + |
| 135 | + const { getByTestId } = render(renderTooltip(props)); |
| 136 | + |
| 137 | + const tip = getByTestId('tip'); |
| 138 | + const tipWrapper = getByTestId('tipWrapper'); |
| 139 | + |
| 140 | + fireEvent.focus(tipWrapper); |
| 141 | + await waitForDomChange({ container: tip }); |
| 142 | + fireEvent.blur(tipWrapper); |
| 143 | + await waitForDomChange({ container: tip }); |
| 144 | + |
| 145 | + expect(props.onBlur).toHaveBeenCalled(); |
| 146 | + expect(props.onClose).toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should handle onMouseEnter events, and call onOpen', async () => { |
| 150 | + const props = getProps(); |
| 151 | + |
| 152 | + const { getByTestId } = render(renderTooltip(props)); |
| 153 | + |
| 154 | + const tip = getByTestId('tip'); |
| 155 | + const tipWrapper = getByTestId('tipWrapper'); |
| 156 | + |
| 157 | + fireEvent.mouseEnter(tipWrapper); |
| 158 | + await waitForDomChange({ container: tip }); |
| 159 | + |
| 160 | + expect(props.onMouseEnter).toHaveBeenCalled(); |
| 161 | + expect(props.onOpen).toHaveBeenCalled(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should handle onMouseLeave events, and call onClose', async () => { |
| 165 | + const props = getProps(); |
| 166 | + |
| 167 | + const { getByTestId } = render(renderTooltip(props)); |
| 168 | + |
| 169 | + const tip = getByTestId('tip'); |
| 170 | + const tipWrapper = getByTestId('tipWrapper'); |
| 171 | + |
| 172 | + fireEvent.mouseEnter(tipWrapper); |
| 173 | + await waitForDomChange({ container: tip }); |
| 174 | + fireEvent.mouseLeave(tipWrapper); |
| 175 | + await waitForDomChange({ container: tip }); |
| 176 | + |
| 177 | + expect(props.onMouseLeave).toHaveBeenCalled(); |
| 178 | + expect(props.onClose).toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should not handle onFocus events when disableFocusListener is true', () => { |
| 182 | + const props = getProps({ disableFocusListener: true }); |
| 183 | + |
| 184 | + const { getByTestId } = render(renderTooltip(props)); |
| 185 | + |
| 186 | + const tipWrapper = getByTestId('tipWrapper'); |
| 187 | + |
| 188 | + fireEvent.focus(tipWrapper); |
| 189 | + |
| 190 | + expect(props.onFocus).not.toHaveBeenCalled(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should not handle onBlur events when disableFocusListener is true', () => { |
| 194 | + const props = getProps({ disableFocusListener: true }); |
| 195 | + |
| 196 | + const { getByTestId } = render(renderTooltip(props)); |
| 197 | + |
| 198 | + const tipWrapper = getByTestId('tipWrapper'); |
| 199 | + |
| 200 | + fireEvent.blur(tipWrapper); |
| 201 | + |
| 202 | + expect(props.onBlur).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should not handle onMouseEnter events when disableMouseListener is true', () => { |
| 206 | + const props = getProps({ disableMouseListener: true }); |
| 207 | + |
| 208 | + const { getByTestId } = render(renderTooltip(props)); |
| 209 | + |
| 210 | + const tipWrapper = getByTestId('tipWrapper'); |
| 211 | + |
| 212 | + fireEvent.mouseEnter(tipWrapper); |
| 213 | + |
| 214 | + expect(props.onMouseEnter).not.toHaveBeenCalled(); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should not handle onMouseLeave events when disableMouseListener is true', () => { |
| 218 | + const props = getProps({ disableMouseListener: true }); |
| 219 | + |
| 220 | + const { getByTestId } = render(renderTooltip(props)); |
| 221 | + |
| 222 | + const tipWrapper = getByTestId('tipWrapper'); |
| 223 | + |
| 224 | + fireEvent.mouseLeave(tipWrapper); |
| 225 | + |
| 226 | + expect(props.onMouseLeave).not.toHaveBeenCalled(); |
| 227 | + }); |
| 228 | + }); |
| 229 | +}); |
0 commit comments