Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 1 | define(['buttongroup','state'], function (buttonGroupClass, stateClass) { |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 2 | |
| 3 | var FunObj = { |
| 4 | count : 0, |
| 5 | create : function () { |
| 6 | return Object.create(this); |
| 7 | }, |
| 8 | incr : function () { |
| 9 | this.count++; |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | describe('KorAP.ButtonGroup', function () { |
| 14 | |
| 15 | it('should be initializable', function () { |
| 16 | var group = buttonGroupClass.create(['action', 'bottom']); |
| 17 | var el = group.element(); |
| 18 | expect(el.tagName).toEqual('DIV'); |
| 19 | expect(el.firstChild).toBeNull(); |
| 20 | expect(el.classList.contains('action')).toBeTruthy(); |
| 21 | expect(el.classList.contains('bottom')).toBeTruthy(); |
| 22 | expect(el.classList.contains('button-group')).toBeTruthy(); |
| 23 | }); |
| 24 | |
| 25 | it('should be expandable', function () { |
| 26 | var group = buttonGroupClass.create(); |
| 27 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 28 | |
| 29 | group.add('Meta', ['meta', 'top'], function (e) {}); |
| 30 | |
| 31 | var btn = group.element().firstChild; |
| 32 | expect(btn.tagName).toEqual('SPAN'); |
| 33 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 34 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 35 | expect(btn.innerText).toEqual('Meta'); |
| 36 | }); |
| 37 | |
Akron | d141a36 | 2018-07-10 18:12:13 +0200 | [diff] [blame] | 38 | it('should be clearable', function () { |
| 39 | var group = buttonGroupClass.create(); |
| 40 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 41 | |
| 42 | group.add('Meta', ['meta', 'top'], function (e) {}); |
| 43 | group.add('Mate', ['mate'], function (e) {}); |
| 44 | |
| 45 | var btn = group.element().children[0]; |
| 46 | expect(btn.tagName).toEqual('SPAN'); |
| 47 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 48 | expect(btn.classList.contains('top')).toBeTruthy(); |
| 49 | expect(btn.innerText).toEqual('Meta'); |
| 50 | |
| 51 | btn = group.element().children[1]; |
| 52 | expect(btn.tagName).toEqual('SPAN'); |
| 53 | expect(btn.classList.contains('mate')).toBeTruthy(); |
| 54 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 55 | expect(btn.innerText).toEqual('Mate'); |
| 56 | |
| 57 | // clear button |
| 58 | group.clear(); |
| 59 | |
| 60 | expect(group.element().children.length).toEqual(0); |
| 61 | |
| 62 | group.add('New', ['new'], function (e) {}); |
| 63 | |
| 64 | btn = group.element().children[0]; |
| 65 | expect(btn.tagName).toEqual('SPAN'); |
| 66 | expect(btn.classList.contains('new')).toBeTruthy(); |
| 67 | expect(btn.classList.contains('top')).toBeFalsy(); |
| 68 | expect(btn.innerText).toEqual('New'); |
| 69 | }); |
| 70 | |
| 71 | |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 72 | it('should listen to button clicks', function () { |
| 73 | var group = buttonGroupClass.create(); |
| 74 | |
| 75 | var count = 0; |
| 76 | |
| 77 | group.add('Meta', undefined, function () { |
| 78 | count++; |
| 79 | }); |
| 80 | |
| 81 | expect(count).toEqual(0); |
| 82 | |
| 83 | // Click on the button |
| 84 | group.element().firstChild.click(); |
| 85 | |
| 86 | expect(count).toEqual(1); |
| 87 | }); |
| 88 | |
| 89 | it('should respect binds', function () { |
| 90 | var group = buttonGroupClass.create(); |
| 91 | fun = FunObj.create(); |
| 92 | expect(fun.count).toEqual(0); |
| 93 | |
| 94 | // Bind group to another object |
Akron | b23e271 | 2018-07-13 18:17:37 +0200 | [diff] [blame] | 95 | var group2 = group.bind(fun); |
| 96 | expect(group2).toEqual(group); |
| 97 | expect(group.bind()).toEqual(fun); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 98 | |
| 99 | group.add('Incr', undefined, function (e) { |
| 100 | // increment on bind object |
| 101 | this.incr() |
| 102 | }); |
| 103 | |
| 104 | // Click on the button |
| 105 | group.element().firstChild.click(); |
| 106 | expect(fun.count).toEqual(1); |
| 107 | }); |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 108 | |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 109 | it('should add icon', function () { |
| 110 | var group = buttonGroupClass.create(); |
| 111 | expect(group.element().classList.contains('button-group')).toBeTruthy(); |
| 112 | |
| 113 | group.add('Meta', ['meta'], function (e) {}, 'metaicon'); |
| 114 | |
| 115 | var btn = group.element().firstChild; |
| 116 | expect(btn.tagName).toEqual('SPAN'); |
| 117 | expect(btn.getAttribute('data-icon')).toEqual('metaicon'); |
| 118 | expect(btn.classList.contains('meta')).toBeTruthy(); |
| 119 | expect(btn.innerText).toEqual('Meta'); |
| 120 | }); |
| 121 | |
| 122 | |
Akron | 52ed22d | 2018-07-11 17:05:19 +0200 | [diff] [blame] | 123 | it('should open lists', function () { |
| 124 | |
| 125 | }); |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 126 | |
| 127 | it('should support toggle buttons', function () { |
| 128 | var group = buttonGroupClass.create(); |
| 129 | |
| 130 | let s = stateClass.create(); |
| 131 | |
| 132 | expect(s.get()).toBeFalsy(); |
| 133 | |
| 134 | group.addToggle('example',["examplecls"],s); |
| 135 | |
| 136 | let e = group.element(); |
| 137 | |
| 138 | expect(e.firstChild.getAttribute("title")).toBe("example"); |
| 139 | expect(e.firstChild.classList.contains("examplecls")).toBeTruthy(); |
| 140 | |
| 141 | expect(e.firstChild.firstChild.classList.contains("check")).toBeTruthy(); |
| 142 | expect(e.firstChild.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 143 | expect(e.firstChild.lastChild.textContent).toBe("example"); |
| 144 | |
| 145 | // Check state |
| 146 | expect(s.get()).toBeFalsy(); |
| 147 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeFalsy(); |
| 148 | |
| 149 | // Click on the button |
| 150 | e.firstChild.click(); |
| 151 | |
| 152 | // Check state |
| 153 | expect(s.get()).toBeTruthy(); |
| 154 | expect(e.firstChild.firstChild.classList.contains("checked")).toBeTruthy(); |
| 155 | }); |
Akron | defa5e8 | 2018-07-10 12:09:46 +0200 | [diff] [blame] | 156 | }); |
| 157 | }); |