|
1 | 1 | import '../spec_helper';
|
2 | 2 | import {DropdownItem} from '../../../src/react/dropdowns';
|
3 | 3 |
|
4 |
| -import {findByClass, findByTag, clickOn} from '../spec_helper'; |
5 |
| - |
6 | 4 | describe('DropdownItem', () => {
|
7 |
| - let result; |
8 |
| - const renderComponent = props => ReactDOM.render( |
9 |
| - <DropdownItem {...props}>DropdownItem Text</DropdownItem>, root |
10 |
| - ); |
11 |
| - |
12 |
| - it('passes through header', () => { |
13 |
| - result = renderComponent({header: true}); |
14 |
| - expect(findByClass(result, 'dropdown-header')).toContainText('DropdownItem Text'); |
15 |
| - }); |
| 5 | + let subject; |
16 | 6 |
|
17 |
| - it('passes through divider', () => { |
18 |
| - result = renderComponent({divider: true}); |
19 |
| - expect(findByClass(result, 'divider')).toBeDefined(); |
| 7 | + beforeEach(() => { |
| 8 | + subject = ReactDOM.render(<DropdownItem>DropdownItem Text</DropdownItem>, root); |
20 | 9 | });
|
21 | 10 |
|
22 |
| - it('passes through className and style to the li ', () => { |
23 |
| - result = renderComponent({ |
24 |
| - className: 'test-item-class', |
25 |
| - style: {opacity: '0.5'} |
| 11 | + describe('header', () => { |
| 12 | + beforeEach(() => { |
| 13 | + subject::setProps({header: true}); |
26 | 14 | });
|
27 | 15 |
|
28 |
| - const listItem = findByTag(result, 'li'); |
29 |
| - expect(listItem).toHaveClass('test-item-class'); |
30 |
| - expect(listItem).toHaveCss({opacity: '0.5'}); |
| 16 | + it('passes through header', () => { |
| 17 | + expect('#root .dropdown-header').toContainText('DropdownItem Text'); |
| 18 | + }); |
31 | 19 | });
|
32 | 20 |
|
33 |
| - it('passes through id to the anchor if an href is provided', () => { |
34 |
| - result = renderComponent({id: 'test-item-id'}); |
| 21 | + describe('divider', () => { |
| 22 | + beforeEach(() => { |
| 23 | + subject::setProps({divider: true}); |
| 24 | + }); |
35 | 25 |
|
36 |
| - let listItem = findByTag(result, 'li'); |
37 |
| - expect(listItem).not.toHaveAttr('id'); |
| 26 | + it('passes through divider', () => { |
| 27 | + expect('#root .divider').toExist(); |
| 28 | + }); |
| 29 | + }); |
38 | 30 |
|
39 |
| - result = renderComponent({id: 'test-item-id', href: 'test'}); |
| 31 | + describe('className', () => { |
| 32 | + beforeEach(() => { |
| 33 | + subject::setProps({className: 'test-item-class'}); |
| 34 | + }); |
40 | 35 |
|
41 |
| - listItem = findByTag(result, 'li'); |
42 |
| - expect(listItem).not.toHaveAttr('id'); |
43 |
| - expect(listItem.querySelector('a')).toHaveAttr('id', 'test-item-id'); |
| 36 | + it('passes through className to the li', () => { |
| 37 | + expect('#root li').toHaveClass('test-item-class'); |
| 38 | + }); |
44 | 39 | });
|
45 | 40 |
|
46 |
| - it('passes through href and target to the anchor', () => { |
47 |
| - result = renderComponent({href: 'test', target: '_blank'}); |
48 |
| - expect(findByTag(result, 'a')).toHaveAttr('href', 'test'); |
49 |
| - expect(findByTag(result, 'a')).toHaveAttr('target', '_blank'); |
| 41 | + describe('style', () => { |
| 42 | + beforeEach(() => { |
| 43 | + subject::setProps({style: {opacity: '0.5'}}); |
| 44 | + }); |
| 45 | + |
| 46 | + it('passes through style to the li', () => { |
| 47 | + expect('#root li').toHaveCss({opacity: '0.5'}); |
| 48 | + }); |
50 | 49 | });
|
51 | 50 |
|
52 |
| - describe('onSelect handling', () => { |
53 |
| - let handleSelectSpy; |
| 51 | + describe('id', () => { |
| 52 | + beforeEach(() => { |
| 53 | + subject::setProps({id: 'test-item-id'}); |
| 54 | + }); |
| 55 | + |
54 | 56 | describe('with href', () => {
|
55 |
| - it('passes through onSelect on anchor click', () => { |
56 |
| - handleSelectSpy = jasmine.createSpy('handleSelect'); |
57 |
| - const eventKey = '1'; |
58 |
| - result = renderComponent({href: 'test', onSelect: handleSelectSpy, eventKey}); |
| 57 | + beforeEach(() => { |
| 58 | + subject::setProps({href: 'test'}); |
| 59 | + }); |
59 | 60 |
|
60 |
| - clickOn(findByTag(result, 'a')); |
61 |
| - expect(handleSelectSpy).toHaveBeenCalled(); |
| 61 | + it('passes through id to the anchor', () => { |
| 62 | + expect('#root li a').toHaveAttr('id', 'test-item-id'); |
62 | 63 | });
|
63 | 64 | });
|
64 | 65 |
|
65 | 66 | describe('without href', () => {
|
66 |
| - it('passes through onSelect on list item click', () => { |
67 |
| - handleSelectSpy = jasmine.createSpy('handleSelect'); |
68 |
| - const eventKey = '1'; |
69 |
| - result = renderComponent({onSelect: handleSelectSpy, eventKey}); |
70 |
| - |
71 |
| - clickOn(findByTag(result, 'li')); |
72 |
| - expect(handleSelectSpy).toHaveBeenCalled(); |
| 67 | + it('does not pass through id to the anchor', () => { |
| 68 | + expect('#root li').not.toHaveAttr('id'); |
| 69 | + expect('#root li a').not.toExist(); |
73 | 70 | });
|
74 | 71 | });
|
| 72 | + }); |
75 | 73 |
|
76 |
| - describe('with disabled prop', () => { |
77 |
| - it('does not call onSelect handler', () => { |
78 |
| - handleSelectSpy = jasmine.createSpy('handleSelect'); |
79 |
| - result = renderComponent({href: 'test', onSelect: handleSelectSpy, disabled: true}); |
| 74 | + describe('with href and target', () => { |
| 75 | + beforeEach(() => { |
| 76 | + subject::setProps({href: 'test', target: '_blank'}); |
| 77 | + }); |
| 78 | + |
| 79 | + it('passes through href and target to the anchor', () => { |
| 80 | + expect('#root a').toHaveAttr('href', 'test'); |
| 81 | + expect('#root a').toHaveAttr('target', '_blank'); |
| 82 | + }); |
| 83 | + }); |
80 | 84 |
|
81 |
| - expect(findByTag(result, 'li')).toHaveClass('disabled'); |
82 |
| - expect(findByTag(result, 'a')).toHaveAttr('disabled'); |
| 85 | + describe('onSelect handling', () => { |
| 86 | + let handleSelectSpy; |
| 87 | + const eventKey = '1'; |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + handleSelectSpy = jasmine.createSpy('handleSelect'); |
| 91 | + subject::setProps({onSelect: handleSelectSpy, eventKey}); |
| 92 | + }); |
83 | 93 |
|
84 |
| - clickOn(findByTag(result, 'a')); |
| 94 | + describe('when li is clicked', () => { |
| 95 | + beforeEach(() => { |
| 96 | + $('#root li').click(); |
| 97 | + }); |
85 | 98 |
|
86 |
| - expect(handleSelectSpy).not.toHaveBeenCalled(); |
| 99 | + it('calls onSelect on li click', () => { |
| 100 | + expect(handleSelectSpy).toHaveBeenCalledWith(jasmine.any(Object), eventKey); |
87 | 101 | });
|
88 | 102 | });
|
89 | 103 | });
|
90 | 104 |
|
91 | 105 | describe('onClick handling', () => {
|
92 | 106 | let handleClickSpy;
|
93 |
| - describe('with href', () => { |
94 |
| - it('passes through onClick on anchor click', () => { |
95 |
| - handleClickSpy = jasmine.createSpy('handleClick'); |
96 |
| - result = renderComponent({href: 'test', onClick: handleClickSpy}); |
97 | 107 |
|
98 |
| - clickOn(findByTag(result, 'a')); |
99 |
| - expect(handleClickSpy).toHaveBeenCalled(); |
100 |
| - }); |
| 108 | + beforeEach(() => { |
| 109 | + handleClickSpy = jasmine.createSpy('handleClick'); |
| 110 | + subject::setProps({onClick: handleClickSpy}); |
101 | 111 | });
|
102 | 112 |
|
103 |
| - describe('without href', () => { |
104 |
| - it('passes through onClick on list item click', () => { |
105 |
| - handleClickSpy = jasmine.createSpy('handleClick'); |
106 |
| - result = renderComponent({onClick: handleClickSpy}); |
| 113 | + describe('when li is clicked', () => { |
| 114 | + beforeEach(() => { |
| 115 | + $('#root li').click(); |
| 116 | + }); |
107 | 117 |
|
108 |
| - clickOn(findByTag(result, 'li')); |
109 |
| - expect(handleClickSpy).toHaveBeenCalled(); |
| 118 | + it('calls onClick on li click', () => { |
| 119 | + expect(handleClickSpy).toHaveBeenCalledWith(jasmine.any(Object)); |
110 | 120 | });
|
111 | 121 | });
|
112 | 122 |
|
113 | 123 | describe('with disabled prop', () => {
|
114 |
| - it('does not call onClick handler', () => { |
115 |
| - handleClickSpy = jasmine.createSpy('handleClick'); |
116 |
| - result = renderComponent({href: 'test', onClick: handleClickSpy, disabled: true}); |
117 |
| - |
118 |
| - expect(findByTag(result, 'li')).toHaveClass('disabled'); |
119 |
| - expect(findByTag(result, 'a')).toHaveAttr('disabled'); |
| 124 | + beforeEach(() => { |
| 125 | + subject::setProps({href: 'test', disabled: true}); |
| 126 | + }); |
120 | 127 |
|
121 |
| - clickOn(findByTag(result, 'a')); |
| 128 | + it('does not call onClick handler', () => { |
| 129 | + expect('#root li').toHaveClass('disabled'); |
| 130 | + expect('#root a').toHaveAttr('disabled'); |
| 131 | + $('#root a').click(); |
122 | 132 |
|
123 | 133 | expect(handleClickSpy).not.toHaveBeenCalled();
|
124 | 134 | });
|
125 | 135 | });
|
126 | 136 | });
|
127 | 137 |
|
128 |
| - |
129 | 138 | describe('when an a tag is passed in as a child', () => {
|
130 | 139 | beforeEach(() => {
|
131 |
| - result = ReactDOM.render( |
| 140 | + subject = ReactDOM.render( |
132 | 141 | <DropdownItem><a href="custom">link</a></DropdownItem>, root
|
133 | 142 | );
|
134 | 143 |
|
135 | 144 | it('renders the child link', () => {
|
136 |
| - const listItem = findByTag(result, 'li'); |
137 |
| - expect(listItem.querySelector('a')).toHaveAttr('id', 'custom'); |
| 145 | + expect('#root li a').toHaveAttr('id', 'custom'); |
138 | 146 | });
|
139 | 147 | });
|
140 | 148 | });
|
|
0 commit comments