summaryrefslogtreecommitdiffstats
path: root/js/tests/unit/util/template-factory.spec.js
blob: 5e5724c5f81dce908d64177416bfe04a3886f291 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { clearFixture, getFixture } from '../../helpers/fixture'
import TemplateFactory from '../../../src/util/template-factory'

describe('TemplateFactory', () => {
  let fixtureEl

  beforeAll(() => {
    fixtureEl = getFixture()
  })

  afterEach(() => {
    clearFixture()
  })

  describe('NAME', () => {
    it('should return plugin NAME', () => {
      expect(TemplateFactory.NAME).toEqual('TemplateFactory')
    })
  })

  describe('Default', () => {
    it('should return plugin default config', () => {
      expect(TemplateFactory.Default).toEqual(jasmine.any(Object))
    })
  })

  describe('toHtml', () => {
    describe('Sanitization', () => {
      it('should use "sanitizeHtml" to sanitize template', () => {
        const factory = new TemplateFactory({
          sanitize: true,
          template: '<div><a href="javascript:alert(7)">Click me</a></div>'
        })
        const spy = spyOn(factory, '_maybeSanitize').and.callThrough()

        expect(factory.toHtml().innerHTML).not.toContain('href="javascript:alert(7)')
        expect(spy).toHaveBeenCalled()
      })

      it('should not sanitize template', () => {
        const factory = new TemplateFactory({
          sanitize: false,
          template: '<div><a href="javascript:alert(7)">Click me</a></div>'
        })
        const spy = spyOn(factory, '_maybeSanitize').and.callThrough()

        expect(factory.toHtml().innerHTML).toContain('href="javascript:alert(7)')
        expect(spy).toHaveBeenCalled()
      })

      it('should use "sanitizeHtml" to sanitize content', () => {
        const factory = new TemplateFactory({
          sanitize: true,
          html: true,
          template: '<div id="foo"></div>',
          content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
        })
        expect(factory.toHtml().innerHTML).not.toContain('href="javascript:alert(7)')
      })

      it('should not sanitize content', () => {
        const factory = new TemplateFactory({
          sanitize: false,
          html: true,
          template: '<div id="foo"></div>',
          content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
        })
        expect(factory.toHtml().innerHTML).toContain('href="javascript:alert(7)')
      })

      it('should sanitize content only if "config.html" is enabled', () => {
        const factory = new TemplateFactory({
          sanitize: true,
          html: false,
          template: '<div id="foo"></div>',
          content: { '#foo': '<a href="javascript:alert(7)">Click me</a>' }
        })
        const spy = spyOn(factory, '_maybeSanitize').and.callThrough()

        expect(spy).not.toHaveBeenCalled()
      })
    })

    describe('Extra Class', () => {
      it('should add extra class', () => {
        const factory = new TemplateFactory({
          extraClass: 'testClass'
        })
        expect(factory.toHtml()).toHaveClass('testClass')
      })

      it('should add extra classes', () => {
        const factory = new TemplateFactory({
          extraClass: 'testClass testClass2'
        })
        expect(factory.toHtml()).toHaveClass('testClass')
        expect(factory.toHtml()).toHaveClass('testClass2')
      })

      it('should resolve class if function is given', () => {
        const factory = new TemplateFactory({
          extraClass(arg) {
            expect(arg).toEqual(factory)
            return 'testClass'
          }
        })

        expect(factory.toHtml()).toHaveClass('testClass')
      })
    })
  })

  describe('Content', () => {
    it('add simple text content', () => {
      const template = [
        '<div>',
        '  <div class="foo"></div>',
        '  <div class="foo2"></div>',
        '</div>'
      ].join('')

      const factory = new TemplateFactory({
        template,
        content: {
          '.foo': 'bar',
          '.foo2': 'bar2'
        }
      })

      const html = factory.toHtml()
      expect(html.querySelector('.foo').textContent).toEqual('bar')
      expect(html.querySelector('.foo2').textContent).toEqual('bar2')
    })

    it('should not fill template if selector not exists', () => {
      const factory = new TemplateFactory({
        sanitize: true,
        html: true,
        template: '<div id="foo"></div>',
        content: { '#bar': 'test' }
      })

      expect(factory.toHtml().outerHTML).toEqual('<div id="foo"></div>')
    })

    it('should remove template selector, if content is null', () => {
      const factory = new TemplateFactory({
        sanitize: true,
        html: true,
        template: '<div><div id="foo"></div></div>',
        content: { '#foo': null }
      })

      expect(factory.toHtml().outerHTML).toEqual('<div></div>')
    })

    it('should resolve content if is function', () => {
      const factory = new TemplateFactory({
        sanitize: true,
        html: true,
        template: '<div><div id="foo"></div></div>',
        content: { '#foo': () => null }
      })

      expect(factory.toHtml().outerHTML).toEqual('<div></div>')
    })

    it('if content is element and "config.html=false", should put content\'s textContent', () => {
      fixtureEl.innerHTML = '<div>foo<span>bar</span></div>'
      const contentElement = fixtureEl.querySelector('div')

      const factory = new TemplateFactory({
        html: false,
        template: '<div><div id="foo"></div></div>',
        content: { '#foo': contentElement }
      })

      const fooEl = factory.toHtml().querySelector('#foo')
      expect(fooEl.innerHTML).not.toEqual(contentElement.innerHTML)
      expect(fooEl.textContent).toEqual(contentElement.textContent)
      expect(fooEl.textContent).toEqual('foobar')
    })

    it('if content is element and "config.html=true", should put content\'s outerHtml as child', () => {
      fixtureEl.innerHTML = '<div>foo<span>bar</span></div>'
      const contentElement = fixtureEl.querySelector('div')

      const factory = new TemplateFactory({
        html: true,
        template: '<div><div id="foo"></div></div>',
        content: { '#foo': contentElement }
      })

      const fooEl = factory.toHtml().querySelector('#foo')
      expect(fooEl.innerHTML).toEqual(contentElement.outerHTML)
      expect(fooEl.textContent).toEqual(contentElement.textContent)
    })
  })

  describe('getContent', () => {
    it('should get content as array', () => {
      const factory = new TemplateFactory({
        content: {
          '.foo': 'bar',
          '.foo2': 'bar2'
        }
      })
      expect(factory.getContent()).toEqual(['bar', 'bar2'])
    })

    it('should filter empties', () => {
      const factory = new TemplateFactory({
        content: {
          '.foo': 'bar',
          '.foo2': '',
          '.foo3': null,
          '.foo4': () => 2,
          '.foo5': () => null
        }
      })
      expect(factory.getContent()).toEqual(['bar', 2])
    })
  })

  describe('hasContent', () => {
    it('should return true, if it has', () => {
      const factory = new TemplateFactory({
        content: {
          '.foo': 'bar',
          '.foo2': 'bar2',
          '.foo3': ''
        }
      })
      expect(factory.hasContent()).toBeTrue()
    })

    it('should return false, if filtered content is empty', () => {
      const factory = new TemplateFactory({
        content: {
          '.foo2': '',
          '.foo3': null,
          '.foo4': () => null
        }
      })
      expect(factory.hasContent()).toBeFalse()
    })
  })

  describe('changeContent', () => {
    it('should change Content', () => {
      const template = [
        '<div>',
        '  <div class="foo"></div>',
        '  <div class="foo2"></div>',
        '</div>'
      ].join('')

      const factory = new TemplateFactory({
        template,
        content: {
          '.foo': 'bar',
          '.foo2': 'bar2'
        }
      })

      const html = selector => factory.toHtml().querySelector(selector).textContent
      expect(html('.foo')).toEqual('bar')
      expect(html('.foo2')).toEqual('bar2')
      factory.changeContent({
        '.foo': 'test',
        '.foo2': 'test2'
      })

      expect(html('.foo')).toEqual('test')
      expect(html('.foo2')).toEqual('test2')
    })

    it('should change only the given, content', () => {
      const template = [
        '<div>',
        '  <div class="foo"></div>',
        '  <div class="foo2"></div>',
        '</div>'
      ].join('')

      const factory = new TemplateFactory({
        template,
        content: {
          '.foo': 'bar',
          '.foo2': 'bar2'
        }
      })

      const html = selector => factory.toHtml().querySelector(selector).textContent
      expect(html('.foo')).toEqual('bar')
      expect(html('.foo2')).toEqual('bar2')
      factory.changeContent({
        '.foo': 'test',
        '.wrong': 'wrong'
      })

      expect(html('.foo')).toEqual('test')
      expect(html('.foo2')).toEqual('bar2')
    })
  })
})