epic.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. const puppeteer = require('puppeteer');
  2. jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
  3. const SHORTKEY = process.platform === 'darwin' ? 'Meta' : 'Control';
  4. const CHAPTER = 'Chapter 1. Loomings.';
  5. const P1 =
  6. 'Call me Ishmael. Some years ago—never mind how long precisely-having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.';
  7. const P2 =
  8. 'There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs—commerce surrounds it with her surf. Right and left, the streets take you waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of water-gazers there.';
  9. describe('quill', function() {
  10. it('compose an epic', async function() {
  11. const browser = await puppeteer.launch({
  12. headless: false,
  13. });
  14. const page = await browser.newPage();
  15. await page.goto('http://localhost:9000/standalone/full/');
  16. await page.waitForSelector('.ql-editor', { timeout: 10000 });
  17. const title = await page.title();
  18. expect(title).toEqual('Full Editor - Quill Rich Text Editor');
  19. await page.type('.ql-editor', 'The Whale');
  20. let html = await page.$eval('.ql-editor', e => e.innerHTML);
  21. expect(html).toEqual('<p>The Whale</p>');
  22. await page.keyboard.press('Enter');
  23. html = await page.$eval('.ql-editor', e => e.innerHTML);
  24. expect(html).toEqual('<p>The Whale</p><p><br></p>');
  25. await page.keyboard.press('Enter');
  26. await page.keyboard.press('Tab');
  27. await page.type('.ql-editor', P1);
  28. await page.keyboard.press('Enter');
  29. await page.keyboard.press('Enter');
  30. await page.type('.ql-editor', P2);
  31. html = await page.$eval('.ql-editor', e => e.innerHTML);
  32. expect(html).toEqual(
  33. [
  34. '<p>The Whale</p>',
  35. '<p><br></p>',
  36. `<p>\t${P1}</p>`,
  37. '<p><br></p>',
  38. `<p>${P2}</p>`,
  39. ].join(''),
  40. );
  41. // More than enough to get to top
  42. await Promise.all(
  43. Array(20)
  44. .fill(0)
  45. .map(() => page.keyboard.press('ArrowUp')),
  46. );
  47. await page.keyboard.press('ArrowDown');
  48. await page.keyboard.press('Enter');
  49. await page.type('.ql-editor', CHAPTER);
  50. await page.keyboard.press('Enter');
  51. html = await page.$eval('.ql-editor', e => e.innerHTML);
  52. expect(html).toEqual(
  53. [
  54. '<p>The Whale</p>',
  55. '<p><br></p>',
  56. `<p>${CHAPTER}</p>`,
  57. '<p><br></p>',
  58. `<p>\t${P1}</p>`,
  59. '<p><br></p>',
  60. `<p>${P2}</p>`,
  61. ].join(''),
  62. );
  63. // More than enough to get to top
  64. await Promise.all(
  65. Array(20)
  66. .fill(0)
  67. .map(() => page.keyboard.press('ArrowUp')),
  68. );
  69. await page.keyboard.press('ArrowRight');
  70. await page.keyboard.press('ArrowRight');
  71. await page.keyboard.press('ArrowRight');
  72. await page.keyboard.press('ArrowRight');
  73. await page.keyboard.press('Backspace');
  74. await page.keyboard.press('Backspace');
  75. await page.keyboard.press('Backspace');
  76. await page.keyboard.press('Backspace');
  77. html = await page.$eval('.ql-editor', e => e.innerHTML);
  78. expect(html).toEqual(
  79. [
  80. '<p>Whale</p>',
  81. '<p><br></p>',
  82. `<p>${CHAPTER}</p>`,
  83. '<p><br></p>',
  84. `<p>\t${P1}</p>`,
  85. '<p><br></p>',
  86. `<p>${P2}</p>`,
  87. ].join(''),
  88. );
  89. await page.keyboard.press('Delete');
  90. await page.keyboard.press('Delete');
  91. await page.keyboard.press('Delete');
  92. await page.keyboard.press('Delete');
  93. await page.keyboard.press('Delete');
  94. html = await page.$eval('.ql-editor', e => e.innerHTML);
  95. expect(html).toEqual(
  96. [
  97. '<p><br></p>',
  98. '<p><br></p>',
  99. `<p>${CHAPTER}</p>`,
  100. '<p><br></p>',
  101. `<p>\t${P1}</p>`,
  102. '<p><br></p>',
  103. `<p>${P2}</p>`,
  104. ].join(''),
  105. );
  106. await page.keyboard.press('Delete');
  107. html = await page.$eval('.ql-editor', e => e.innerHTML);
  108. expect(html).toEqual(
  109. [
  110. '<p><br></p>',
  111. `<p>${CHAPTER}</p>`,
  112. '<p><br></p>',
  113. `<p>\t${P1}</p>`,
  114. '<p><br></p>',
  115. `<p>${P2}</p>`,
  116. ].join(''),
  117. );
  118. await page.click('.ql-toolbar .ql-bold');
  119. await page.click('.ql-toolbar .ql-italic');
  120. html = await page.$eval('.ql-editor', e => e.innerHTML);
  121. expect(html).toEqual(
  122. [
  123. '<p><strong><em><span class="ql-cursor">\uFEFF</span></em></strong></p>',
  124. `<p>${CHAPTER}</p>`,
  125. '<p><br></p>',
  126. `<p>\t${P1}</p>`,
  127. '<p><br></p>',
  128. `<p>${P2}</p>`,
  129. ].join(''),
  130. );
  131. let bold = await page.$('.ql-toolbar .ql-bold.ql-active');
  132. let italic = await page.$('.ql-toolbar .ql-italic.ql-active');
  133. expect(bold).not.toBe(null);
  134. expect(italic).not.toBe(null);
  135. await page.type('.ql-editor', 'Moby Dick');
  136. html = await page.$eval('.ql-editor', e => e.innerHTML);
  137. expect(html).toEqual(
  138. [
  139. '<p><strong><em>Moby Dick</em></strong></p>',
  140. `<p>${CHAPTER}</p>`,
  141. '<p><br></p>',
  142. `<p>\t${P1}</p>`,
  143. '<p><br></p>',
  144. `<p>${P2}</p>`,
  145. ].join(''),
  146. );
  147. bold = await page.$('.ql-toolbar .ql-bold.ql-active');
  148. italic = await page.$('.ql-toolbar .ql-italic.ql-active');
  149. expect(bold).not.toBe(null);
  150. expect(italic).not.toBe(null);
  151. await page.keyboard.press('ArrowRight');
  152. await page.keyboard.down('Shift');
  153. await Promise.all(
  154. Array(CHAPTER.length)
  155. .fill(0)
  156. .map(() => page.keyboard.press('ArrowRight')),
  157. );
  158. await page.keyboard.up('Shift');
  159. bold = await page.$('.ql-toolbar .ql-bold.ql-active');
  160. italic = await page.$('.ql-toolbar .ql-italic.ql-active');
  161. expect(bold).toBe(null);
  162. expect(italic).toBe(null);
  163. await page.keyboard.down(SHORTKEY);
  164. await page.keyboard.press('b');
  165. await page.keyboard.up(SHORTKEY);
  166. bold = await page.$('.ql-toolbar .ql-bold.ql-active');
  167. expect(bold).not.toBe(null);
  168. html = await page.$eval('.ql-editor', e => e.innerHTML);
  169. expect(html).toEqual(
  170. [
  171. '<p><strong><em>Moby Dick</em></strong></p>',
  172. `<p><strong>${CHAPTER}</strong></p>`,
  173. '<p><br></p>',
  174. `<p>\t${P1}</p>`,
  175. '<p><br></p>',
  176. `<p>${P2}</p>`,
  177. ].join(''),
  178. );
  179. await page.keyboard.press('ArrowLeft');
  180. await page.keyboard.press('ArrowUp');
  181. await page.click('.ql-toolbar .ql-header[value="1"]');
  182. html = await page.$eval('.ql-editor', e => e.innerHTML);
  183. expect(html).toEqual(
  184. [
  185. '<h1><strong><em>Moby Dick</em></strong></h1>',
  186. `<p><strong>${CHAPTER}</strong></p>`,
  187. '<p><br></p>',
  188. `<p>\t${P1}</p>`,
  189. '<p><br></p>',
  190. `<p>${P2}</p>`,
  191. ].join(''),
  192. );
  193. const header = await page.$('.ql-toolbar .ql-header.ql-active[value="1"]');
  194. expect(header).not.toBe(null);
  195. await page.keyboard.press('ArrowDown');
  196. await page.keyboard.press('ArrowDown');
  197. await page.keyboard.press('Enter');
  198. await page.keyboard.press('Enter');
  199. await page.keyboard.press('ArrowUp');
  200. await page.type('.ql-editor', 'AA');
  201. await page.keyboard.press('ArrowLeft');
  202. await page.keyboard.down(SHORTKEY);
  203. await page.keyboard.press('b');
  204. await page.keyboard.press('b');
  205. await page.keyboard.up(SHORTKEY);
  206. await page.type('.ql-editor', 'B');
  207. html = await page.$$eval('.ql-editor p', paras => paras[2].innerHTML);
  208. expect(html).toBe('ABA');
  209. await page.keyboard.down(SHORTKEY);
  210. await page.keyboard.press('b');
  211. await page.keyboard.up(SHORTKEY);
  212. await page.type('.ql-editor', 'C');
  213. await page.keyboard.down(SHORTKEY);
  214. await page.keyboard.press('b');
  215. await page.keyboard.up(SHORTKEY);
  216. await page.type('.ql-editor', 'D');
  217. html = await page.$$eval('.ql-editor p', paras => paras[2].innerHTML);
  218. expect(html).toBe('AB<strong>C</strong>DA');
  219. const selection = await page.evaluate(getSelectionInTextNode);
  220. expect(selection).toBe('["DA",1,"DA",1]');
  221. // await page.waitFor(1000000);
  222. await browser.close();
  223. });
  224. });
  225. function getSelectionInTextNode() {
  226. const {
  227. anchorNode,
  228. anchorOffset,
  229. focusNode,
  230. focusOffset,
  231. } = document.getSelection();
  232. return JSON.stringify([
  233. anchorNode.data,
  234. anchorOffset,
  235. focusNode.data,
  236. focusOffset,
  237. ]);
  238. }