test_creta_toolbar.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. var visual_editor_div;
  2. var visual_editor_content;
  3. var first_div;
  4. function initial_values(){
  5. visual_editor_div = document.getElementById('visual-editor');
  6. visual_editor_content = visual_editor_div.getElementsByClassName('visual-editor')[0];
  7. let clean_button = visual_editor_div.getElementsByClassName('clean')[0];
  8. clean_button.click();
  9. first_div = visual_editor_content.firstChild;
  10. }
  11. QUnit.module('creta_toolbar', function() {
  12. QUnit.test('clean editor', function(assert) {
  13. initial_values();
  14. first_div.innerText = "Lorem Ipsum";
  15. let clean_button = visual_editor_div.getElementsByClassName('clean')[0];
  16. assert.equal(visual_editor.get_text(), "<div>Lorem Ipsum</div>");
  17. clean_button.click();
  18. assert.equal(visual_editor.get_text(), "");
  19. });
  20. QUnit.test('headings', function(assert) {
  21. initial_values();
  22. first_div.innerText = "Lorem Ipsum";
  23. let heading_select = visual_editor_div.getElementsByClassName('heading_toolbar')[0];
  24. heading_select.selectedIndex = 1;
  25. heading_select.dispatchEvent(new Event('click'));
  26. assert.equal(visual_editor.get_text(), "<h1>Lorem Ipsum</h1>");
  27. heading_select.selectedIndex = 2;
  28. heading_select.dispatchEvent(new Event('click'));
  29. assert.equal(visual_editor.get_text(), "<h2>Lorem Ipsum</h2>");
  30. heading_select.selectedIndex = 3;
  31. heading_select.dispatchEvent(new Event('click'));
  32. assert.equal(visual_editor.get_text(), "<h3>Lorem Ipsum</h3>");
  33. });
  34. QUnit.test('list item', function(assert) {
  35. initial_values();
  36. first_div.innerText = "Lorem Ipsum";
  37. let list_button = visual_editor_div.getElementsByClassName('list_toolbar')[0];
  38. list_button.click();
  39. assert.equal(visual_editor.get_text(),
  40. "<div>Lorem Ipsum</div>\n<div>\n <ul>\n <li></li>\n </ul>\n</div>");
  41. });
  42. QUnit.test('link', function(assert) {
  43. initial_values();
  44. first_div.innerText = "Lorem Ipsum";
  45. let selection = window.getSelection();
  46. let range = document.createRange();
  47. range.selectNodeContents(first_div);
  48. selection.removeAllRanges();
  49. selection.addRange(range);
  50. let link_button = visual_editor_div.getElementsByClassName('link_toolbar')[0];
  51. link_button.click();
  52. visual_editor_div.getElementsByClassName('link_input')[0].value = "url";
  53. let accept_button = visual_editor_div.getElementsByClassName('accept_link')[0];
  54. accept_button.click();
  55. assert.equal(visual_editor.get_text(),
  56. "<div><a href=\"https://url\">Lorem Ipsum</a></div>");
  57. });
  58. QUnit.test('table', function(assert) {
  59. initial_values();
  60. first_div.innerText = "Lorem Ipsum";
  61. let link_button = visual_editor_div.getElementsByClassName('table_toolbar')[0];
  62. link_button.click();
  63. visual_editor_div.getElementsByClassName('w')[0].value = "2";
  64. visual_editor_div.getElementsByClassName('h')[0].value = "2";
  65. let accept_button = visual_editor_div.getElementsByClassName('accept_table')[0];
  66. accept_button.click();
  67. assert.equal(visual_editor.get_text(),
  68. "<div>Lorem Ipsum</div>\n<table style=\"border: 1px solid black;\">\n <tbody>\n <tr>\n <td style=\"border: 1px solid black;\"></td>\n <td style=\"border: 1px solid black;\"></td>\n </tr>\n <tr>\n <td style=\"border: 1px solid black;\"></td>\n <td style=\"border: 1px solid black;\"></td>\n </tr>\n </tbody>\n</table>\n<div></div>");
  69. });
  70. QUnit.test('block', function(assert) {
  71. initial_values();
  72. first_div.innerText = "Lorem Ipsum";
  73. let selection = window.getSelection();
  74. let range = document.createRange();
  75. range.selectNodeContents(first_div);
  76. selection.removeAllRanges();
  77. selection.addRange(range);
  78. let block_button = visual_editor_div.getElementsByClassName('block_toolbar')[0];
  79. block_button.click();
  80. visual_editor_div.getElementsByClassName('block_input')[0].value = "block_class";
  81. let accept_button = visual_editor_div.getElementsByClassName('accept_block')[0];
  82. accept_button.click();
  83. assert.equal(visual_editor.get_text(),
  84. "<div><div class=\"block_class\">Lorem Ipsum</div></div>");
  85. });
  86. QUnit.test('metatag', function(assert) {
  87. initial_values();
  88. first_div.innerText = "Lorem Ipsum";
  89. let selection = window.getSelection();
  90. let range = document.createRange();
  91. range.selectNodeContents(first_div);
  92. selection.removeAllRanges();
  93. selection.addRange(range);
  94. let code_button = visual_editor_div.getElementsByClassName('code_toolbar')[0];
  95. code_button.click();
  96. assert.equal(visual_editor.get_text(),
  97. "<div><code>Lorem Ipsum</code></div>");
  98. });
  99. QUnit.test('embed', function(assert) {
  100. initial_values();
  101. first_div.innerText = "Lorem Ipsum";
  102. let selection = window.getSelection();
  103. let range = document.createRange();
  104. range.selectNodeContents(first_div);
  105. selection.removeAllRanges();
  106. selection.addRange(range);
  107. let embed_button = visual_editor_div.getElementsByClassName('embed_toolbar')[0];
  108. embed_button.click();
  109. assert.equal(visual_editor.get_text(),
  110. "<div class=\"embed\">Lorem Ipsum</div>");
  111. });
  112. QUnit.test('save', function(assert) {
  113. initial_values();
  114. first_div.innerText = "Lorem Ipsum";
  115. let save_button = visual_editor_div.getElementsByClassName('save_toolbar')[0];
  116. save_button.click();
  117. let has_local_storage = false;
  118. try{
  119. let value = localStorage.getItem("visual-editor");
  120. has_local_storage = true;
  121. assert.equal(visual_editor_content.innerHTML, value);
  122. }catch(e){}
  123. assert.ok(has_local_storage);
  124. });
  125. QUnit.test('load', function(assert) {
  126. initial_values();
  127. let load_button = visual_editor_div.getElementsByClassName('load_toolbar')[0];
  128. load_button.click();
  129. let has_local_storage = false;
  130. try{
  131. let value = localStorage.getItem("visual-editor");
  132. has_local_storage = true;
  133. assert.equal(visual_editor_content.innerHTML, value);
  134. }catch(e){}
  135. assert.ok(has_local_storage);
  136. });
  137. });