2
0

remark-growi-directive.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @typedef {import('mdast').Root} Root
  3. */
  4. import fs from 'node:fs';
  5. import path from 'node:path';
  6. import { isHidden } from 'is-hidden';
  7. import { remark } from 'remark';
  8. import { readSync } from 'to-vfile';
  9. import { unified } from 'unified';
  10. import { describe, expect, it } from 'vitest';
  11. import { remarkGrowiDirectivePlugin } from '../src/remark-growi-directive.js';
  12. describe('directive()', () => {
  13. it('should not throw if not passed options', () => {
  14. expect(() => {
  15. remark().use(remarkGrowiDirectivePlugin).freeze();
  16. }).not.toThrow();
  17. });
  18. it('should not throw if without parser or compiler', () => {
  19. expect(() => {
  20. unified().use(remarkGrowiDirectivePlugin).freeze();
  21. }).not.toThrow();
  22. });
  23. });
  24. describe('fixtures', () => {
  25. const base = path.join('test', 'fixtures');
  26. const entries = fs.readdirSync(base).filter((d) => !isHidden(d));
  27. for (const fixture of entries) {
  28. it(`should handle ${fixture}`, () => {
  29. const file = readSync(path.join(base, fixture, 'input.md'));
  30. const input = String(file);
  31. const outputPath = path.join(base, fixture, 'output.md');
  32. const treePath = path.join(base, fixture, 'tree.json');
  33. const proc = remark().use(remarkGrowiDirectivePlugin).freeze();
  34. const actual = proc.parse(file);
  35. /** @type {string} */
  36. let output;
  37. /** @type {Root} */
  38. let expected;
  39. try {
  40. expected = JSON.parse(String(fs.readFileSync(treePath)));
  41. } catch {
  42. // New fixture.
  43. fs.writeFileSync(treePath, `${JSON.stringify(actual, null, 2)}\n`);
  44. expected = actual;
  45. }
  46. try {
  47. output = fs.readFileSync(outputPath, 'utf8');
  48. } catch {
  49. output = input;
  50. }
  51. expect(actual).toEqual(expected);
  52. expect(String(proc.processSync(file))).toBe(output);
  53. });
  54. }
  55. });