DraftModeManager.test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { vi, expect, describe, beforeEach, afterEach, it } from 'vitest'
  2. import Path from 'node:path'
  3. import fsPromises from 'node:fs/promises'
  4. import mockFs from 'mock-fs'
  5. const MODULE_PATH = Path.join(
  6. import.meta.dirname,
  7. '../../../app/js/DraftModeManager'
  8. )
  9. describe('DraftModeManager', () => {
  10. beforeEach(async ctx => {
  11. vi.doMock('node:fs/promises', () => ({
  12. default: fsPromises,
  13. }))
  14. ctx.DraftModeManager = (await import(MODULE_PATH)).default
  15. ctx.filename = '/mock/filename.tex'
  16. ctx.contents = `\
  17. \\documentclass{article}
  18. \\begin{document}
  19. Hello world
  20. \\end{document}\
  21. `
  22. mockFs({
  23. [ctx.filename]: ctx.contents,
  24. })
  25. })
  26. afterEach(() => {
  27. mockFs.restore()
  28. })
  29. describe('injectDraftMode', () => {
  30. it('prepends a special command to the beginning of the file', async ctx => {
  31. await ctx.DraftModeManager.promises.injectDraftMode(ctx.filename)
  32. const contents = await fsPromises.readFile(ctx.filename, {
  33. encoding: 'utf8',
  34. })
  35. expect(contents).to.equal(
  36. '\\PassOptionsToPackage{draft}{graphicx}\\PassOptionsToPackage{draft}{graphics}' +
  37. ctx.contents
  38. )
  39. })
  40. })
  41. })