Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 1 | define(['tutorial','util'], function (tutClass) { |
| 2 | describe('KorAP.Tutorial', function () { |
| 3 | |
| 4 | const clean = function () { |
| 5 | ['tutorial','q-field','ql-field'].forEach( |
| 6 | function (id) { |
| 7 | const el = document.getElementById(id); |
| 8 | if (el != null) { |
| 9 | el.parentNode.removeChild(el); |
| 10 | }; |
| 11 | } |
| 12 | ); |
| 13 | }; |
| 14 | |
| 15 | beforeEach(clean); |
| 16 | afterEach(clean); |
| 17 | |
| 18 | function queryFactory (query, ql = 'poliqarp', cutoff = true) { |
| 19 | const q = document.createElement('div'); |
| 20 | q.setAttribute('data-query', query); |
| 21 | q.setAttribute('data-query-language', ql); |
| 22 | q.setAttribute('data-query-cutoff', cutoff); |
| 23 | return q; |
| 24 | }; |
| 25 | |
| 26 | it('should be initializable', function () { |
| 27 | tutObj = document.createElement('div'); |
| 28 | let tut = tutClass.create(tutObj); |
| 29 | expect(tut).toBeFalsy(); |
| 30 | |
| 31 | tutObj = document.createElement('div'); |
| 32 | tutObj.setAttribute('href', '/doc/ql'); |
| 33 | tut = tutClass.create(tutObj); |
| 34 | expect(tut).toBeTruthy(); |
| 35 | tut._session.clear(); |
| 36 | }); |
| 37 | |
| 38 | it('should rewrite to JS open', function () { |
| 39 | tutObj = document.createElement('div'); |
| 40 | tutObj.setAttribute('href', 'http://example.com'); |
| 41 | expect(tutObj.getAttribute('href')).toBeTruthy(); |
| 42 | expect(tutObj.onclick).toBeNull(); |
| 43 | var tut = tutClass.create(tutObj); |
| 44 | expect(tutObj.getAttribute('href')).toBeFalsy(); |
| 45 | expect(tutObj.onclick).not.toBeNull(); |
| 46 | tut._session.clear(); |
| 47 | }); |
| 48 | |
| 49 | it('should create an embedded tutorial', function () { |
| 50 | expect(document.getElementById('tutorial')).toBeFalsy(); |
| 51 | |
| 52 | tutObj = document.createElement('div'); |
| 53 | tutObj.setAttribute('href', '/doc/ql'); |
| 54 | let tut = tutClass.create(tutObj); |
| 55 | |
| 56 | let tutEmb = document.getElementById('tutorial'); |
| 57 | expect(tutEmb).toBeTruthy(); |
| 58 | |
| 59 | expect(tutEmb.style.display).toEqual('none'); |
| 60 | tut._session.clear(); |
| 61 | }); |
| 62 | |
| 63 | it('should be visible', function () { |
| 64 | const tutE = document.createElement('div'); |
| 65 | tutE.setAttribute('href', '/doc/ql'); |
| 66 | let tut = tutClass.create(tutE); |
| 67 | tut._session.clear(); |
| 68 | let tutEmb = document.getElementById('tutorial'); |
| 69 | |
| 70 | expect(tutEmb.style.display).toEqual('none'); |
| 71 | expect(tutEmb.getElementsByTagName('IFRAME')[0]).toBeUndefined(); |
| 72 | |
| 73 | tut.show(); |
| 74 | |
| 75 | expect(tutEmb.style.display).toEqual('block'); |
| 76 | |
| 77 | let iframe = tutEmb.getElementsByTagName('IFRAME')[0]; |
| 78 | expect(iframe).not.toBeUndefined(); |
| 79 | expect(iframe.getAttribute('src')).toEqual('/doc/ql?embedded=true'); |
| 80 | tut._session.clear(); |
| 81 | }); |
| 82 | |
| 83 | it('should be visible by click', function () { |
| 84 | const tutE = document.createElement('div'); |
| 85 | tutE.setAttribute('href', '/doc/ql'); |
| 86 | let tut = tutClass.create(tutE); |
| 87 | tut._session.clear(); |
| 88 | let tutEmb = document.getElementById('tutorial'); |
| 89 | |
| 90 | expect(tutEmb.style.display).toEqual('none'); |
| 91 | expect(tutEmb.getElementsByTagName('IFRAME')[0]).toBeUndefined(); |
| 92 | |
| 93 | tutE.click(); |
| 94 | |
| 95 | expect(tutEmb.style.display).toEqual('block'); |
| 96 | |
| 97 | let iframe = tutEmb.getElementsByTagName('IFRAME')[0]; |
| 98 | expect(iframe).not.toBeUndefined(); |
| 99 | expect(iframe.getAttribute('src')).toEqual('/doc/ql?embedded=true'); |
| 100 | tut._session.clear(); |
| 101 | }); |
| 102 | |
| 103 | it('should be hidable', function () { |
| 104 | const tutE = document.createElement('div'); |
| 105 | tutE.setAttribute('href', '/doc/ql'); |
| 106 | let tut = tutClass.create(tutE); |
| 107 | tut._session.clear(); |
| 108 | let tutEmb = document.getElementById('tutorial'); |
| 109 | |
| 110 | tut.show(); |
| 111 | expect(tutEmb.style.display).toEqual('block'); |
| 112 | |
| 113 | let iframe = tutEmb.getElementsByTagName('IFRAME')[0]; |
| 114 | expect(iframe).not.toBeUndefined(); |
| 115 | |
| 116 | tut.hide(); |
| 117 | expect(tutEmb.style.display).toEqual('none'); |
| 118 | |
| 119 | iframe = tutEmb.getElementsByTagName('IFRAME')[0]; |
| 120 | expect(iframe).not.toBeUndefined(); |
| 121 | tut._session.clear(); |
| 122 | }); |
| 123 | |
| 124 | it('should remember page', function () { |
| 125 | const tutE = document.createElement('div'); |
| 126 | tutE.setAttribute('href', '/doc/ql'); |
| 127 | let tut = tutClass.create(tutE); |
| 128 | tut._session.clear(); |
| 129 | |
| 130 | expect(tut.getPage()).toBeUndefined(); |
| 131 | |
| 132 | tut.setPage('poliqarp'); |
| 133 | expect(tut.getPage()).toEqual('poliqarp'); |
| 134 | |
| 135 | // Remember section |
| 136 | let sec = document.createElement('SECTION'); |
| 137 | sec.setAttribute('id', 'cosmas-ii'); |
| 138 | |
| 139 | tut.setPage(sec); |
| 140 | expect(tut.getPage().endsWith('#cosmas-ii')).toBeTruthy(); |
| 141 | |
| 142 | // Check an inner obj |
| 143 | let doc = document.createElement('div'); |
| 144 | |
| 145 | // Create wrappers |
| 146 | let docP = document.createElement('div'); |
| 147 | docP.appendChild(sec); |
| 148 | let pre = sec = sec.addE('pre'); |
| 149 | sec = sec.addE('section'); |
| 150 | sec.appendChild(doc); |
| 151 | |
| 152 | expect(docP.outerHTML).toEqual( |
| 153 | '<div><section id="cosmas-ii"><pre><section><div>' + |
| 154 | '</div></section></pre></section></div>' |
| 155 | ); |
| 156 | |
| 157 | tut.setPage(doc); |
| 158 | expect(tut.getPage().endsWith('#cosmas-ii')).toBeTruthy(); |
| 159 | |
| 160 | pre.setAttribute('id', 'middle'); |
| 161 | |
| 162 | tut.setPage(doc); |
| 163 | expect(tut.getPage().endsWith('#middle')).toBeTruthy(); |
| 164 | tut._session.clear(); |
| 165 | }); |
| 166 | |
| 167 | it('should enable embedded queries', function () { |
| 168 | |
| 169 | // qField |
| 170 | const qField = document.createElement('input'); |
| 171 | qField.setAttribute('id','q-field'); |
| 172 | qField.value = 'xxx'; |
| 173 | document.body.appendChild(qField); |
| 174 | |
| 175 | // qlSelect |
| 176 | const qlField = document.createElement('select'); |
| 177 | qlField.setAttribute('id','ql-field'); |
| 178 | let opt = qlField.addE('option'); |
| 179 | opt.setAttribute('value', 'poliqarp'); |
| 180 | opt = qlField.addE('option'); |
| 181 | opt.setAttribute('value', 'cosmas-ii'); |
| 182 | opt.selected = true; |
| 183 | document.body.appendChild(qlField); |
| 184 | |
| 185 | expect(qlField.options[qlField.selectedIndex].value).toEqual('cosmas-ii'); |
| 186 | |
| 187 | const tutE = document.createElement('div'); |
| 188 | tutE.setAttribute('href', '/doc/ql'); |
| 189 | |
| 190 | let tut = tutClass.create(tutE); |
| 191 | tut._session.clear(); |
| 192 | |
| 193 | let tutEmb = document.getElementById('tutorial'); |
| 194 | expect(tutEmb).toBeTruthy(); |
| 195 | |
| 196 | expect(tutEmb.style.display).toEqual('none'); |
| 197 | |
| 198 | tut.show(); |
| 199 | |
| 200 | expect(tutEmb.style.display).toEqual('block'); |
| 201 | expect(qField.value).toEqual("xxx"); |
| 202 | |
| 203 | let q = queryFactory("[orth=works]"); |
| 204 | tut.useQuery(q); |
| 205 | expect(qField.value).toEqual("[orth=works]"); |
| 206 | expect(qlField.options[qlField.selectedIndex].value).toEqual('poliqarp'); |
| 207 | expect(tutEmb.style.display).toEqual('none'); |
| 208 | tut._session.clear(); |
| 209 | }); |
| 210 | |
| 211 | it('should initialize queries', function () { |
Akron | f6302ff | 2021-03-01 17:11:11 +0100 | [diff] [blame] | 212 | |
| 213 | // qField |
| 214 | const qField = document.createElement('input'); |
| 215 | qField.setAttribute('id','q-field'); |
| 216 | qField.value = 'xxx'; |
| 217 | document.body.appendChild(qField); |
| 218 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 219 | const tutE = document.createElement('div'); |
| 220 | tutE.setAttribute('href', '/doc/ql'); |
| 221 | let tut = tutClass.create(tutE); |
| 222 | tut._session.clear(); |
| 223 | let tutEmb = document.getElementById('tutorial'); |
| 224 | |
| 225 | let queries = document.createElement('div'); |
| 226 | |
| 227 | let pre0 = document.createElement('div'); |
| 228 | queries.appendChild(pre0); |
| 229 | |
| 230 | let pre1 = document.createElement('pre'); |
| 231 | pre1.classList.add('query','tutorial'); |
Akron | f6302ff | 2021-03-01 17:11:11 +0100 | [diff] [blame] | 232 | pre1.setAttribute('data-query', 'yyy'); |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 233 | queries.appendChild(pre1); |
| 234 | |
| 235 | let pre2 = document.createElement('pre'); |
| 236 | pre2.classList.add('query','tutorial','unsupported'); |
| 237 | queries.appendChild(pre2); |
| 238 | |
| 239 | let pre3 = document.createElement('div'); |
| 240 | queries.appendChild(pre3); |
| 241 | |
| 242 | let pre4 = document.createElement('pre'); |
| 243 | pre4.classList.add('query','tutorial'); |
| 244 | queries.appendChild(pre4); |
| 245 | |
| 246 | expect(pre0.onclick).toBeNull(); |
| 247 | expect(pre1.onclick).toBeNull(); |
| 248 | expect(pre2.onclick).toBeNull(); |
| 249 | expect(pre3.onclick).toBeNull(); |
| 250 | expect(pre4.onclick).toBeNull(); |
| 251 | |
| 252 | tut.initQueries(queries); |
| 253 | |
| 254 | expect(pre0.onclick).toBeNull(); |
| 255 | expect(pre1.onclick).not.toBeNull(); |
| 256 | expect(pre2.onclick).toBeNull(); |
| 257 | expect(pre3.onclick).toBeNull(); |
| 258 | expect(pre4.onclick).not.toBeNull(); |
Akron | f6302ff | 2021-03-01 17:11:11 +0100 | [diff] [blame] | 259 | |
| 260 | expect(document.getElementById('q-field').value).toBe("xxx") |
| 261 | pre1.click(); |
| 262 | expect(document.getElementById('q-field').value).toBe("yyy") |
| 263 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 264 | tut._session.clear(); |
| 265 | }); |
| 266 | |
| 267 | it('should initialize doc-links', function () { |
| 268 | const tutE = document.createElement('div'); |
| 269 | tutE.setAttribute('href', '/doc/ql'); |
| 270 | let tut = tutClass.create(tutE); |
| 271 | tut._session.clear(); |
| 272 | let tutEmb = document.getElementById('tutorial'); |
| 273 | |
| 274 | let docLinks = document.createElement('div'); |
| 275 | |
| 276 | let a; |
| 277 | let l1 = a = docLinks.addE('a'); |
| 278 | a.setAttribute('href','example:1'); |
| 279 | let l2 = a = docLinks.addE('a'); |
| 280 | a.setAttribute('href','example:2'); |
| 281 | a.classList.add('doc-link'); |
| 282 | let l3 = a = docLinks.addE('a'); |
| 283 | a.setAttribute('href','example:3'); |
| 284 | let l4 = a = docLinks.addE('a'); |
| 285 | a.setAttribute('href','example:4'); |
| 286 | a.classList.add('doc-link'); |
| 287 | |
| 288 | expect(l1.onclick).toBeNull(); |
| 289 | expect(l2.onclick).toBeNull(); |
| 290 | expect(l3.onclick).toBeNull(); |
| 291 | expect(l4.onclick).toBeNull(); |
| 292 | |
| 293 | tut.initDocLinks(docLinks); |
| 294 | |
| 295 | expect(l1.onclick).toBeNull(); |
| 296 | expect(l2.onclick).not.toBeNull(); |
| 297 | expect(l3.onclick).toBeNull(); |
| 298 | expect(l4.onclick).not.toBeNull(); |
| 299 | |
| 300 | // Click |
| 301 | expect(tut.getPage()).toEqual(undefined); |
| 302 | l2.onclick(); |
| 303 | expect(tut.getPage()).toEqual('example:2'); |
| 304 | tut._session.clear(); |
| 305 | }); |
| 306 | }); |
| 307 | }); |