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 () { |
| 212 | const tutE = document.createElement('div'); |
| 213 | tutE.setAttribute('href', '/doc/ql'); |
| 214 | let tut = tutClass.create(tutE); |
| 215 | tut._session.clear(); |
| 216 | let tutEmb = document.getElementById('tutorial'); |
| 217 | |
| 218 | let queries = document.createElement('div'); |
| 219 | |
| 220 | let pre0 = document.createElement('div'); |
| 221 | queries.appendChild(pre0); |
| 222 | |
| 223 | let pre1 = document.createElement('pre'); |
| 224 | pre1.classList.add('query','tutorial'); |
| 225 | queries.appendChild(pre1); |
| 226 | |
| 227 | let pre2 = document.createElement('pre'); |
| 228 | pre2.classList.add('query','tutorial','unsupported'); |
| 229 | queries.appendChild(pre2); |
| 230 | |
| 231 | let pre3 = document.createElement('div'); |
| 232 | queries.appendChild(pre3); |
| 233 | |
| 234 | let pre4 = document.createElement('pre'); |
| 235 | pre4.classList.add('query','tutorial'); |
| 236 | queries.appendChild(pre4); |
| 237 | |
| 238 | expect(pre0.onclick).toBeNull(); |
| 239 | expect(pre1.onclick).toBeNull(); |
| 240 | expect(pre2.onclick).toBeNull(); |
| 241 | expect(pre3.onclick).toBeNull(); |
| 242 | expect(pre4.onclick).toBeNull(); |
| 243 | |
| 244 | tut.initQueries(queries); |
| 245 | |
| 246 | expect(pre0.onclick).toBeNull(); |
| 247 | expect(pre1.onclick).not.toBeNull(); |
| 248 | expect(pre2.onclick).toBeNull(); |
| 249 | expect(pre3.onclick).toBeNull(); |
| 250 | expect(pre4.onclick).not.toBeNull(); |
| 251 | tut._session.clear(); |
| 252 | }); |
| 253 | |
| 254 | it('should initialize doc-links', function () { |
| 255 | const tutE = document.createElement('div'); |
| 256 | tutE.setAttribute('href', '/doc/ql'); |
| 257 | let tut = tutClass.create(tutE); |
| 258 | tut._session.clear(); |
| 259 | let tutEmb = document.getElementById('tutorial'); |
| 260 | |
| 261 | let docLinks = document.createElement('div'); |
| 262 | |
| 263 | let a; |
| 264 | let l1 = a = docLinks.addE('a'); |
| 265 | a.setAttribute('href','example:1'); |
| 266 | let l2 = a = docLinks.addE('a'); |
| 267 | a.setAttribute('href','example:2'); |
| 268 | a.classList.add('doc-link'); |
| 269 | let l3 = a = docLinks.addE('a'); |
| 270 | a.setAttribute('href','example:3'); |
| 271 | let l4 = a = docLinks.addE('a'); |
| 272 | a.setAttribute('href','example:4'); |
| 273 | a.classList.add('doc-link'); |
| 274 | |
| 275 | expect(l1.onclick).toBeNull(); |
| 276 | expect(l2.onclick).toBeNull(); |
| 277 | expect(l3.onclick).toBeNull(); |
| 278 | expect(l4.onclick).toBeNull(); |
| 279 | |
| 280 | tut.initDocLinks(docLinks); |
| 281 | |
| 282 | expect(l1.onclick).toBeNull(); |
| 283 | expect(l2.onclick).not.toBeNull(); |
| 284 | expect(l3.onclick).toBeNull(); |
| 285 | expect(l4.onclick).not.toBeNull(); |
| 286 | |
| 287 | // Click |
| 288 | expect(tut.getPage()).toEqual(undefined); |
| 289 | l2.onclick(); |
| 290 | expect(tut.getPage()).toEqual('example:2'); |
| 291 | tut._session.clear(); |
| 292 | }); |
| 293 | }); |
| 294 | }); |