mdx-component.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import mdx from '@astrojs/mdx';
  2. import { describe, it, before, after } from 'node:test';
  3. import * as assert from 'node:assert/strict';
  4. import { parseHTML } from 'linkedom';
  5. import { loadFixture } from '../../../astro/test/test-utils.js';
  6. describe('MDX Component', () => {
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: new URL('./fixtures/mdx-component/', import.meta.url),
  11. integrations: [mdx()],
  12. });
  13. });
  14. describe('build', () => {
  15. before(async () => {
  16. await fixture.build();
  17. });
  18. it('supports top-level imports', async () => {
  19. const html = await fixture.readFile('/index.html');
  20. const { document } = parseHTML(html);
  21. const h1 = document.querySelector('h1');
  22. const foo = document.querySelector('#foo');
  23. assert.equal(h1.textContent, 'Hello component!');
  24. assert.equal(foo.textContent, 'bar');
  25. });
  26. it('supports glob imports - <Component.default />', async () => {
  27. const html = await fixture.readFile('/glob/index.html');
  28. const { document } = parseHTML(html);
  29. const h1 = document.querySelector('[data-default-export] h1');
  30. const foo = document.querySelector('[data-default-export] #foo');
  31. assert.equal(h1.textContent, 'Hello component!');
  32. assert.equal(foo.textContent, 'bar');
  33. });
  34. it('supports glob imports - <Content />', async () => {
  35. const html = await fixture.readFile('/glob/index.html');
  36. const { document } = parseHTML(html);
  37. const h1 = document.querySelector('[data-content-export] h1');
  38. const foo = document.querySelector('[data-content-export] #foo');
  39. assert.equal(h1.textContent, 'Hello component!');
  40. assert.equal(foo.textContent, 'bar');
  41. });
  42. describe('with <Fragment>', () => {
  43. it('supports top-level imports', async () => {
  44. const html = await fixture.readFile('/w-fragment/index.html');
  45. const { document } = parseHTML(html);
  46. const h1 = document.querySelector('h1');
  47. const p = document.querySelector('p');
  48. assert.equal(h1.textContent, 'MDX containing <Fragment />');
  49. assert.equal(p.textContent, 'bar');
  50. });
  51. it('supports glob imports - <Component.default />', async () => {
  52. const html = await fixture.readFile('/glob/index.html');
  53. const { document } = parseHTML(html);
  54. const h = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] h1');
  55. const p = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] p');
  56. assert.equal(h.textContent, 'MDX containing <Fragment />');
  57. assert.equal(p.textContent, 'bar');
  58. });
  59. it('supports glob imports - <Content />', async () => {
  60. const html = await fixture.readFile('/glob/index.html');
  61. const { document } = parseHTML(html);
  62. const h = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] h1');
  63. const p = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] p');
  64. assert.equal(h.textContent, 'MDX containing <Fragment />');
  65. assert.equal(p.textContent, 'bar');
  66. });
  67. });
  68. });
  69. describe('dev', () => {
  70. let devServer;
  71. before(async () => {
  72. devServer = await fixture.startDevServer();
  73. });
  74. after(async () => {
  75. await devServer.stop();
  76. });
  77. it('supports top-level imports', async () => {
  78. const res = await fixture.fetch('/');
  79. assert.equal(res.status, 200);
  80. const html = await res.text();
  81. const { document } = parseHTML(html);
  82. const h1 = document.querySelector('h1');
  83. const foo = document.querySelector('#foo');
  84. assert.equal(h1.textContent, 'Hello component!');
  85. assert.equal(foo.textContent, 'bar');
  86. });
  87. it('supports glob imports - <Component.default />', async () => {
  88. const res = await fixture.fetch('/glob');
  89. assert.equal(res.status, 200);
  90. const html = await res.text();
  91. const { document } = parseHTML(html);
  92. const h1 = document.querySelector('[data-default-export] h1');
  93. const foo = document.querySelector('[data-default-export] #foo');
  94. assert.equal(h1.textContent, 'Hello component!');
  95. assert.equal(foo.textContent, 'bar');
  96. });
  97. it('supports glob imports - <Content />', async () => {
  98. const res = await fixture.fetch('/glob');
  99. assert.equal(res.status, 200);
  100. const html = await res.text();
  101. const { document } = parseHTML(html);
  102. const h1 = document.querySelector('[data-content-export] h1');
  103. const foo = document.querySelector('[data-content-export] #foo');
  104. assert.equal(h1.textContent, 'Hello component!');
  105. assert.equal(foo.textContent, 'bar');
  106. });
  107. describe('with <Fragment>', () => {
  108. it('supports top-level imports', async () => {
  109. const res = await fixture.fetch('/w-fragment');
  110. assert.equal(res.status, 200);
  111. const html = await res.text();
  112. const { document } = parseHTML(html);
  113. const h1 = document.querySelector('h1');
  114. const p = document.querySelector('p');
  115. assert.equal(h1.textContent, 'MDX containing <Fragment />');
  116. assert.equal(p.textContent, 'bar');
  117. });
  118. it('supports glob imports - <Component.default />', async () => {
  119. const res = await fixture.fetch('/glob');
  120. assert.equal(res.status, 200);
  121. const html = await res.text();
  122. const { document } = parseHTML(html);
  123. const h = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] h1');
  124. const p = document.querySelector('[data-default-export] [data-file="WithFragment.mdx"] p');
  125. assert.equal(h.textContent, 'MDX containing <Fragment />');
  126. assert.equal(p.textContent, 'bar');
  127. });
  128. it('supports glob imports - <Content />', async () => {
  129. const res = await fixture.fetch('/glob');
  130. assert.equal(res.status, 200);
  131. const html = await res.text();
  132. const { document } = parseHTML(html);
  133. const h = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] h1');
  134. const p = document.querySelector('[data-content-export] [data-file="WithFragment.mdx"] p');
  135. assert.equal(h.textContent, 'MDX containing <Fragment />');
  136. assert.equal(p.textContent, 'bar');
  137. });
  138. });
  139. });
  140. });