text_spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var textParser = require('../../../../src/parse/text')
  2. var config = require('../../../../src/config')
  3. var Vue = require('../../../../src/vue')
  4. var testCases = [
  5. {
  6. // no tags
  7. text: 'haha',
  8. expected: null
  9. },
  10. {
  11. // basic
  12. text: 'a {{ a }} c',
  13. expected: [
  14. { value: 'a ' },
  15. { tag: true, value: 'a', html: false, oneTime: false },
  16. { value: ' c' }
  17. ]
  18. },
  19. {
  20. // html
  21. text: '{{ text }} and {{{ html }}}',
  22. expected: [
  23. { tag: true, value: 'text', html: false, oneTime: false },
  24. { value: ' and ' },
  25. { tag: true, value: 'html', html: true, oneTime: false },
  26. ]
  27. },
  28. {
  29. // one time
  30. text: '{{* text }} and {{{* html }}}',
  31. expected: [
  32. { tag: true, value: 'text', html: false, oneTime: true },
  33. { value: ' and ' },
  34. { tag: true, value: 'html', html: true, oneTime: true },
  35. ]
  36. },
  37. {
  38. // partial
  39. text: '{{> hello }} and {{>hello}}',
  40. expected: [
  41. { tag: true, value: 'hello', html: false, oneTime: false, partial: true },
  42. { value: ' and ' },
  43. { tag: true, value: 'hello', html: false, oneTime: false, partial: true }
  44. ]
  45. }
  46. ]
  47. function assertParse (test) {
  48. var res = textParser.parse(test.text)
  49. var exp = test.expected
  50. if (!Array.isArray(exp)) {
  51. expect(res).toBe(exp)
  52. } else {
  53. expect(res.length).toBe(exp.length)
  54. res.forEach(function (r, i) {
  55. var e = exp[i]
  56. for (var key in e) {
  57. expect(e[key]).toEqual(r[key])
  58. }
  59. })
  60. }
  61. }
  62. describe('Text Parser', function () {
  63. it('parse', function () {
  64. testCases.forEach(assertParse)
  65. })
  66. it('cache', function () {
  67. var res1 = textParser.parse('{{a}}')
  68. var res2 = textParser.parse('{{a}}')
  69. expect(res1).toBe(res2)
  70. })
  71. it('custom delimiters', function () {
  72. config.delimiters = ['[%', '%]']
  73. assertParse({
  74. text: '[%* text %] and [[% html %]]',
  75. expected: [
  76. { tag: true, value: 'text', html: false, oneTime: true },
  77. { value: ' and ' },
  78. { tag: true, value: 'html', html: true, oneTime: false },
  79. ]
  80. })
  81. config.delimiters = ['{{', '}}']
  82. })
  83. it('tokens to expression', function () {
  84. var tokens = textParser.parse('view-{{test}}-test-{{ok}}')
  85. var exp = textParser.tokensToExp(tokens)
  86. expect(exp).toBe('"view-"+test+"-test-"+ok')
  87. })
  88. it('tokens to expression with oneTime tags & vm', function () {
  89. var vm = new Vue({
  90. data: { test: 'a', ok: 'b' }
  91. })
  92. var tokens = textParser.parse('view-{{*test}}-test-{{ok}}')
  93. var exp = textParser.tokensToExp(tokens, vm)
  94. expect(exp).toBe('"view-"+"a"+"-test-"+ok')
  95. })
  96. })