index.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import puppeteer from "puppeteer";
  2. declare let vditorTest: any;
  3. describe("use puppeteer to test methods", () => {
  4. let browser: any;
  5. let page: any;
  6. const defaultValue = `下一代的 Markdown 编辑器,为未来而构建
  7. [Vditor](https://github.com/Vanessa219/vditor) 是一款浏览器端的 Markdown 编辑器,使用 TypeScript 实现。`;
  8. const insertValue = "于是,Vditor 就这样诞生了。";
  9. const updateValue = "* [Vditor 使用指南](https://ld246.com/article/1549638745630?r=Vanessa)";
  10. beforeAll(async () => {
  11. browser = await puppeteer.launch();
  12. page = await browser.newPage();
  13. await Promise.all([
  14. page.coverage.startJSCoverage(),
  15. page.coverage.startCSSCoverage(),
  16. ]);
  17. await page.goto("http://localhost:9000");
  18. });
  19. it("method: getValue", async () => {
  20. const result = await page.evaluate(() => {
  21. vditorTest.setValue(`下一代的 Markdown 编辑器,为未来而构建
  22. [Vditor](https://github.com/Vanessa219/vditor) 是一款浏览器端的 Markdown 编辑器,使用 TypeScript 实现。`);
  23. return vditorTest.getValue();
  24. });
  25. expect(result).toBe(defaultValue + "\n");
  26. });
  27. it("method: insertValue", async () => {
  28. const result = await page.evaluate(() => {
  29. vditorTest.insertValue("于是,Vditor 就这样诞生了。");
  30. return vditorTest.getValue();
  31. });
  32. expect(result).toBe(defaultValue + insertValue + "\n");
  33. });
  34. it("method: focus", async () => {
  35. const result = await page.evaluate(() => {
  36. vditorTest.focus();
  37. return document.activeElement === vditorTest.vditor.editor.element;
  38. });
  39. expect(result).toBeTruthy();
  40. });
  41. it("method: blur", async () => {
  42. const result = await page.evaluate(() => {
  43. vditorTest.blur();
  44. return document.activeElement === vditorTest.vditor.editor.element;
  45. });
  46. expect(result).toBeFalsy();
  47. });
  48. it("method: disabled", async () => {
  49. const result = await page.evaluate(() => {
  50. vditorTest.disabled();
  51. return vditorTest.vditor.editor.element.getAttribute("contenteditable");
  52. });
  53. expect(result).toBe("false");
  54. });
  55. it("method: enable", async () => {
  56. const result = await page.evaluate(() => {
  57. vditorTest.enable();
  58. return vditorTest.vditor.editor.element.getAttribute("contenteditable");
  59. });
  60. expect(result).toBeTruthy();
  61. });
  62. it("method: setSelection and getSelection", async () => {
  63. const result = await page.evaluate(() => {
  64. vditorTest.setSelection(25, 66);
  65. return vditorTest.getSelection();
  66. });
  67. expect(result).toBe("[Vditor](https://github.com/Vanessa219/vditor)");
  68. });
  69. it("method: setValue", async () => {
  70. const result = await page.evaluate(() => {
  71. vditorTest.setValue("于是,Vditor 就这样诞生了。");
  72. return vditorTest.getValue();
  73. });
  74. expect(result).toBe(insertValue + "\n");
  75. });
  76. it("method: deleteValue and disabledCache", async () => {
  77. const result = await page.evaluate(() => {
  78. vditorTest.disabledCache();
  79. vditorTest.setSelection(0, 3);
  80. vditorTest.deleteValue();
  81. return {
  82. cache: localStorage.getItem("vditorvditorTest"),
  83. value: vditorTest.getValue(),
  84. };
  85. });
  86. expect(result.value).toBe("Vditor 就这样诞生了。\n");
  87. expect(result.cache).toBe(insertValue + "\n");
  88. });
  89. it("method: deleteValue null", async () => {
  90. const result = await page.evaluate(() => {
  91. vditorTest.deleteValue();
  92. return vditorTest.getValue();
  93. });
  94. expect(result).toBe("Vditor 就这样诞生了。\n");
  95. });
  96. it("method: updateValue and enableCache", async () => {
  97. const result = await page.evaluate(() => {
  98. vditorTest.enableCache();
  99. vditorTest.setSelection(0, 14);
  100. vditorTest.updateValue("* [Vditor 使用指南](https://ld246.com/article/1549638745630?r=Vanessa)");
  101. return {
  102. value: vditorTest.getValue(),
  103. cache: localStorage.getItem("vditorvditorTest"),
  104. };
  105. });
  106. expect(result.value).toBe(updateValue + "\n");
  107. expect(result.cache).toBe(updateValue + "\n");
  108. });
  109. it("method: clearCache", async () => {
  110. const result = await page.evaluate(() => {
  111. vditorTest.clearCache();
  112. return localStorage.getItem("vditorvditorTest");
  113. });
  114. expect(result).toBeNull();
  115. });
  116. it("method: html2md", async () => {
  117. const result = await page.evaluate(() => {
  118. return vditorTest.html2md('<a href="https://ld246.com/tag/vditor">讨论区</a>');
  119. });
  120. expect(result).toBe("[讨论区](https://ld246.com/tag/vditor)");
  121. });
  122. it("method: isUploading false", async () => {
  123. const result = await page.evaluate(() => {
  124. return vditorTest.isUploading();
  125. });
  126. expect(result).toBeFalsy();
  127. });
  128. it("method: isUploading true", async () => {
  129. // TODO
  130. });
  131. it("method: renderPreview", async () => {
  132. // TODO
  133. });
  134. afterAll(async () => {
  135. await browser.close();
  136. });
  137. });