test.js (9230B)
1 import { denormalizeCamel } from '../../src/utils'; 2 const { assert } = chai; 3 4 mocha.setup("bdd"); 5 6 function getEditor() { 7 const editor = [...document.getElementsByClassName('striven-editor')].pop(); 8 return editor.StrivenEditor(); 9 } 10 11 function node(content) { 12 const node = document.createElement("div"); 13 node.innerHTML = content; 14 return node; 15 } 16 17 /* Regression Tests */ 18 describe('Regression Tests', () => { 19 it('should not return script elements', () => { 20 const se = getEditor(); 21 22 const content = ` 23 <p>This is some dangerous content!</p> 24 <script>alert('hello');</script> 25 `; 26 27 se.setContent(content); 28 const script = node(se.getContent()).querySelector('script'); 29 30 assert.notExists(script, 'script tags were pruned'); 31 se.clearContent(); 32 }); 33 34 it('should create an ordered list with no content', () => { 35 const se = getEditor(); 36 se.toolbar.querySelector('#toolbar-insertOrderedList').click(); 37 38 assert.exists(se.body.querySelector('ol'), 'an ordered list was created'); 39 se.clearContent(); 40 }); 41 42 it('should create an unordered list with no content', () => { 43 const se = getEditor(); 44 se.toolbar.querySelector('#toolbar-insertUnorderedList').click(); 45 46 assert.exists(se.body.querySelector('ul'), 'an unordered list was created'); 47 se.clearContent(); 48 }); 49 50 it('should apply font style to content', done => { 51 const se = getEditor(); 52 se.setContent('<p>Change me to Verdana</p>'); 53 54 setTimeout(() => { 55 se.getRange().selectNode(se.body.querySelector('p')); 56 se.toolbar.querySelector('#toolbar-fontName').click(); 57 se.editor 58 .querySelector( 59 '.se-toolbar-popup-option[style="font-family: Verdana;"]' 60 ).dispatchEvent(new Event("mousedown")); 61 62 se.body.onfocus(); 63 setTimeout(() => { 64 assert.exists( 65 se.body.querySelector('p font[face="Verdana"]'), 66 'selected font style was applied' 67 ); 68 se.clearContent(); 69 done(); 70 }, 0); 71 }, 200); 72 }); 73 74 it('should apply font size to content', done => { 75 const se = getEditor(); 76 se.setContent('<p>Change me to 36pt size</p>'); 77 78 setTimeout(() => { 79 se.getRange().selectNode(se.body.querySelector('p')); 80 se.toolbar.querySelector('#toolbar-fontSize').click(); 81 const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')] 82 .filter(opt => opt.textContent === '36pt') 83 .pop(); 84 option.dispatchEvent(new Event("mousedown")); 85 se.body.onfocus(); 86 setTimeout(() => { 87 assert.exists( 88 se.body.querySelector('font[size="7"]'), 89 'selected font size was applied' 90 ); 91 se.clearContent(); 92 done(); 93 }, 0); 94 }, 200); 95 }); 96 97 it('should apply font format to content', done => { 98 const se = getEditor(); 99 se.setContent('<p>Change me to an H1 format</p>'); 100 101 setTimeout(() => { 102 se.getRange().selectNode(se.body.querySelector('p')); 103 se.toolbar.querySelector('#toolbar-fontFormat').click(); 104 const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')] 105 .filter(opt => opt.textContent === 'Heading 1') 106 .pop(); 107 option.click(); 108 se.body.onfocus(); 109 setTimeout(() => { 110 assert.exists( 111 se.body.querySelector('h1'), 112 'selected font format was applied' 113 ); 114 se.clearContent(); 115 done(); 116 }, 0); 117 }, 200); 118 }); 119 120 it('should apply a scroll bar for large content', () => { 121 const se = getEditor(); 122 se.editor.setAttribute('max-height', '300px'); 123 se.body.style.maxHeight = '300px'; 124 let largeContent = ''; 125 for (let i = 0; i < 1000; i++) { 126 largeContent += `<p>line ${i}</p>`; 127 } 128 129 se.setContent(largeContent); 130 se.body.blur(); 131 132 assert.notEqual( 133 se.body.scrollHeight, 134 se.body.offsetHeight, 135 'scroll bar was set' 136 ); 137 se.clearContent(); 138 se.overflow(); 139 }); 140 141 it('should apply a scroll bar for large content after collapsing fullscreen', () => { 142 const se = getEditor(); 143 se.editor.setAttribute('max-height', '300px'); 144 se.body.style.maxHeight = '300px'; 145 146 se.toolbar.querySelector('#toolbar-fullscreen').click(); 147 148 let largeContent = ''; 149 for (let i = 0; i < 1000; i++) { 150 largeContent += `<p>line ${i}</p>`; 151 } 152 153 se.setContent(largeContent); 154 se.toolbar.querySelector('#toolbar-fullscreen').click(); 155 156 assert.notEqual( 157 se.body.scrollHeight, 158 se.body.offsetHeight, 159 'scroll bar was set' 160 ); 161 se.clearContent(); 162 se.overflow(); 163 }); 164 165 it('should attach a file', done => { 166 const se = getEditor(); 167 168 fetch( 169 'https://raw.githubusercontent.com/striven-erp/striven-editor/master/LICENSE.txt' 170 ).then(res => 171 res.body 172 .getReader() 173 .read() 174 .then(d => { 175 se.attachFile(new File([new Blob(d.value)], 'LICENSE.txt')); 176 177 const isAttached = se.getFiles().pop() instanceof File; 178 const elCreated = 179 se.filesSection.querySelector('.se-file .se-file-name') 180 .textContent === 'LICENSE.txt'; 181 182 assert.isTrue(isAttached && elCreated, 'file was attached'); 183 se.clearFiles(); 184 done(); 185 }) 186 ); 187 }); 188 189 it('should open/close link menu', done => { 190 const se = getEditor(); 191 var wasVisible = false; 192 se.openLinkMenu(); 193 assert.exists(document.querySelector('#link-menu'), 'menu is there'); 194 195 se.openLinkMenu(); 196 setTimeout(function() { 197 if ( 198 window.getComputedStyle(document.getElementById('link-menu')) 199 .visibility == 'visible' 200 ) { 201 wasVisible = true; 202 se.closeLinkMenu(); 203 setTimeout(function() { 204 if ( 205 window.getComputedStyle(document.getElementById('link-menu')) 206 .visibility == 'hidden' 207 ) { 208 assert.isTrue(wasVisible); 209 done(); 210 } 211 }, 500); 212 } 213 }, 250); 214 }); 215 216 it("should apply strikethrough decoration to content", (done) => { 217 const se = getEditor(); 218 se.setContent("<p>Strike Me Through</p>"); 219 setTimeout(() => { 220 se.getRange().selectNode(se.body.querySelector("p")); 221 se.body.blur(); 222 se.toolbar.querySelector("#toolbar-strikethrough").click(); 223 se.body.focus(); 224 setTimeout(() => { 225 assert.exists( 226 se.body.querySelector("strike"), 227 "strikethrough was applied" 228 ); 229 se.clearContent(); 230 done(); 231 }, 0); 232 }, 200); 233 }); 234 235 }); 236 237 /* Internal Tests */ 238 describe('Internal Tests', () => { 239 it('should of been created', () => { 240 assert.exists(getEditor().editor, 'Editor is created'); 241 }); 242 243 it('should have a body', () => { 244 assert.exists(getEditor().body, 'Editor contains a body'); 245 }); 246 247 it('should have a toolbar', () => { 248 assert.exists(getEditor().toolbar, 'Editor contains a body'); 249 }); 250 }); 251 252 describe('Unit Tests', () => { 253 it('should denormalize camel case strings', () => { 254 assert.equal(denormalizeCamel('helloWorld'), 'Hello World'); 255 assert.equal(denormalizeCamel('aVariable'), 'A Variable'); 256 assert.equal( 257 denormalizeCamel('aVeryVeryVeryVeryVeryLongVariable'), 258 'A Very Very Very Very Very Long Variable' 259 ); 260 assert.equal(denormalizeCamel('nocase'), 'Nocase'); 261 assert.equal(denormalizeCamel('has_Underscore'), 'Has_ Underscore'); 262 }); 263 264 it('should passes bytes and returns a nicely formatted string of the size', () => { 265 const se = getEditor(); 266 const kb = 1024; 267 const mb = kb * kb; 268 const gb = Math.pow(kb, 3); 269 const tb = Math.pow(kb, 4); 270 const pb = Math.pow(kb, 5); 271 const eb = Math.pow(kb, 6); 272 const zb = Math.pow(kb, 7); 273 const yb = Math.pow(kb, 8); 274 275 assert.equal(se.formatBytes(0), '0 Bytes'); 276 assert.equal(se.formatBytes(256), '256 Bytes'); 277 assert.equal(se.formatBytes(kb), '1 KB'); 278 assert.equal(se.formatBytes(mb), '1 MB'); 279 assert.equal(se.formatBytes(gb), '1 GB'); 280 assert.equal(se.formatBytes(tb), '1 TB'); 281 assert.equal(se.formatBytes(pb), '1 PB'); 282 assert.equal(se.formatBytes(eb), '1 EB'); 283 assert.equal(se.formatBytes(zb), '1 ZB'); 284 assert.equal(se.formatBytes(yb), '1 YB'); 285 }) 286 287 it("should change position to static", () => { 288 const se = getEditor(); 289 const content = node( 290 `<div style="position: relative"></div> 291 <div style="position: fixed"></div> 292 <div style="position: sticky"></div> 293 <div style="position: absolute"></div>` 294 ); 295 296 const pruned = se.pruneInlineStyles(content); 297 298 const prunedElementsWithStyle = pruned.querySelectorAll('*[style]'); 299 const testArray = Array.from(prunedElementsWithStyle).filter(el=> el.style.position !== 'static') 300 assert.isEmpty(testArray, "inline styles are pruned"); 301 se.clearContent(); 302 }); 303 304 it('should check if the input string is a valid URL', () => { 305 const se = getEditor(); 306 assert.isTrue(se.validURL('https://www.google.com'),'The string is a valid link'); 307 assert.isFalse(se.validURL('invalid.string'),'The string is an invalid link'); 308 }); 309 }); 310 311 mocha.run();