mdx-frontmatter-injection.test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { describe, it, before } from 'node:test';
  2. import * as assert from 'node:assert/strict';
  3. import { parseHTML } from 'linkedom';
  4. import { loadFixture } from '../../../astro/test/test-utils.js';
  5. const FIXTURE_ROOT = new URL('./fixtures/mdx-frontmatter-injection/', import.meta.url);
  6. describe('MDX frontmatter injection', () => {
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: FIXTURE_ROOT,
  11. });
  12. await fixture.build();
  13. });
  14. it('remark supports custom vfile data - get title', async () => {
  15. const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
  16. const titles = frontmatterByPage.map((frontmatter = {}) => frontmatter.title);
  17. assert.equal(titles.includes('Page 1'), true);
  18. assert.equal(titles.includes('Page 2'), true);
  19. });
  20. it('rehype supports custom vfile data - reading time', async () => {
  21. const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
  22. const readingTimes = frontmatterByPage.map(
  23. (frontmatter = {}) => frontmatter.injectedReadingTime
  24. );
  25. assert.equal(readingTimes.length > 0, true);
  26. for (let readingTime of readingTimes) {
  27. assert.notEqual(readingTime, null);
  28. assert.match(readingTime.text, /^\d+ min read/);
  29. }
  30. });
  31. it('allow user frontmatter mutation', async () => {
  32. const frontmatterByPage = JSON.parse(await fixture.readFile('/glob.json'));
  33. const descriptions = frontmatterByPage.map((frontmatter = {}) => frontmatter.description);
  34. assert.equal(
  35. descriptions.includes('Processed by remarkDescription plugin: Page 1 description'),
  36. true
  37. );
  38. assert.equal(
  39. descriptions.includes('Processed by remarkDescription plugin: Page 2 description'),
  40. true
  41. );
  42. });
  43. it('passes injected frontmatter to layouts', async () => {
  44. const html1 = await fixture.readFile('/page-1/index.html');
  45. const html2 = await fixture.readFile('/page-2/index.html');
  46. const title1 = parseHTML(html1).document.querySelector('title');
  47. const title2 = parseHTML(html2).document.querySelector('title');
  48. assert.equal(title1.innerHTML, 'Page 1');
  49. assert.equal(title2.innerHTML, 'Page 2');
  50. });
  51. });