Views.js 3.5 KB

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