|
3 | 3 | import classnames from 'classnames';
|
4 | 4 | import toJson from 'enzyme-to-json';
|
5 | 5 | import { mount } from 'enzyme';
|
| 6 | +import { Simulate } from 'react-dom/test-utils'; |
| 7 | +import ReactDOM from 'react-dom'; |
| 8 | + |
6 | 9 | // $FlowFixMe
|
7 | 10 | import React16 from 'react-16';
|
8 | 11 | // $FlowFixMe
|
9 | 12 | import React15 from 'react-15';
|
| 13 | + |
10 | 14 | import { type LabelProps, type OverlayProps } from './';
|
11 | 15 |
|
12 | 16 | function testReact(React, Tooltip) {
|
@@ -53,52 +57,112 @@ function testReact(React, Tooltip) {
|
53 | 57 | closeButton = wrapper.find(CloseButton);
|
54 | 58 | });
|
55 | 59 |
|
56 |
| - it(`${React.version} - asdmatches the previous snapshot`, () => { |
57 |
| - expect(toJson(wrapper)).toMatchSnapshot(); |
58 |
| - }); |
59 |
| - |
60 |
| - it("increments the overlay's `id` attribute with each instance.", () => { |
61 |
| - expect(overlay.find('div').prop('id')).toEqual( |
62 |
| - 'react-accessible-tooltip-1', // as opposed to `react-accessible-tooltip-0` |
63 |
| - ); |
64 |
| - }); |
65 |
| - |
66 |
| - it('hides the overlay by default', () => { |
67 |
| - expect(wrapper.state('isHidden')).toBeTruthy(); |
68 |
| - }); |
69 |
| - |
70 |
| - it('reveals the overlay when the label is focussed, and hides the overlay when the whole tooltip is blurred.', () => { |
71 |
| - label.simulate('focus'); |
72 |
| - expect(wrapper.state('isHidden')).toBeFalsy(); |
73 |
| - |
74 |
| - overlay.simulate('focus'); |
75 |
| - expect(wrapper.state('isHidden')).toBeFalsy(); |
76 |
| - |
77 |
| - label.simulate('blur'); |
78 |
| - expect(wrapper.state('isHidden')).toBeTruthy(); |
79 |
| - }); |
80 |
| - |
81 |
| - it('respects a manual close request', () => { |
82 |
| - label.simulate('focus'); |
83 |
| - expect(wrapper.state('isHidden')).toBeFalsy(); |
84 |
| - |
85 |
| - closeButton.simulate('click'); |
86 |
| - expect(wrapper.state('isHidden')).toBeTruthy(); |
87 |
| - }); |
88 |
| - |
89 |
| - it('respects the containerRef prop', () => { |
90 |
| - const containerRef = jest.fn(); |
91 |
| - |
92 |
| - wrapper = mount( |
93 |
| - <Tooltip |
94 |
| - label={Label} |
95 |
| - overlay={Overlay} |
96 |
| - containerRef={containerRef} |
97 |
| - />, |
98 |
| - ); |
99 |
| - |
100 |
| - expect(containerRef.mock.calls.length).toEqual(1); |
101 |
| - expect(containerRef.mock.calls[0][0]).toBeInstanceOf(HTMLDivElement); |
| 60 | + describe(`${React.version} -`, () => { |
| 61 | + it('matches the previous snapshot', () => { |
| 62 | + expect(toJson(wrapper)).toMatchSnapshot(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("increments the overlay's `id` attribute with each instance.", () => { |
| 66 | + expect(overlay.find('div').prop('id')).toEqual( |
| 67 | + 'react-accessible-tooltip-1', // as opposed to `react-accessible-tooltip-0` |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('hides the overlay by default', () => { |
| 72 | + expect(wrapper.state('isHidden')).toBeTruthy(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('reveals the overlay when the label is focussed, and hides the overlay when the whole tooltip is blurred.', () => { |
| 76 | + label.simulate('focus'); |
| 77 | + expect(wrapper.state('isHidden')).toBeFalsy(); |
| 78 | + |
| 79 | + overlay.simulate('focus'); |
| 80 | + expect(wrapper.state('isHidden')).toBeFalsy(); |
| 81 | + |
| 82 | + label.simulate('blur'); |
| 83 | + expect(wrapper.state('isHidden')).toBeTruthy(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('respects a manual close request', () => { |
| 87 | + label.simulate('focus'); |
| 88 | + expect(wrapper.state('isHidden')).toBeFalsy(); |
| 89 | + |
| 90 | + closeButton.simulate('click'); |
| 91 | + expect(wrapper.state('isHidden')).toBeTruthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('respects the containerRef prop', () => { |
| 95 | + const containerRef = jest.fn(); |
| 96 | + |
| 97 | + wrapper = mount( |
| 98 | + <Tooltip |
| 99 | + label={Label} |
| 100 | + overlay={Overlay} |
| 101 | + containerRef={containerRef} |
| 102 | + />, |
| 103 | + ); |
| 104 | + |
| 105 | + wrapper.simulate('touchStart', { |
| 106 | + type: 'touchstart', |
| 107 | + x: 0, |
| 108 | + y: 0, |
| 109 | + }); |
| 110 | + |
| 111 | + expect(containerRef.mock.calls.length).toEqual(1); |
| 112 | + expect(containerRef.mock.calls[0][0]).toBeInstanceOf( |
| 113 | + HTMLDivElement, |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('touch devices -', () => { |
| 118 | + // let containerRef; |
| 119 | + let labelRef; |
| 120 | + let overlayRef; |
| 121 | + |
| 122 | + beforeAll(() => { |
| 123 | + const testRoot = document.createElement('div'); |
| 124 | + ReactDOM.render( |
| 125 | + <div |
| 126 | + // ref={_containerRef => { |
| 127 | + // containerRef = _containerRef; |
| 128 | + // }} |
| 129 | + > |
| 130 | + <Tooltip |
| 131 | + label={({ labelAttributes }) => ( |
| 132 | + <div |
| 133 | + {...labelAttributes} |
| 134 | + ref={_labelRef => { |
| 135 | + labelRef = _labelRef; |
| 136 | + }} |
| 137 | + /> |
| 138 | + )} |
| 139 | + overlay={({ overlayAttributes }) => ( |
| 140 | + <div |
| 141 | + {...overlayAttributes} |
| 142 | + ref={_overlayRef => { |
| 143 | + overlayRef = _overlayRef; |
| 144 | + }} |
| 145 | + /> |
| 146 | + )} |
| 147 | + /> |
| 148 | + </div>, |
| 149 | + testRoot, |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('opens on focus', () => { |
| 154 | + expect(overlayRef.getAttribute('aria-hidden')).toEqual('true'); |
| 155 | + Simulate.focus(labelRef); |
| 156 | + expect(overlayRef.getAttribute('aria-hidden')).toEqual('false'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('closes on touch-away', () => { |
| 160 | + const testEvent = new Event('touchstart', { bubbles: true }); |
| 161 | + // $FlowFixMe |
| 162 | + document.body.dispatchEvent(testEvent); |
| 163 | + expect(overlayRef.getAttribute('aria-hidden')).toEqual('true'); |
| 164 | + }); |
| 165 | + }); |
102 | 166 | });
|
103 | 167 | }
|
104 | 168 |
|
|
0 commit comments