render.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { parseHTML } from 'linkedom';
  2. import { loadFixture } from '../../../astro/test/test-utils.js';
  3. import assert from 'node:assert/strict';
  4. import { 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 - render', () => {
  11. describe('dev', () => {
  12. it('renders content - simple', async () => {
  13. const fixture = await getFixture('render-simple');
  14. const server = await fixture.startDevServer();
  15. const res = await fixture.fetch('/');
  16. const html = await res.text();
  17. renderSimpleChecks(html);
  18. await server.stop();
  19. });
  20. it('renders content - with config', async () => {
  21. const fixture = await getFixture('render-with-config');
  22. const server = await fixture.startDevServer();
  23. const res = await fixture.fetch('/');
  24. const html = await res.text();
  25. renderConfigChecks(html);
  26. await server.stop();
  27. });
  28. it('renders content - with components', async () => {
  29. const fixture = await getFixture('render-with-components');
  30. const server = await fixture.startDevServer();
  31. const res = await fixture.fetch('/');
  32. const html = await res.text();
  33. renderComponentsChecks(html);
  34. await server.stop();
  35. });
  36. it('renders content - with indented components', async () => {
  37. const fixture = await getFixture('render-with-indented-components');
  38. const server = await fixture.startDevServer();
  39. const res = await fixture.fetch('/');
  40. const html = await res.text();
  41. renderIndentedComponentsChecks(html);
  42. await server.stop();
  43. });
  44. it('renders content - with `render: null` in document', async () => {
  45. const fixture = await getFixture('render-null');
  46. const server = await fixture.startDevServer();
  47. const res = await fixture.fetch('/');
  48. const html = await res.text();
  49. renderNullChecks(html);
  50. await server.stop();
  51. });
  52. it('renders content - with root folder containing space', async () => {
  53. const fixture = await getFixture('render with-space');
  54. const server = await fixture.startDevServer();
  55. const res = await fixture.fetch('/');
  56. const html = await res.text();
  57. renderWithRootFolderContainingSpace(html);
  58. await server.stop();
  59. });
  60. });
  61. describe('build', () => {
  62. it('renders content - simple', async () => {
  63. const fixture = await getFixture('render-simple');
  64. await fixture.build();
  65. const html = await fixture.readFile('/index.html');
  66. renderSimpleChecks(html);
  67. });
  68. it('renders content - with config', async () => {
  69. const fixture = await getFixture('render-with-config');
  70. await fixture.build();
  71. const html = await fixture.readFile('/index.html');
  72. renderConfigChecks(html);
  73. });
  74. it('renders content - with components', async () => {
  75. const fixture = await getFixture('render-with-components');
  76. await fixture.build();
  77. const html = await fixture.readFile('/index.html');
  78. renderComponentsChecks(html);
  79. });
  80. it('renders content - with indented components', async () => {
  81. const fixture = await getFixture('render-with-indented-components');
  82. await fixture.build();
  83. const html = await fixture.readFile('/index.html');
  84. renderIndentedComponentsChecks(html);
  85. });
  86. it('renders content - with `render: null` in document', async () => {
  87. const fixture = await getFixture('render-null');
  88. await fixture.build();
  89. const html = await fixture.readFile('/index.html');
  90. renderNullChecks(html);
  91. });
  92. it('renders content - with root folder containing space', async () => {
  93. const fixture = await getFixture('render with-space');
  94. await fixture.build();
  95. const html = await fixture.readFile('/index.html');
  96. renderWithRootFolderContainingSpace(html);
  97. });
  98. });
  99. });
  100. /**
  101. * @param {string} html
  102. */
  103. function renderNullChecks(html) {
  104. const { document } = parseHTML(html);
  105. const h2 = document.querySelector('h2');
  106. assert.equal(h2.textContent, 'Post with render null');
  107. assert.equal(h2.parentElement?.tagName, 'BODY');
  108. }
  109. /** @param {string} html */
  110. function renderComponentsChecks(html) {
  111. const { document } = parseHTML(html);
  112. const h2 = document.querySelector('h2');
  113. assert.equal(h2.textContent, 'Post with components');
  114. // Renders custom shortcode component
  115. const marquee = document.querySelector('marquee');
  116. assert.notEqual(marquee, null);
  117. assert.equal(marquee.hasAttribute('data-custom-marquee'), true);
  118. // Renders Astro Code component
  119. const pre = document.querySelector('pre');
  120. assert.notEqual(pre, null);
  121. assert.equal(pre.className, 'astro-code github-dark');
  122. }
  123. /** @param {string} html */
  124. function renderIndentedComponentsChecks(html) {
  125. const { document } = parseHTML(html);
  126. const h2 = document.querySelector('h2');
  127. assert.equal(h2.textContent, 'Post with indented components');
  128. // Renders custom shortcode components
  129. const marquees = document.querySelectorAll('marquee');
  130. assert.equal(marquees.length, 2);
  131. // Renders h3
  132. const h3 = document.querySelector('h3');
  133. assert.equal(h3.textContent, 'I am an h3!');
  134. // Renders Astro Code component
  135. const pre = document.querySelector('pre');
  136. assert.notEqual(pre, null);
  137. assert.equal(pre.className, 'astro-code github-dark');
  138. }
  139. /** @param {string} html */
  140. function renderConfigChecks(html) {
  141. const { document } = parseHTML(html);
  142. const h2 = document.querySelector('h2');
  143. assert.equal(h2.textContent, 'Post with config');
  144. const textContent = html;
  145. assert.notEqual(textContent.includes('Hello'), true);
  146. assert.equal(textContent.includes('Hola'), true);
  147. assert.equal(textContent.includes('Konnichiwa'), true);
  148. const runtimeVariable = document.querySelector('#runtime-variable');
  149. assert.equal(runtimeVariable?.textContent?.trim(), 'working!');
  150. }
  151. /** @param {string} html */
  152. function renderSimpleChecks(html) {
  153. const { document } = parseHTML(html);
  154. const h2 = document.querySelector('h2');
  155. assert.equal(h2.textContent, 'Simple post');
  156. const p = document.querySelector('p');
  157. assert.equal(p.textContent, 'This is a simple Markdoc post.');
  158. }
  159. /** @param {string} html */
  160. function renderWithRootFolderContainingSpace(html) {
  161. const { document } = parseHTML(html);
  162. const h2 = document.querySelector('h2');
  163. assert.equal(h2.textContent, 'Simple post with root folder containing a space');
  164. const p = document.querySelector('p');
  165. assert.equal(p.textContent, 'This is a simple Markdoc post with root folder containing a space.');
  166. }