remark-growi-plugin.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @typedef {import('mdast').Root} Root
  3. */
  4. import fs from 'node:fs'
  5. import path from 'node:path'
  6. import test from 'tape'
  7. import {readSync} from 'to-vfile'
  8. import {unified} from 'unified'
  9. import {remark} from 'remark'
  10. import {isHidden} from 'is-hidden'
  11. import directive from '../src/index.js'
  12. test('directive()', (t) => {
  13. t.doesNotThrow(() => {
  14. remark().use(directive).freeze()
  15. }, 'should not throw if not passed options')
  16. t.doesNotThrow(() => {
  17. unified().use(directive).freeze()
  18. }, 'should not throw if without parser or compiler')
  19. t.end()
  20. })
  21. test('fixtures', (t) => {
  22. const base = path.join('test', 'fixtures')
  23. const entries = fs.readdirSync(base).filter((d) => !isHidden(d))
  24. t.plan(entries.length)
  25. let index = -1
  26. while (++index < entries.length) {
  27. const fixture = entries[index]
  28. t.test(fixture, (st) => {
  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(directive).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. st.deepEqual(actual, expected, 'tree')
  52. st.equal(String(proc.processSync(file)), output, 'process')
  53. st.end()
  54. })
  55. }
  56. })