headings.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { parseHTML } from 'linkedom';
  2. import { loadFixture } from '../../../astro/test/test-utils.js';
  3. import assert from 'node:assert/strict';
  4. import { after, before, describe, it } from 'node:test';
  5. async function getFixture(name) {
  6. return await loadFixture({
  7. root: new URL(`./fixtures/${name}/`, import.meta.url),
  8. });
  9. }
  10. describe('Markdoc - Headings', () => {
  11. let fixture;
  12. before(async () => {
  13. fixture = await getFixture('headings');
  14. });
  15. describe('dev', () => {
  16. let devServer;
  17. before(async () => {
  18. devServer = await fixture.startDevServer();
  19. });
  20. after(async () => {
  21. await devServer.stop();
  22. });
  23. it('applies IDs to headings', async () => {
  24. const res = await fixture.fetch('/headings');
  25. const html = await res.text();
  26. const { document } = parseHTML(html);
  27. idTest(document);
  28. });
  29. it('generates the same IDs for other documents with the same headings', async () => {
  30. const res = await fixture.fetch('/headings-stale-cache-check');
  31. const html = await res.text();
  32. const { document } = parseHTML(html);
  33. idTest(document);
  34. });
  35. it('generates a TOC with correct info', async () => {
  36. const res = await fixture.fetch('/headings');
  37. const html = await res.text();
  38. const { document } = parseHTML(html);
  39. tocTest(document);
  40. });
  41. });
  42. describe('build', () => {
  43. before(async () => {
  44. await fixture.build();
  45. });
  46. it('applies IDs to headings', async () => {
  47. const html = await fixture.readFile('/headings/index.html');
  48. const { document } = parseHTML(html);
  49. idTest(document);
  50. });
  51. it('generates the same IDs for other documents with the same headings', async () => {
  52. const html = await fixture.readFile('/headings-stale-cache-check/index.html');
  53. const { document } = parseHTML(html);
  54. idTest(document);
  55. });
  56. it('generates a TOC with correct info', async () => {
  57. const html = await fixture.readFile('/headings/index.html');
  58. const { document } = parseHTML(html);
  59. tocTest(document);
  60. });
  61. });
  62. });
  63. describe('Markdoc - Headings with custom Astro renderer', () => {
  64. let fixture;
  65. before(async () => {
  66. fixture = await getFixture('headings-custom');
  67. });
  68. describe('dev', () => {
  69. let devServer;
  70. before(async () => {
  71. devServer = await fixture.startDevServer();
  72. });
  73. after(async () => {
  74. await devServer.stop();
  75. });
  76. it('applies IDs to headings', async () => {
  77. const res = await fixture.fetch('/headings');
  78. const html = await res.text();
  79. const { document } = parseHTML(html);
  80. idTest(document);
  81. });
  82. it('generates the same IDs for other documents with the same headings', async () => {
  83. const res = await fixture.fetch('/headings-stale-cache-check');
  84. const html = await res.text();
  85. const { document } = parseHTML(html);
  86. idTest(document);
  87. });
  88. it('generates a TOC with correct info', async () => {
  89. const res = await fixture.fetch('/headings');
  90. const html = await res.text();
  91. const { document } = parseHTML(html);
  92. tocTest(document);
  93. });
  94. it('renders Astro component for each heading', async () => {
  95. const res = await fixture.fetch('/headings');
  96. const html = await res.text();
  97. const { document } = parseHTML(html);
  98. astroComponentTest(document);
  99. });
  100. });
  101. describe('build', () => {
  102. before(async () => {
  103. await fixture.build();
  104. });
  105. it('applies IDs to headings', async () => {
  106. const html = await fixture.readFile('/headings/index.html');
  107. const { document } = parseHTML(html);
  108. idTest(document);
  109. });
  110. it('generates the same IDs for other documents with the same headings', async () => {
  111. const html = await fixture.readFile('/headings-stale-cache-check/index.html');
  112. const { document } = parseHTML(html);
  113. idTest(document);
  114. });
  115. it('generates a TOC with correct info', async () => {
  116. const html = await fixture.readFile('/headings/index.html');
  117. const { document } = parseHTML(html);
  118. tocTest(document);
  119. });
  120. it('renders Astro component for each heading', async () => {
  121. const html = await fixture.readFile('/headings/index.html');
  122. const { document } = parseHTML(html);
  123. astroComponentTest(document);
  124. });
  125. });
  126. });
  127. const depthToHeadingMap = {
  128. 1: {
  129. slug: 'level-1-heading',
  130. text: 'Level 1 heading',
  131. },
  132. 2: {
  133. slug: 'level-2-heading',
  134. text: 'Level 2 heading',
  135. },
  136. 3: {
  137. slug: 'level-3-heading',
  138. text: 'Level 3 heading',
  139. },
  140. 4: {
  141. slug: 'level-4-heading',
  142. text: 'Level 4 heading',
  143. },
  144. 5: {
  145. slug: 'id-override',
  146. text: 'Level 5 heading with override',
  147. },
  148. 6: {
  149. slug: 'level-6-heading',
  150. text: 'Level 6 heading',
  151. },
  152. };
  153. /** @param {Document} document */
  154. function idTest(document) {
  155. for (const [depth, info] of Object.entries(depthToHeadingMap)) {
  156. assert.equal(document.querySelector(`h${depth}`)?.getAttribute('id'), info.slug);
  157. }
  158. }
  159. /** @param {Document} document */
  160. function tocTest(document) {
  161. const toc = document.querySelector('[data-toc] > ul');
  162. assert.equal(toc.children.length, Object.keys(depthToHeadingMap).length);
  163. for (const [depth, info] of Object.entries(depthToHeadingMap)) {
  164. const linkEl = toc.querySelector(`a[href="#${info.slug}"]`);
  165. assert.ok(linkEl);
  166. assert.equal(linkEl.getAttribute('data-depth'), depth);
  167. assert.equal(linkEl.textContent.trim(), info.text);
  168. }
  169. }
  170. /** @param {Document} document */
  171. function astroComponentTest(document) {
  172. const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
  173. for (const heading of headings) {
  174. assert.equal(heading.hasAttribute('data-custom-heading'), true);
  175. }
  176. }