Views.test.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { vi, expect } from 'vitest'
  2. import pug from 'pug'
  3. const modulePath = '../../../../app/src/infrastructure/Views.mjs'
  4. describe('Views', function () {
  5. beforeEach(async function (ctx) {
  6. vi.doMock('@overleaf/settings', () => ({
  7. default: (ctx.settings = {
  8. viewIncludes: {
  9. someInclude: 'path/to/_include.pug',
  10. },
  11. }),
  12. }))
  13. ctx.Views = (await import(modulePath)).default
  14. })
  15. describe('_expectMetaFor', function () {
  16. const cases = [
  17. {
  18. name: 'no-js',
  19. filename: '500.pug',
  20. firstLine: 'extends ../layout/layout-no-js',
  21. expectMeta: false,
  22. },
  23. {
  24. name: 'doctype html',
  25. filename: 'user_info_not_found.pug',
  26. firstLine: 'doctype html',
  27. expectMeta: false,
  28. },
  29. {
  30. name: 'doctype xml',
  31. filename: 'feed.pug',
  32. firstLine: 'doctype xml',
  33. expectMeta: false,
  34. },
  35. {
  36. name: 'view include',
  37. filename: 'path/to/_include.pug',
  38. firstLine: '//- comment in include',
  39. expectMeta: false,
  40. },
  41. {
  42. name: 'view include',
  43. filename: 'ide-react.pug',
  44. firstLine: 'extends ../layout-react',
  45. expectMeta: true,
  46. },
  47. ]
  48. for (const { name, filename, firstLine, expectMeta } of cases) {
  49. it(name, function (ctx) {
  50. expect(ctx.Views._expectMetaFor(filename, firstLine)).to.equal(
  51. expectMeta
  52. )
  53. })
  54. }
  55. })
  56. describe('_findAllMetaTags', function () {
  57. const cases = [
  58. {
  59. name: 'simple quote',
  60. src: "meta(name='ol-foo' content=1)",
  61. found: ['ol-foo'],
  62. duplicates: [],
  63. },
  64. {
  65. name: 'double quote',
  66. src: 'meta(name="ol-foo" content=1)',
  67. found: ['ol-foo'],
  68. duplicates: [],
  69. },
  70. {
  71. name: 'code quote',
  72. src: 'meta(name=`ol-foo` content=1)',
  73. found: ['ol-foo'],
  74. duplicates: [],
  75. },
  76. {
  77. name: 'multiple',
  78. src: "meta(name='ol-foo' content=1)\nmeta(name='ol-bar' content=2)",
  79. found: ['ol-foo', 'ol-bar'],
  80. duplicates: [],
  81. },
  82. {
  83. name: 'computed single',
  84. src: "meta(name='ol-prefix-' + foo content=1)",
  85. found: ['ol-prefix-'],
  86. duplicates: [],
  87. },
  88. {
  89. name: 'computed double',
  90. src: 'meta(name="ol-prefix-" + foo content=1)',
  91. found: ['ol-prefix-'],
  92. duplicates: [],
  93. },
  94. {
  95. name: 'computed code',
  96. // eslint-disable-next-line no-template-curly-in-string
  97. src: 'meta(name=`ol-prefix-${foo}` content=1)',
  98. // eslint-disable-next-line no-template-curly-in-string
  99. found: ['ol-prefix-${foo}'],
  100. duplicates: [],
  101. },
  102. {
  103. name: 'duplicate',
  104. src: `meta(name='ol-foo' content=1)\nmeta(name="ol-foo")`,
  105. found: ['ol-foo'],
  106. duplicates: ['ol-foo'],
  107. },
  108. {
  109. name: 'compiled code',
  110. compiled: `pug_html = pug_html + "\u003Cmeta" + (" name=\\"ol-csrfToken\\""+pug.attr("content", csrfToken, true, true)) + "\u003E\u003Cmeta" + (" name=\\"ol-baseAssetPath\\""+pug.attr("content", buildBaseAssetPath(), true, true))`,
  111. found: ['ol-csrfToken', 'ol-baseAssetPath'],
  112. duplicates: [],
  113. },
  114. ]
  115. for (const { name, compiled, src, found, duplicates } of cases) {
  116. it(name, function (ctx) {
  117. const res = ctx.Views._findAllMetaTags(
  118. compiled || pug.compileClient(src, ctx.Views.PUG_COMPILE_ARGUMENTS)
  119. )
  120. expect(res).to.deep.equal({ found, duplicates })
  121. })
  122. }
  123. })
  124. })