|
| 1 | +/* eslint react/no-multi-comp: 0, react/prop-types: 0 */ |
| 2 | +import React from 'react'; |
| 3 | +import { mount } from 'enzyme'; |
| 4 | +import { TetherContent } from '../lib'; |
| 5 | + |
| 6 | +describe('TetherContent', () => { |
| 7 | + let state; |
| 8 | + let toggle; |
| 9 | + let tetherConfig; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + state = false; |
| 13 | + toggle = () => state = !state; |
| 14 | + tetherConfig = { |
| 15 | + target: () => document.body, |
| 16 | + attachment: 'middle left', |
| 17 | + targetAttachment: 'middle right' |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + it('should not return children', () => { |
| 22 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 23 | + const instance = wrapper.instance(); |
| 24 | + expect(wrapper.children().length).toBe(0); |
| 25 | + expect(instance._element).toBe(undefined); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should renderChildren when isOpen is true', () => { |
| 29 | + state = true; |
| 30 | + spyOn(TetherContent.prototype, 'componentDidMount').and.callThrough(); |
| 31 | + spyOn(TetherContent.prototype, 'renderChildren').and.callThrough(); |
| 32 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 33 | + const instance = wrapper.instance(); |
| 34 | + |
| 35 | + expect(TetherContent.prototype.componentDidMount.calls.count()).toBe(1); |
| 36 | + expect(TetherContent.prototype.renderChildren.calls.count()).toBe(1); |
| 37 | + expect(instance.props.isOpen).toBe(true); |
| 38 | + expect(instance._element.className.indexOf('tether') > -1).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should render an arrow dom node when prop is true', () => { |
| 42 | + state = true; |
| 43 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} arrow="tooltip" toggle={toggle}><p>Content</p></TetherContent>); |
| 44 | + const instance = wrapper.instance(); |
| 45 | + |
| 46 | + expect(instance._element.textContent).toBe('Content'); |
| 47 | + expect(instance._tether.enabled).toBe(true); |
| 48 | + expect(instance._element.innerHTML.indexOf('<div class="tooltip-arrow"></div>') > -1).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should not call props.toggle when disabled ', () => { |
| 52 | + state = true; |
| 53 | + let props = jasmine.createSpyObj('props', ['toggle']); |
| 54 | + const wrapper = mount(<TetherContent disabled tether={tetherConfig} isOpen={state} toggle={props.toggle}><p>Content</p></TetherContent>); |
| 55 | + const instance = wrapper.instance(); |
| 56 | + |
| 57 | + instance.toggle({ preventDefault: () => { } }); |
| 58 | + expect(props.toggle).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('hide', () => { |
| 62 | + it('should be called on componentWillUnmount', () => { |
| 63 | + state = true; |
| 64 | + spyOn(TetherContent.prototype, 'componentWillUnmount').and.callThrough(); |
| 65 | + spyOn(TetherContent.prototype, 'hide').and.callThrough(); |
| 66 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 67 | + const instance = wrapper.instance(); |
| 68 | + |
| 69 | + expect(TetherContent.prototype.componentWillUnmount.calls.count()).toBe(0); |
| 70 | + expect(TetherContent.prototype.hide.calls.count()).toBe(0); |
| 71 | + expect(instance._element.textContent).toBe('Content'); |
| 72 | + expect(instance._tether.enabled).toBe(true); |
| 73 | + |
| 74 | + wrapper.unmount(); |
| 75 | + |
| 76 | + expect(TetherContent.prototype.componentWillUnmount.calls.count()).toBe(1); |
| 77 | + expect(TetherContent.prototype.hide.calls.count()).toBe(1); |
| 78 | + expect(instance._element).toBe(null); |
| 79 | + expect(instance._tether).toBe(null); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('show', () => { |
| 84 | + it('should be called on componentWillUnmount', () => { |
| 85 | + state = true; |
| 86 | + spyOn(TetherContent.prototype, 'componentWillUnmount').and.callThrough(); |
| 87 | + spyOn(TetherContent.prototype, 'show').and.callThrough(); |
| 88 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 89 | + const instance = wrapper.instance(); |
| 90 | + |
| 91 | + expect(TetherContent.prototype.componentWillUnmount.calls.count()).toBe(0); |
| 92 | + expect(TetherContent.prototype.show.calls.count()).toBe(1); |
| 93 | + expect(instance._element.textContent).toBe('Content'); |
| 94 | + expect(instance._tether.enabled).toBe(true); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('getTarget', () => { |
| 99 | + it('should grab dom node from function', () => { |
| 100 | + state = true; |
| 101 | + spyOn(tetherConfig, 'target').and.callThrough(); |
| 102 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 103 | + const instance = wrapper.instance(); |
| 104 | + |
| 105 | + expect(instance._element.textContent).toBe('Content'); |
| 106 | + expect(instance._tether.enabled).toBe(true); |
| 107 | + expect(tetherConfig.target).toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should grab dom node from string', () => { |
| 111 | + state = true; |
| 112 | + tetherConfig.target = 'body'; |
| 113 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 114 | + const instance = wrapper.instance(); |
| 115 | + |
| 116 | + expect(instance._element.textContent).toBe('Content'); |
| 117 | + expect(instance._tether.enabled).toBe(true); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('handleDocumentClick', () => { |
| 122 | + it('should call toggle on document click', () => { |
| 123 | + state = true; |
| 124 | + spyOn(TetherContent.prototype, 'handleDocumentClick').and.callThrough(); |
| 125 | + spyOn(TetherContent.prototype, 'toggle').and.callThrough(); |
| 126 | + |
| 127 | + mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 128 | + |
| 129 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(0); |
| 130 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(0); |
| 131 | + |
| 132 | + document.body.click(); |
| 133 | + |
| 134 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(1); |
| 135 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should call toggle on container click', () => { |
| 139 | + state = true; |
| 140 | + spyOn(TetherContent.prototype, 'handleDocumentClick').and.callThrough(); |
| 141 | + spyOn(TetherContent.prototype, 'toggle').and.callThrough(); |
| 142 | + |
| 143 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 144 | + const instance = wrapper.instance(); |
| 145 | + |
| 146 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(0); |
| 147 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(0); |
| 148 | + |
| 149 | + instance._element.click(); |
| 150 | + |
| 151 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(1); |
| 152 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(1); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should not call toggle on tethered element click', () => { |
| 156 | + state = true; |
| 157 | + spyOn(TetherContent.prototype, 'handleDocumentClick').and.callThrough(); |
| 158 | + spyOn(TetherContent.prototype, 'toggle').and.callThrough(); |
| 159 | + |
| 160 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 161 | + const instance = wrapper.instance(); |
| 162 | + |
| 163 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(0); |
| 164 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(0); |
| 165 | + |
| 166 | + instance._element.childNodes[0].click(); |
| 167 | + |
| 168 | + expect(TetherContent.prototype.handleDocumentClick.calls.count()).toBe(1); |
| 169 | + expect(TetherContent.prototype.toggle.calls.count()).toBe(0); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('handleProps', () => { |
| 174 | + it('should call .hide when false', () => { |
| 175 | + spyOn(TetherContent.prototype, 'componentDidMount').and.callThrough(); |
| 176 | + spyOn(TetherContent.prototype, 'hide').and.callThrough(); |
| 177 | + spyOn(TetherContent.prototype, 'handleProps').and.callThrough(); |
| 178 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 179 | + const instance = wrapper.instance(); |
| 180 | + |
| 181 | + expect(TetherContent.prototype.componentDidMount.calls.count()).toBe(1); |
| 182 | + expect(TetherContent.prototype.hide.calls.count()).toBe(1); |
| 183 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(1); |
| 184 | + expect(instance.props.isOpen).toBe(false); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should call .show when true', () => { |
| 188 | + state = true; |
| 189 | + spyOn(TetherContent.prototype, 'componentDidMount').and.callThrough(); |
| 190 | + spyOn(TetherContent.prototype, 'show').and.callThrough(); |
| 191 | + spyOn(TetherContent.prototype, 'handleProps').and.callThrough(); |
| 192 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 193 | + const instance = wrapper.instance(); |
| 194 | + |
| 195 | + expect(TetherContent.prototype.componentDidMount.calls.count()).toBe(1); |
| 196 | + expect(TetherContent.prototype.show.calls.count()).toBe(1); |
| 197 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(1); |
| 198 | + expect(instance.props.isOpen).toBe(true); |
| 199 | + expect(instance._element.className.indexOf('tether') > -1).toBe(true); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should be called on componentDidUpdate when isOpen changed', () => { |
| 203 | + spyOn(TetherContent.prototype, 'componentDidUpdate').and.callThrough(); |
| 204 | + spyOn(TetherContent.prototype, 'handleProps').and.callThrough(); |
| 205 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 206 | + const instance = wrapper.instance(); |
| 207 | + |
| 208 | + expect(TetherContent.prototype.componentDidUpdate.calls.count()).toBe(0); |
| 209 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(1); |
| 210 | + expect(instance.props.isOpen).toBe(false); |
| 211 | + |
| 212 | + state = true; |
| 213 | + wrapper.setProps({ isOpen: state }); |
| 214 | + |
| 215 | + expect(TetherContent.prototype.componentDidUpdate.calls.count()).toBe(1); |
| 216 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(2); |
| 217 | + expect(instance.props.isOpen).toBe(true); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should not be called on componentDidUpdate when isOpen did not change', () => { |
| 221 | + spyOn(TetherContent.prototype, 'componentDidUpdate').and.callThrough(); |
| 222 | + spyOn(TetherContent.prototype, 'handleProps').and.callThrough(); |
| 223 | + const wrapper = mount(<TetherContent tether={tetherConfig} isOpen={state} toggle={toggle}><p>Content</p></TetherContent>); |
| 224 | + const instance = wrapper.instance(); |
| 225 | + |
| 226 | + expect(TetherContent.prototype.componentDidUpdate.calls.count()).toBe(0); |
| 227 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(1); |
| 228 | + expect(instance.props.isOpen).toBe(false); |
| 229 | + |
| 230 | + wrapper.setProps({ foo: 'bar' }); |
| 231 | + |
| 232 | + expect(TetherContent.prototype.componentDidUpdate.calls.count()).toBe(1); |
| 233 | + expect(TetherContent.prototype.handleProps.calls.count()).toBe(1); |
| 234 | + expect(instance.props.isOpen).toBe(false); |
| 235 | + }); |
| 236 | + }); |
| 237 | +}); |
0 commit comments