content-collections.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { parse as parseDevalue } from 'devalue';
  2. import { loadFixture, fixLineEndings } from '../../../astro/test/test-utils.js';
  3. import markdoc from '../dist/index.js';
  4. import assert from 'node:assert/strict';
  5. import { after, before, describe, it } from 'node:test';
  6. function formatPost(post) {
  7. return {
  8. ...post,
  9. body: fixLineEndings(post.body),
  10. };
  11. }
  12. const root = new URL('./fixtures/content-collections/', import.meta.url);
  13. describe('Markdoc - Content Collections', () => {
  14. let baseFixture;
  15. before(async () => {
  16. baseFixture = await loadFixture({
  17. root,
  18. integrations: [markdoc()],
  19. });
  20. });
  21. describe('dev', () => {
  22. let devServer;
  23. before(async () => {
  24. devServer = await baseFixture.startDevServer();
  25. });
  26. after(async () => {
  27. await devServer.stop();
  28. });
  29. it('loads entry', async () => {
  30. const res = await baseFixture.fetch('/entry.json');
  31. const post = parseDevalue(await res.text());
  32. assert.deepEqual(formatPost(post), post1Entry);
  33. });
  34. it('loads collection', async () => {
  35. const res = await baseFixture.fetch('/collection.json');
  36. const posts = parseDevalue(await res.text());
  37. assert.notEqual(posts, null);
  38. assert.deepEqual(
  39. posts.sort().map((post) => formatPost(post)),
  40. [post1Entry, post2Entry, post3Entry]
  41. );
  42. });
  43. });
  44. describe('build', () => {
  45. before(async () => {
  46. await baseFixture.build();
  47. });
  48. it('loads entry', async () => {
  49. const res = await baseFixture.readFile('/entry.json');
  50. const post = parseDevalue(res);
  51. assert.deepEqual(formatPost(post), post1Entry);
  52. });
  53. it('loads collection', async () => {
  54. const res = await baseFixture.readFile('/collection.json');
  55. const posts = parseDevalue(res);
  56. assert.notEqual(posts, null);
  57. assert.deepEqual(
  58. posts.sort().map((post) => formatPost(post)),
  59. [post1Entry, post2Entry, post3Entry]
  60. );
  61. });
  62. });
  63. });
  64. const post1Entry = {
  65. id: 'post-1.mdoc',
  66. slug: 'post-1',
  67. collection: 'blog',
  68. data: {
  69. schemaWorks: true,
  70. title: 'Post 1',
  71. },
  72. body: '\n## Post 1\n\nThis is the contents of post 1.\n',
  73. };
  74. const post2Entry = {
  75. id: 'post-2.mdoc',
  76. slug: 'post-2',
  77. collection: 'blog',
  78. data: {
  79. schemaWorks: true,
  80. title: 'Post 2',
  81. },
  82. body: '\n## Post 2\n\nThis is the contents of post 2.\n',
  83. };
  84. const post3Entry = {
  85. id: 'post-3.mdoc',
  86. slug: 'post-3',
  87. collection: 'blog',
  88. data: {
  89. schemaWorks: true,
  90. title: 'Post 3',
  91. },
  92. body: '\n## Post 3\n\nThis is the contents of post 3.\n',
  93. };