| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- var _ = require('../../../../../src/util')
- var Vue = require('../../../../../src/vue')
- if (_.inBrowser) {
- describe('prop', function () {
- var el
- beforeEach(function () {
- el = document.createElement('div')
- spyOn(_, 'warn')
- })
- it('one way binding', function (done) {
- var vm = new Vue({
- el: el,
- data: {
- b: 'B'
- },
- template: '<test v-bind:b="b" v-ref:child></test>',
- components: {
- test: {
- props: ['b'],
- template: '{{b}}'
- }
- }
- })
- expect(el.innerHTML).toBe('<test>B</test>')
- vm.b = 'BB'
- _.nextTick(function () {
- expect(el.innerHTML).toBe('<test>BB</test>')
- vm.$refs.child.b = 'BBB'
- expect(vm.b).toBe('BB')
- _.nextTick(function () {
- expect(el.innerHTML).toBe('<test>BBB</test>')
- done()
- })
- })
- })
- it('with filters', function (done) {
- var vm = new Vue({
- el: el,
- template: '<test :name="a | test"></test>',
- data: {
- a: 123
- },
- filters: {
- test: function (v) {
- return v * 2
- }
- },
- components: {
- test: {
- props: ['name'],
- template: '{{name}}'
- }
- }
- })
- expect(el.textContent).toBe('246')
- vm.a = 234
- _.nextTick(function () {
- expect(el.textContent).toBe('468')
- done()
- })
- })
- it('two-way binding', function (done) {
- var vm = new Vue({
- el: el,
- data: {
- b: 'B',
- test: {
- a: 'A'
- }
- },
- template: '<test v-bind:testt.sync="test" :bb.sync="b" :a.sync=" test.a " v-ref:child></test>',
- components: {
- test: {
- props: ['testt', 'bb', 'a'],
- template: '{{testt.a}} {{bb}} {{a}}'
- }
- }
- })
- expect(el.firstChild.textContent).toBe('A B A')
- vm.test.a = 'AA'
- vm.b = 'BB'
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('AA BB AA')
- vm.test = { a: 'AAA' }
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('AAA BB AAA')
- vm.$data = {
- b: 'BBB',
- test: {
- a: 'AAAA'
- }
- }
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('AAAA BBB AAAA')
- // test two-way
- vm.$refs.child.bb = 'B'
- vm.$refs.child.testt = { a: 'A' }
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('A B A')
- expect(vm.test.a).toBe('A')
- expect(vm.test).toBe(vm.$refs.child.testt)
- expect(vm.b).toBe('B')
- vm.$refs.child.a = 'Oops'
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('Oops B Oops')
- expect(vm.test.a).toBe('Oops')
- done()
- })
- })
- })
- })
- })
- })
- it('explicit one time binding', function (done) {
- var vm = new Vue({
- el: el,
- data: {
- b: 'B'
- },
- template: '<test :b.once="b" v-ref:child></test>',
- components: {
- test: {
- props: ['b'],
- template: '{{b}}'
- }
- }
- })
- expect(el.innerHTML).toBe('<test>B</test>')
- vm.b = 'BB'
- _.nextTick(function () {
- expect(el.innerHTML).toBe('<test>B</test>')
- done()
- })
- })
- it('warn non-settable parent path', function (done) {
- var vm = new Vue({
- el: el,
- data: {
- b: 'B'
- },
- template: '<test :b.sync=" b + \'B\'" v-ref:child></test>',
- components: {
- test: {
- props: ['b'],
- template: '{{b}}'
- }
- }
- })
- expect(hasWarned(_, 'Cannot bind two-way prop with non-settable parent path')).toBe(true)
- expect(el.innerHTML).toBe('<test>BB</test>')
- vm.b = 'BB'
- _.nextTick(function () {
- expect(el.innerHTML).toBe('<test>BBB</test>')
- vm.$refs.child.b = 'hahaha'
- _.nextTick(function () {
- expect(vm.b).toBe('BB')
- expect(el.innerHTML).toBe('<test>hahaha</test>')
- done()
- })
- })
- })
- it('warn expect two-way', function () {
- new Vue({
- el: el,
- template: '<test :test="ok"></test>',
- data: {
- ok: 'hi'
- },
- components: {
- test: {
- props: {
- test: {
- twoWay: true
- }
- }
- }
- }
- })
- expect(hasWarned(_, 'expects a two-way binding type')).toBe(true)
- })
- it('warn $data as prop', function () {
- new Vue({
- el: el,
- template: '<test></test>',
- data: {
- ok: 'hi'
- },
- components: {
- test: {
- props: ['$data']
- }
- }
- })
- expect(hasWarned(_, 'Do not use $data as prop')).toBe(true)
- })
- it('warn invalid keys', function () {
- new Vue({
- el: el,
- template: '<test :a.b.c="test"></test>',
- components: {
- test: {
- props: ['a.b.c']
- }
- }
- })
- expect(hasWarned(_, 'Invalid prop key')).toBe(true)
- })
- it('warn props with no el option', function () {
- new Vue({
- props: ['a']
- })
- expect(hasWarned(_, 'Props will not be compiled if no `el`')).toBe(true)
- })
- it('warn object/array default values', function () {
- new Vue({
- el: el,
- props: {
- arr: {
- type: Array,
- default: []
- },
- obj: {
- type: Object,
- default: {}
- }
- }
- })
- expect(hasWarned(_, 'Use a factory function to return the default value')).toBe(true)
- expect(_.warn.calls.count()).toBe(2)
- })
- it('teardown', function (done) {
- var vm = new Vue({
- el: el,
- data: {
- a: 'A',
- b: 'B'
- },
- template: '<test :aa.sync="a" :bb="b"></test>',
- components: {
- test: {
- props: ['aa', 'bb'],
- template: '{{aa}} {{bb}}'
- }
- }
- })
- var child = vm.$children[0]
- expect(el.firstChild.textContent).toBe('A B')
- child.aa = 'AA'
- vm.b = 'BB'
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('AA BB')
- expect(vm.a).toBe('AA')
- // unbind the two props
- child._directives[0].unbind()
- child._directives[1].unbind()
- child.aa = 'AAA'
- vm.b = 'BBB'
- _.nextTick(function () {
- expect(el.firstChild.textContent).toBe('AAA BB')
- expect(vm.a).toBe('AA')
- done()
- })
- })
- })
- it('block instance with replace:true', function () {
- new Vue({
- el: el,
- template: '<test :b="a" :c="d"></test>',
- data: {
- a: 'AAA',
- d: 'DDD'
- },
- components: {
- test: {
- props: ['b', 'c'],
- template: '<p>{{b}}</p><p>{{c}}</p>',
- replace: true
- }
- }
- })
- expect(el.innerHTML).toBe('<p>AAA</p><p>DDD</p>')
- })
- describe('assertions', function () {
- function makeInstance (value, type, validator) {
- return new Vue({
- el: document.createElement('div'),
- template: '<test :test="val"></test>',
- data: {
- val: value
- },
- components: {
- test: {
- props: [
- {
- name: 'test',
- type: type,
- validator: validator
- }
- ]
- }
- }
- })
- }
- it('string', function () {
- makeInstance('hello', String)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance(123, String)
- expect(hasWarned(_, 'Expected String')).toBe(true)
- })
- it('number', function () {
- makeInstance(123, Number)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance('123', Number)
- expect(hasWarned(_, 'Expected Number')).toBe(true)
- })
- it('boolean', function () {
- makeInstance(true, Boolean)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance('123', Boolean)
- expect(hasWarned(_, 'Expected Boolean')).toBe(true)
- })
- it('function', function () {
- makeInstance(function () {}, Function)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance(123, Function)
- expect(hasWarned(_, 'Expected Function')).toBe(true)
- })
- it('object', function () {
- makeInstance({}, Object)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance([], Object)
- expect(hasWarned(_, 'Expected Object')).toBe(true)
- })
- it('array', function () {
- makeInstance([], Array)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance({}, Array)
- expect(hasWarned(_, 'Expected Array')).toBe(true)
- })
- it('custom constructor', function () {
- function Class () {}
- makeInstance(new Class(), Class)
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance({}, Class)
- expect(hasWarned(_, 'Expected custom type')).toBe(true)
- })
- it('custom validator', function () {
- makeInstance(123, null, function (v) {
- return v === 123
- })
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance(123, null, function (v) {
- return v === 234
- })
- expect(hasWarned(_, 'custom validator check failed')).toBe(true)
- })
- it('type check + custom validator', function () {
- makeInstance(123, Number, function (v) {
- return v === 123
- })
- expect(_.warn).not.toHaveBeenCalled()
- makeInstance(123, Number, function (v) {
- return v === 234
- })
- expect(hasWarned(_, 'custom validator check failed')).toBe(true)
- makeInstance(123, String, function (v) {
- return v === 123
- })
- expect(hasWarned(_, 'Expected String')).toBe(true)
- })
- it('required', function () {
- new Vue({
- el: document.createElement('div'),
- template: '<test></test>',
- components: {
- test: {
- props: [
- {
- name: 'prop',
- required: true
- }
- ]
- }
- }
- })
- expect(hasWarned(_, 'Missing required prop')).toBe(true)
- })
- })
- it('alternative syntax', function () {
- new Vue({
- el: el,
- template: '<test :b="a" :c="d"></test>',
- data: {
- a: 'AAA',
- d: 'DDD'
- },
- components: {
- test: {
- props: {
- b: String,
- c: {
- type: Number
- },
- d: {
- required: true
- }
- },
- template: '<p>{{b}}</p><p>{{c}}</p>'
- }
- }
- })
- expect(hasWarned(_, 'Missing required prop')).toBe(true)
- expect(hasWarned(_, 'Expected Number')).toBe(true)
- expect(el.textContent).toBe('AAA')
- })
- it('should not overwrite default value for an absent Boolean prop', function () {
- var vm = new Vue({
- el: el,
- template: '<test></test>',
- components: {
- test: {
- props: {
- prop: Boolean
- },
- data: function () {
- return {
- prop: true
- }
- },
- template: '{{prop}}'
- }
- }
- })
- expect(vm.$children[0].prop).toBe(true)
- expect(vm.$el.textContent).toBe('true')
- expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
- prop: true
- }))
- })
- it('should respect default value of a Boolean prop', function () {
- var vm = new Vue({
- el: el,
- template: '<test></test>',
- components: {
- test: {
- props: {
- prop: {
- type: Boolean,
- default: true
- }
- },
- template: '{{prop}}'
- }
- }
- })
- expect(vm.$el.textContent).toBe('true')
- })
- it('should initialize with default value when not provided & has default data', function (done) {
- var vm = new Vue({
- el: el,
- template: '<test></test>',
- components: {
- test: {
- props: {
- prop: {
- type: String,
- default: 'hello'
- }
- },
- data: function () {
- return {
- other: 'world'
- }
- },
- template: '{{prop}} {{other}}'
- }
- }
- })
- expect(vm.$el.textContent).toBe('hello world')
- vm.$children[0].prop = 'bye'
- _.nextTick(function () {
- expect(vm.$el.textContent).toBe('bye world')
- done()
- })
- })
- it('should warn data fields already defined as a prop', function () {
- new Vue({
- el: el,
- props: {
- a: null
- },
- data: {
- a: 1
- }
- })
- expect(hasWarned(_, 'already defined as a prop')).toBe(true)
- })
- it('should not warn for non-required, absent prop', function () {
- new Vue({
- el: el,
- template: '<test></test>',
- components: {
- test: {
- props: {
- prop: {
- type: String
- }
- }
- }
- }
- })
- expect(_.warn).not.toHaveBeenCalled()
- })
- })
- }
|