functional.spec.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import Vue from 'vue'
  2. import { createEmptyVNode } from 'core/vdom/vnode'
  3. describe('Options functional', () => {
  4. it('should work', done => {
  5. const vm = new Vue({
  6. data: { test: 'foo' },
  7. template: '<div><wrap :msg="test">bar</wrap></div>',
  8. components: {
  9. wrap: {
  10. functional: true,
  11. props: ['msg'],
  12. render (h, { props, children }) {
  13. return h('div', null, [props.msg, ' '].concat(children))
  14. }
  15. }
  16. }
  17. }).$mount()
  18. expect(vm.$el.innerHTML).toBe('<div>foo bar</div>')
  19. vm.test = 'qux'
  20. waitForUpdate(() => {
  21. expect(vm.$el.innerHTML).toBe('<div>qux bar</div>')
  22. }).then(done)
  23. })
  24. it('should expose all props when not declared', done => {
  25. const fn = {
  26. functional: true,
  27. render (h, { props }) {
  28. return h('div', `${props.msg} ${props.kebabMsg}`)
  29. }
  30. }
  31. const vm = new Vue({
  32. data: { test: 'foo' },
  33. render (h) {
  34. return h('div', [
  35. h(fn, {
  36. props: { msg: this.test },
  37. attrs: { 'kebab-msg': 'bar' }
  38. })
  39. ])
  40. }
  41. }).$mount()
  42. expect(vm.$el.innerHTML).toBe('<div>foo bar</div>')
  43. vm.test = 'qux'
  44. waitForUpdate(() => {
  45. expect(vm.$el.innerHTML).toBe('<div>qux bar</div>')
  46. }).then(done)
  47. })
  48. it('should expose data.on as listeners', () => {
  49. const foo = jasmine.createSpy('foo')
  50. const bar = jasmine.createSpy('bar')
  51. const vm = new Vue({
  52. template: '<div><wrap @click="foo" @test="bar"/></div>',
  53. methods: { foo, bar },
  54. components: {
  55. wrap: {
  56. functional: true,
  57. render (h, { listeners }) {
  58. return h('div', {
  59. on: {
  60. click: [listeners.click, () => listeners.test('bar')]
  61. }
  62. })
  63. }
  64. }
  65. }
  66. }).$mount()
  67. triggerEvent(vm.$el.children[0], 'click')
  68. expect(foo).toHaveBeenCalled()
  69. expect(foo.calls.argsFor(0)[0].type).toBe('click') // should have click event
  70. triggerEvent(vm.$el.children[0], 'mousedown')
  71. expect(bar).toHaveBeenCalledWith('bar')
  72. })
  73. it('should support returning more than one root node', () => {
  74. const vm = new Vue({
  75. template: `<div><test></test></div>`,
  76. components: {
  77. test: {
  78. functional: true,
  79. render (h) {
  80. return [h('span', 'foo'), h('span', 'bar')]
  81. }
  82. }
  83. }
  84. }).$mount()
  85. expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
  86. })
  87. it('should support slots', () => {
  88. const vm = new Vue({
  89. data: { test: 'foo' },
  90. template: '<div><wrap><div slot="a">foo</div><div slot="b">bar</div></wrap></div>',
  91. components: {
  92. wrap: {
  93. functional: true,
  94. props: ['msg'],
  95. render (h, { slots }) {
  96. slots = slots()
  97. return h('div', null, [slots.b, slots.a])
  98. }
  99. }
  100. }
  101. }).$mount()
  102. expect(vm.$el.innerHTML).toBe('<div><div>bar</div><div>foo</div></div>')
  103. })
  104. it('should let vnode raw data pass through', done => {
  105. const onValid = jasmine.createSpy('valid')
  106. const vm = new Vue({
  107. data: { msg: 'hello' },
  108. template: `<div>
  109. <validate field="field1" @valid="onValid">
  110. <input type="text" v-model="msg">
  111. </validate>
  112. </div>`,
  113. components: {
  114. validate: {
  115. functional: true,
  116. props: ['field'],
  117. render (h, { props, children, data: { on }}) {
  118. props.child = children[0]
  119. return h('validate-control', { props, on })
  120. }
  121. },
  122. 'validate-control': {
  123. props: ['field', 'child'],
  124. render () {
  125. return this.child
  126. },
  127. mounted () {
  128. this.$el.addEventListener('input', this.onInput)
  129. },
  130. destroyed () {
  131. this.$el.removeEventListener('input', this.onInput)
  132. },
  133. methods: {
  134. onInput (e) {
  135. const value = e.target.value
  136. if (this.validate(value)) {
  137. this.$emit('valid', this)
  138. }
  139. },
  140. // something validation logic here
  141. validate (val) {
  142. return val.length > 0
  143. }
  144. }
  145. }
  146. },
  147. methods: { onValid }
  148. }).$mount()
  149. document.body.appendChild(vm.$el)
  150. const input = vm.$el.querySelector('input')
  151. expect(onValid).not.toHaveBeenCalled()
  152. waitForUpdate(() => {
  153. input.value = 'foo'
  154. triggerEvent(input, 'input')
  155. }).then(() => {
  156. expect(onValid).toHaveBeenCalled()
  157. }).then(() => {
  158. document.body.removeChild(vm.$el)
  159. vm.$destroy()
  160. }).then(done)
  161. })
  162. it('create empty vnode when render return null', () => {
  163. const child = {
  164. functional: true,
  165. render () {
  166. return null
  167. }
  168. }
  169. const vm = new Vue({
  170. components: {
  171. child
  172. }
  173. })
  174. const h = vm.$createElement
  175. const vnode = h('child')
  176. expect(vnode).toEqual(createEmptyVNode())
  177. })
  178. // #7282
  179. it('should normalize top-level arrays', () => {
  180. const Foo = {
  181. functional: true,
  182. render (h) {
  183. return [h('span', 'hi'), null]
  184. }
  185. }
  186. const vm = new Vue({
  187. template: `<div><foo/></div>`,
  188. components: { Foo }
  189. }).$mount()
  190. expect(vm.$el.innerHTML).toBe('<span>hi</span>')
  191. })
  192. it('should work when used as named slot and returning array', () => {
  193. const Foo = {
  194. template: `<div><slot name="test"/></div>`
  195. }
  196. const Bar = {
  197. functional: true,
  198. render: h => ([
  199. h('div', 'one'),
  200. h('div', 'two'),
  201. h(Baz)
  202. ])
  203. }
  204. const Baz = {
  205. functional: true,
  206. render: h => h('div', 'three')
  207. }
  208. const vm = new Vue({
  209. template: `<foo><bar slot="test"/></foo>`,
  210. components: { Foo, Bar }
  211. }).$mount()
  212. expect(vm.$el.innerHTML).toBe('<div>one</div><div>two</div><div>three</div>')
  213. })
  214. it('should apply namespace when returning arrays', () => {
  215. const Child = {
  216. functional: true,
  217. render: h => ([h('foo'), h('bar')])
  218. }
  219. const vm = new Vue({
  220. template: `<svg><child/></svg>`,
  221. components: { Child }
  222. }).$mount()
  223. expect(vm.$el.childNodes[0].namespaceURI).toContain('svg')
  224. expect(vm.$el.childNodes[1].namespaceURI).toContain('svg')
  225. })
  226. it('should work with render fns compiled from template', done => {
  227. // code generated via vue-template-es2015-compiler
  228. var render = function (_h, _vm) {
  229. var _c = _vm._c
  230. return _c(
  231. 'div',
  232. [
  233. _c('h2', { staticClass: 'red' }, [_vm._v(_vm._s(_vm.props.msg))]),
  234. _vm._t('default'),
  235. _vm._t('slot2'),
  236. _vm._t('scoped', null, { msg: _vm.props.msg }),
  237. _vm._m(0),
  238. _c('div', { staticClass: 'clickable', on: { click: _vm.parent.fn }}, [
  239. _vm._v('click me')
  240. ])
  241. ],
  242. 2
  243. )
  244. }
  245. var staticRenderFns = [
  246. function (_h, _vm) {
  247. var _c = _vm._c
  248. return _c('div', [_vm._v('Some '), _c('span', [_vm._v('text')])])
  249. }
  250. ]
  251. const child = {
  252. functional: true,
  253. _compiled: true,
  254. render,
  255. staticRenderFns
  256. }
  257. const parent = new Vue({
  258. components: {
  259. child
  260. },
  261. data: {
  262. msg: 'hello'
  263. },
  264. template: `
  265. <div>
  266. <child :msg="msg">
  267. <span>{{ msg }}</span>
  268. <div slot="slot2">Second slot</div>
  269. <template slot="scoped" slot-scope="scope">{{ scope.msg }}</template>
  270. </child>
  271. </div>
  272. `,
  273. methods: {
  274. fn () {
  275. this.msg = 'bye'
  276. }
  277. }
  278. }).$mount()
  279. function assertMarkup () {
  280. expect(parent.$el.innerHTML).toBe(
  281. `<div>` +
  282. `<h2 class="red">${parent.msg}</h2>` +
  283. `<span>${parent.msg}</span> ` +
  284. `<div>Second slot</div>` +
  285. parent.msg +
  286. // static
  287. `<div>Some <span>text</span></div>` +
  288. `<div class="clickable">click me</div>` +
  289. `</div>`
  290. )
  291. }
  292. assertMarkup()
  293. triggerEvent(parent.$el.querySelector('.clickable'), 'click')
  294. waitForUpdate(assertMarkup).then(done)
  295. })
  296. })