Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 1 | define(['panel','view','panel/result','panel/pagination','util'], function (panelClass,viewClass,resultClass,paginationClass) { |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 2 | |
| 3 | var controlStr = ""; |
| 4 | |
| 5 | var helloViewClass = { |
| 6 | create : function () { |
| 7 | return Object.create(viewClass)._init(['myview']).upgradeTo(this); |
| 8 | }, |
| 9 | |
| 10 | show : function () { |
| 11 | if (this._show) |
| 12 | return this._show; |
| 13 | |
| 14 | var e = document.createElement('span'); |
| 15 | e.addT("Hello World!"); |
| 16 | |
| 17 | this._show = e; |
| 18 | return e; |
| 19 | } |
| 20 | }; |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 21 | |
| 22 | var secViewClass = { |
| 23 | create : function () { |
| 24 | return Object.create(viewClass)._init(['secview']).upgradeTo(this); |
| 25 | }, |
| 26 | }; |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 27 | |
| 28 | describe('KorAP.Panel', function () { |
| 29 | |
| 30 | it('should be initializable', function () { |
| 31 | var panel = panelClass.create(); |
| 32 | var e = panel.element(); |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame^] | 33 | |
| 34 | expect(panel.type).toEqual('base'); |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 35 | expect(e.tagName).toEqual("DIV"); |
| 36 | expect(e.classList.contains("panel")).toBeTruthy(); |
| 37 | expect(e.firstChild.tagName).toEqual("DIV"); |
| 38 | |
| 39 | // No children in the empty view element |
| 40 | expect(e.firstChild.firstChild).toBeFalsy(); |
| 41 | expect(e.lastChild.tagName).toEqual("DIV"); |
| 42 | expect(e.lastChild.classList.contains("button-panel")).toBeTruthy(); |
| 43 | expect(e.lastChild.classList.contains("button-group")).toBeTruthy(); |
| 44 | expect(e.lastChild.firstChild).toBeFalsy(); |
| 45 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 46 | expect(panel.actions()).toBeTruthy(); |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | |
| 50 | it('should be extensible', function () { |
| 51 | var panel = panelClass.create(); |
| 52 | |
| 53 | controlStr = ""; |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 54 | panel.actions().add("New", {'cls':["new"]}, function () { |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 55 | controlStr = 'New!!!'; |
| 56 | }); |
| 57 | |
| 58 | var e = panel.element(); |
| 59 | |
| 60 | expect(e.tagName).toEqual("DIV"); |
| 61 | expect(e.firstChild.firstChild).toBeFalsy(); |
| 62 | |
| 63 | expect(e.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 64 | expect(e.lastChild.firstChild.getAttribute("title")).toEqual("New"); |
| 65 | expect(e.lastChild.firstChild.classList.contains("new")).toBeTruthy(); |
| 66 | expect(e.lastChild.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 67 | expect(e.lastChild.firstChild.firstChild.firstChild.data).toEqual("New"); |
| 68 | |
| 69 | expect(controlStr).toEqual(""); |
| 70 | e.lastChild.firstChild.click(); |
| 71 | expect(controlStr).toEqual("New!!!"); |
| 72 | }); |
| 73 | |
| 74 | it('should be classable', function () { |
| 75 | var panel = panelClass.create(["versuch"]); |
| 76 | var e = panel.element(); |
| 77 | expect(e.tagName).toEqual("DIV"); |
| 78 | expect(e.classList.contains("panel")).toBeTruthy(); |
| 79 | expect(e.classList.contains("versuch")).toBeTruthy(); |
| 80 | expect(e.lastChild.classList.contains("button-panel")).toBeTruthy(); |
| 81 | expect(e.lastChild.classList.contains("versuch")).toBeTruthy(); |
| 82 | }); |
| 83 | |
| 84 | it('should be extensible by a view', function () { |
| 85 | var panel = panelClass.create(); |
| 86 | var view = helloViewClass.create(); |
| 87 | var e = panel.element(); |
| 88 | |
| 89 | panel.add(view); |
| 90 | |
| 91 | var viewE = e.firstChild.firstChild; |
| 92 | expect(viewE.classList.contains('view')).toBeTruthy(); |
| 93 | expect(viewE.classList.contains('myview')).toBeTruthy(); |
| 94 | expect(viewE.firstChild.tagName).toEqual("SPAN"); |
| 95 | expect(viewE.firstChild.firstChild.data).toEqual("Hello World!"); |
| 96 | }); |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 97 | |
| 98 | it('views should be appended or prepended', function () { |
| 99 | let panel = panelClass.create(); |
| 100 | let view = helloViewClass.create(); |
| 101 | let e = panel.element(); |
| 102 | panel.add(view); |
| 103 | let secview = secViewClass.create(); |
| 104 | panel.add(secview); |
| 105 | let viewFirst = e.firstChild.firstChild; |
| 106 | expect(viewFirst.classList.contains('myview')).toBeTruthy(); |
| 107 | |
| 108 | let prependPanel = panelClass.create(); |
| 109 | prependPanel.prepend = true; |
| 110 | prependPanel.add(view); |
| 111 | prependPanel.add(secview); |
| 112 | viewFirst = prependPanel.element().firstChild.firstChild; |
| 113 | expect(viewFirst.classList.contains('secview')).toBeTruthy(); |
| 114 | }); |
| 115 | |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 116 | }); |
Akron | 362c11a | 2018-08-29 20:01:30 +0200 | [diff] [blame] | 117 | |
| 118 | describe('KorAP.Panel.Result', function () { |
| 119 | |
| 120 | it('should be initializable', function () { |
| 121 | var show = {}; |
| 122 | var result = resultClass.create(show); |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame^] | 123 | expect(result.type).toEqual('result'); |
Akron | 362c11a | 2018-08-29 20:01:30 +0200 | [diff] [blame] | 124 | expect(result.element().children.length).toEqual(2); |
| 125 | expect(result.element().firstChild.children.length).toEqual(0); |
| 126 | }); |
| 127 | |
| 128 | it('should open KQAction', function () { |
| 129 | var show = {}; |
| 130 | var result = resultClass.create(show); |
| 131 | |
| 132 | result.addKqAction(); |
| 133 | |
| 134 | expect(result.element().lastChild.firstChild.textContent).toEqual("show KoralQuery"); |
| 135 | expect(show["kq"]).toBeFalsy(); |
| 136 | |
| 137 | // Open KQ view |
| 138 | result.element().lastChild.firstChild.click(); |
| 139 | |
| 140 | expect(result.element().querySelector('#koralquery').textContent).toEqual("{}"); |
| 141 | expect(show["kq"]).toBeTruthy(); |
| 142 | |
| 143 | var close = result.element().firstChild.firstChild.lastChild.firstChild; |
| 144 | expect(close.textContent).toEqual("Close"); |
| 145 | |
| 146 | // Close view |
| 147 | close.click(); |
| 148 | |
| 149 | expect(result.element().querySelector('#koralquery')).toBeFalsy(); |
| 150 | expect(show["kq"]).toBeFalsy(); |
| 151 | }); |
Akron | ece4bb7 | 2020-10-19 12:24:20 +0200 | [diff] [blame] | 152 | |
| 153 | it('should open toggler', function () { |
| 154 | const show = {}; |
| 155 | |
| 156 | const div = document.body.addE("div"); |
| 157 | div.setAttribute("id","search"); |
| 158 | const ol = div.addE("ol"); |
| 159 | ol.classList.add("align-left"); |
| 160 | |
| 161 | const result = resultClass.create(show); |
| 162 | |
| 163 | result.addAlignAction(); |
| 164 | |
| 165 | const b = result.element().lastChild.firstChild; |
| 166 | |
| 167 | expect(b.textContent).toEqual("toggle alignment"); |
| 168 | expect(b.classList.contains('right')).toBeTruthy(); |
| 169 | expect(ol.classList.contains('align-left')).toBeTruthy(); |
| 170 | |
| 171 | b.click(); |
| 172 | |
| 173 | expect(b.textContent).toEqual("toggle alignment"); |
| 174 | expect(b.classList.contains('center')).toBeTruthy(); |
| 175 | expect(ol.classList.contains('align-right')).toBeTruthy(); |
| 176 | |
| 177 | b.click(); |
| 178 | |
| 179 | expect(b.textContent).toEqual("toggle alignment"); |
| 180 | expect(b.classList.contains('left')).toBeTruthy(); |
| 181 | expect(ol.classList.contains('align-center')).toBeTruthy(); |
| 182 | |
| 183 | b.click(); |
| 184 | |
| 185 | expect(b.textContent).toEqual("toggle alignment"); |
| 186 | expect(b.classList.contains('right')).toBeTruthy(); |
| 187 | expect(ol.classList.contains('align-left')).toBeTruthy(); |
| 188 | |
| 189 | document.body.removeChild(div); |
| 190 | }); |
Akron | 362c11a | 2018-08-29 20:01:30 +0200 | [diff] [blame] | 191 | }); |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 192 | |
| 193 | describe('KorAP.Panel.Pagination', function () { |
| 194 | it('should be initializable', function () { |
| 195 | // Create pagination element for pagination information |
| 196 | let p = document.createElement('div'); |
| 197 | p.setAttribute('id', 'pagination') |
| 198 | p.setAttribute('data-page',3); |
| 199 | p.setAttribute('data-total',30); |
| 200 | p.setAttribute('data-count',25); |
| 201 | |
| 202 | document.body.appendChild(p); |
| 203 | |
| 204 | // Create pagination class object |
| 205 | var pagination = paginationClass.create(); |
| 206 | let list = pagination.actions().element(); |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame^] | 207 | expect(pagination.type).toEqual('pagination'); |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 208 | expect(list.classList.contains('button-group-list')).toBeTruthy(); |
| 209 | expect(list.classList.contains('visible')).toBeFalsy(); |
| 210 | |
| 211 | pagination.addRandomPage(); |
| 212 | |
| 213 | let clicked = false; |
| 214 | pagination.actions().add( |
| 215 | "test", {}, function () { clicked = true } |
| 216 | ); |
| 217 | |
| 218 | pagination.buttonGroup().element().firstChild.click(); |
| 219 | expect(list.classList.contains('visible')).toBeTruthy(); |
| 220 | |
| 221 | expect(clicked).toBeFalsy(); |
| 222 | pagination.actions().element().children[4].click(); |
| 223 | expect(clicked).toBeTruthy(); |
| 224 | |
| 225 | document.body.removeChild(p); |
| 226 | document.body.removeChild(pagination.actions().element()); |
| 227 | }); |
| 228 | }); |
Akron | 56d4207 | 2018-07-24 11:17:24 +0200 | [diff] [blame] | 229 | }); |