template-factory.spec.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import TemplateFactory from '../../../src/util/template-factory.js'
  2. import { clearFixture, getFixture } from '../../helpers/fixture.js'
  3. describe('TemplateFactory', () => {
  4. let fixtureEl
  5. beforeAll(() => {
  6. fixtureEl = getFixture()
  7. })
  8. afterEach(() => {
  9. clearFixture()
  10. })
  11. describe('NAME', () => {
  12. it('should return plugin NAME', () => {
  13. expect(TemplateFactory.NAME).toEqual('TemplateFactory')
  14. })
  15. })
  16. describe('Default', () => {
  17. it('should return plugin default config', () => {
  18. expect(TemplateFactory.Default).toEqual(jasmine.any(Object))
  19. })
  20. })
  21. describe('toHtml', () => {
  22. describe('Sanitization', () => {
  23. it('should use "sanitizeHtml" to sanitize template', () => {
  24. const factory = new TemplateFactory({
  25. sanitize: true,
  26. template: '<div><a href="javascript:alert(7)">Click me</a></div>'
  27. })
  28. const spy = spyOn(factory, '_maybeSanitize').and.callThrough()
  29. expect(factory.toHtml().innerHTML).not.toContain('href="javascript:alert(7)')
  30. expect(spy).toHaveBeenCalled()
  31. })
  32. it('should not sanitize template', () => {
  33. const factory = new TemplateFactory({
  34. sanitize: false,
  35. template: '<div><a href="javascript:alert(7)">Click me</a></div>'
  36. })
  37. const spy = spyOn(factory, '_maybeSanitize').and.callThrough()
  38. expect(factory.toHtml().innerHTML).toContain('href="javascript:alert(7)')
  39. expect(spy).toHaveBeenCalled()
  40. })
  41. it('should use "sanitizeHtml" to sanitize content', () => {
  42. const factory = new TemplateFactory({
  43. sanitize: true,
  44. html: true,
  45. template: '<div id="foo"></div>',
  46. content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
  47. })
  48. expect(factory.toHtml().innerHTML).not.toContain('href="javascript:alert(7)')
  49. })
  50. it('should not sanitize content', () => {
  51. const factory = new TemplateFactory({
  52. sanitize: false,
  53. html: true,
  54. template: '<div id="foo"></div>',
  55. content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
  56. })
  57. expect(factory.toHtml().innerHTML).toContain('href="javascript:alert(7)')
  58. })
  59. it('should sanitize content only if "config.html" is enabled', () => {
  60. const factory = new TemplateFactory({
  61. sanitize: true,
  62. html: false,
  63. template: '<div id="foo"></div>',
  64. content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
  65. })
  66. const spy = spyOn(factory, '_maybeSanitize').and.callThrough()
  67. expect(spy).not.toHaveBeenCalled()
  68. })
  69. })
  70. describe('Extra Class', () => {
  71. it('should add extra class', () => {
  72. const factory = new TemplateFactory({
  73. extraClass: 'testClass'
  74. })
  75. expect(factory.toHtml()).toHaveClass('testClass')
  76. })
  77. it('should add extra classes', () => {
  78. const factory = new TemplateFactory({
  79. extraClass: 'testClass testClass2'
  80. })
  81. expect(factory.toHtml()).toHaveClass('testClass')
  82. expect(factory.toHtml()).toHaveClass('testClass2')
  83. })
  84. it('should resolve class if function is given', () => {
  85. const factory = new TemplateFactory({
  86. extraClass(arg) {
  87. expect(arg).toEqual(factory)
  88. return 'testClass'
  89. }
  90. })
  91. expect(factory.toHtml()).toHaveClass('testClass')
  92. })
  93. })
  94. })
  95. describe('Content', () => {
  96. it('add simple text content', () => {
  97. const template = [
  98. '<div>',
  99. ' <div class="foo"></div>',
  100. ' <div class="foo2"></div>',
  101. '</div>'
  102. ].join('')
  103. const factory = new TemplateFactory({
  104. template,
  105. content: {
  106. '.foo': 'bar',
  107. '.foo2': 'bar2'
  108. }
  109. })
  110. const html = factory.toHtml()
  111. expect(html.querySelector('.foo').textContent).toEqual('bar')
  112. expect(html.querySelector('.foo2').textContent).toEqual('bar2')
  113. })
  114. it('should not fill template if selector not exists', () => {
  115. const factory = new TemplateFactory({
  116. sanitize: true,
  117. html: true,
  118. template: '<div id="foo"></div>',
  119. content: { '#bar': 'test' }
  120. })
  121. expect(factory.toHtml().outerHTML).toEqual('<div id="foo"></div>')
  122. })
  123. it('should remove template selector, if content is null', () => {
  124. const factory = new TemplateFactory({
  125. sanitize: true,
  126. html: true,
  127. template: '<div><div id="foo"></div></div>',
  128. content: { '#foo': null }
  129. })
  130. expect(factory.toHtml().outerHTML).toEqual('<div></div>')
  131. })
  132. it('should resolve content if is function', () => {
  133. const factory = new TemplateFactory({
  134. sanitize: true,
  135. html: true,
  136. template: '<div><div id="foo"></div></div>',
  137. content: { '#foo': () => null }
  138. })
  139. expect(factory.toHtml().outerHTML).toEqual('<div></div>')
  140. })
  141. it('if content is element and "config.html=false", should put content\'s textContent', () => {
  142. fixtureEl.innerHTML = '<div>foo<span>bar</span></div>'
  143. const contentElement = fixtureEl.querySelector('div')
  144. const factory = new TemplateFactory({
  145. html: false,
  146. template: '<div><div id="foo"></div></div>',
  147. content: { '#foo': contentElement }
  148. })
  149. const fooEl = factory.toHtml().querySelector('#foo')
  150. expect(fooEl.innerHTML).not.toEqual(contentElement.innerHTML)
  151. expect(fooEl.textContent).toEqual(contentElement.textContent)
  152. expect(fooEl.textContent).toEqual('foobar')
  153. })
  154. it('if content is element and "config.html=true", should put content\'s outerHtml as child', () => {
  155. fixtureEl.innerHTML = '<div>foo<span>bar</span></div>'
  156. const contentElement = fixtureEl.querySelector('div')
  157. const factory = new TemplateFactory({
  158. html: true,
  159. template: '<div><div id="foo"></div></div>',
  160. content: { '#foo': contentElement }
  161. })
  162. const fooEl = factory.toHtml().querySelector('#foo')
  163. expect(fooEl.innerHTML).toEqual(contentElement.outerHTML)
  164. expect(fooEl.textContent).toEqual(contentElement.textContent)
  165. })
  166. })
  167. describe('getContent', () => {
  168. it('should get content as array', () => {
  169. const factory = new TemplateFactory({
  170. content: {
  171. '.foo': 'bar',
  172. '.foo2': 'bar2'
  173. }
  174. })
  175. expect(factory.getContent()).toEqual(['bar', 'bar2'])
  176. })
  177. it('should filter empties', () => {
  178. const factory = new TemplateFactory({
  179. content: {
  180. '.foo': 'bar',
  181. '.foo2': '',
  182. '.foo3': null,
  183. '.foo4': () => 2,
  184. '.foo5': () => null
  185. }
  186. })
  187. expect(factory.getContent()).toEqual(['bar', 2])
  188. })
  189. })
  190. describe('hasContent', () => {
  191. it('should return true, if it has', () => {
  192. const factory = new TemplateFactory({
  193. content: {
  194. '.foo': 'bar',
  195. '.foo2': 'bar2',
  196. '.foo3': ''
  197. }
  198. })
  199. expect(factory.hasContent()).toBeTrue()
  200. })
  201. it('should return false, if filtered content is empty', () => {
  202. const factory = new TemplateFactory({
  203. content: {
  204. '.foo2': '',
  205. '.foo3': null,
  206. '.foo4': () => null
  207. }
  208. })
  209. expect(factory.hasContent()).toBeFalse()
  210. })
  211. })
  212. describe('changeContent', () => {
  213. it('should change Content', () => {
  214. const template = [
  215. '<div>',
  216. ' <div class="foo"></div>',
  217. ' <div class="foo2"></div>',
  218. '</div>'
  219. ].join('')
  220. const factory = new TemplateFactory({
  221. template,
  222. content: {
  223. '.foo': 'bar',
  224. '.foo2': 'bar2'
  225. }
  226. })
  227. const html = selector => factory.toHtml().querySelector(selector).textContent
  228. expect(html('.foo')).toEqual('bar')
  229. expect(html('.foo2')).toEqual('bar2')
  230. factory.changeContent({
  231. '.foo': 'test',
  232. '.foo2': 'test2'
  233. })
  234. expect(html('.foo')).toEqual('test')
  235. expect(html('.foo2')).toEqual('test2')
  236. })
  237. it('should change only the given, content', () => {
  238. const template = [
  239. '<div>',
  240. ' <div class="foo"></div>',
  241. ' <div class="foo2"></div>',
  242. '</div>'
  243. ].join('')
  244. const factory = new TemplateFactory({
  245. template,
  246. content: {
  247. '.foo': 'bar',
  248. '.foo2': 'bar2'
  249. }
  250. })
  251. const html = selector => factory.toHtml().querySelector(selector).textContent
  252. expect(html('.foo')).toEqual('bar')
  253. expect(html('.foo2')).toEqual('bar2')
  254. factory.changeContent({
  255. '.foo': 'test',
  256. '.wrong': 'wrong'
  257. })
  258. expect(html('.foo')).toEqual('test')
  259. expect(html('.foo2')).toEqual('bar2')
  260. })
  261. })
  262. })