|
| 1 | +var Vue = require('../../../../src/vue') |
| 2 | +var _ = require('../../../../src/util') |
| 3 | +var compiler = require('../../../../src/compiler') |
| 4 | + |
| 5 | +describe('Partial', function () { |
| 6 | + |
| 7 | + var el |
| 8 | + beforeEach(function () { |
| 9 | + el = document.createElement('div') |
| 10 | + }) |
| 11 | + |
| 12 | + it('static', function (done) { |
| 13 | + var vm = new Vue({ |
| 14 | + el: el, |
| 15 | + template: '<partial name="yo"></partial>', |
| 16 | + data: { |
| 17 | + msg: 'hello' |
| 18 | + }, |
| 19 | + partials: { |
| 20 | + yo: '{{msg}}' |
| 21 | + } |
| 22 | + }) |
| 23 | + expect(el.textContent).toBe('hello') |
| 24 | + vm.msg = 'what' |
| 25 | + _.nextTick(function () { |
| 26 | + expect(el.textContent).toBe('what') |
| 27 | + done() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('dynamic', function (done) { |
| 32 | + var vm = new Vue({ |
| 33 | + el: el, |
| 34 | + template: '<partial name="test-{{id}}"></partial>', |
| 35 | + data: { |
| 36 | + id: 'a', |
| 37 | + }, |
| 38 | + partials: { |
| 39 | + 'test-a': 'a {{id}}', |
| 40 | + 'test-b': 'b {{id}}' |
| 41 | + } |
| 42 | + }) |
| 43 | + expect(el.textContent).toBe('a a') |
| 44 | + vm.id = 'b' |
| 45 | + _.nextTick(function () { |
| 46 | + expect(el.textContent).toBe('b b') |
| 47 | + done() |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('caching', function () { |
| 52 | + var calls = 0 |
| 53 | + var compile = compiler.compile |
| 54 | + compiler.compile = function () { |
| 55 | + calls++ |
| 56 | + return compile.apply(this, arguments) |
| 57 | + } |
| 58 | + var vm = new Vue({ |
| 59 | + el: el, |
| 60 | + template: |
| 61 | + '<partial name="cache-test"></partial> ' + |
| 62 | + '<partial name="cache-test"></partial>', |
| 63 | + data: { |
| 64 | + msg: 'hi' |
| 65 | + }, |
| 66 | + partials: { |
| 67 | + 'cache-test': 'cache-test {{msg}}' |
| 68 | + } |
| 69 | + }) |
| 70 | + expect(el.textContent).toBe('cache-test hi cache-test hi') |
| 71 | + // one call for instance, and one for partial |
| 72 | + expect(calls).toBe(2) |
| 73 | + // cleanup |
| 74 | + compiler.compile = compile |
| 75 | + }) |
| 76 | + |
| 77 | + it('teardown', function () { |
| 78 | + var vm = new Vue({ |
| 79 | + el: el, |
| 80 | + template: '<partial name="test-{{id}}"></partial>', |
| 81 | + data: { |
| 82 | + id: 'a', |
| 83 | + }, |
| 84 | + partials: { |
| 85 | + 'test-a': 'a {{id}}' |
| 86 | + } |
| 87 | + }) |
| 88 | + expect(vm._directives.length).toBe(2) |
| 89 | + expect(vm._directives[1].name).toBe('partial') |
| 90 | + expect(vm._watchers.length).toBe(2) |
| 91 | + vm._directives[1]._teardown() |
| 92 | + // the text-directive should've been removed. |
| 93 | + expect(vm._directives.length).toBe(1) |
| 94 | + expect(vm._directives[0].name).toBe('partial') |
| 95 | + expect(vm._watchers.length).toBe(0) |
| 96 | + }) |
| 97 | + |
| 98 | +}) |
0 commit comments