functional.spec.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. document.body.appendChild(vm.$el)
  68. triggerEvent(vm.$el.children[0], 'click')
  69. expect(foo).toHaveBeenCalled()
  70. expect(foo.calls.argsFor(0)[0].type).toBe('click') // should have click event
  71. triggerEvent(vm.$el.children[0], 'mousedown')
  72. expect(bar).toHaveBeenCalledWith('bar')
  73. document.body.removeChild(vm.$el)
  74. })
  75. it('should expose scopedSlots on render context', () => {
  76. const vm = new Vue({
  77. template: '<div><wrap>foo<p slot="p" slot-scope="a">{{ a }}</p></wrap></div>',
  78. components: {
  79. wrap: {
  80. functional: true,
  81. render (h, { scopedSlots }) {
  82. return [
  83. // scoped
  84. scopedSlots.p('a'),
  85. // normal slot content should be exposed as well
  86. scopedSlots.default()
  87. ]
  88. }
  89. }
  90. }
  91. }).$mount()
  92. expect(vm.$el.textContent).toBe('afoo')
  93. })
  94. it('should support returning more than one root node', () => {
  95. const vm = new Vue({
  96. template: `<div><test></test></div>`,
  97. components: {
  98. test: {
  99. functional: true,
  100. render (h) {
  101. return [h('span', 'foo'), h('span', 'bar')]
  102. }
  103. }
  104. }
  105. }).$mount()
  106. expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
  107. })
  108. it('should support slots', () => {
  109. const vm = new Vue({
  110. data: { test: 'foo' },
  111. template: '<div><wrap><div slot="a">foo</div><div slot="b">bar</div></wrap></div>',
  112. components: {
  113. wrap: {
  114. functional: true,
  115. props: ['msg'],
  116. render (h, { slots }) {
  117. slots = slots()
  118. return h('div', null, [slots.b, slots.a])
  119. }
  120. }
  121. }
  122. }).$mount()
  123. expect(vm.$el.innerHTML).toBe('<div><div>bar</div><div>foo</div></div>')
  124. })
  125. it('should let vnode raw data pass through', done => {
  126. const onValid = jasmine.createSpy('valid')
  127. const vm = new Vue({
  128. data: { msg: 'hello' },
  129. template: `<div>
  130. <validate field="field1" @valid="onValid">
  131. <input type="text" v-model="msg">
  132. </validate>
  133. </div>`,
  134. components: {
  135. validate: {
  136. functional: true,
  137. props: ['field'],
  138. render (h, { props, children, data: { on }}) {
  139. props.child = children[0]
  140. return h('validate-control', { props, on })
  141. }
  142. },
  143. 'validate-control': {
  144. props: ['field', 'child'],
  145. render () {
  146. return this.child
  147. },
  148. mounted () {
  149. this.$el.addEventListener('input', this.onInput)
  150. },
  151. destroyed () {
  152. this.$el.removeEventListener('input', this.onInput)
  153. },
  154. methods: {
  155. onInput (e) {
  156. const value = e.target.value
  157. if (this.validate(value)) {
  158. this.$emit('valid', this)
  159. }
  160. },
  161. // something validation logic here
  162. validate (val) {
  163. return val.length > 0
  164. }
  165. }
  166. }
  167. },
  168. methods: { onValid }
  169. }).$mount()
  170. document.body.appendChild(vm.$el)
  171. const input = vm.$el.querySelector('input')
  172. expect(onValid).not.toHaveBeenCalled()
  173. waitForUpdate(() => {
  174. input.value = 'foo'
  175. triggerEvent(input, 'input')
  176. }).then(() => {
  177. expect(onValid).toHaveBeenCalled()
  178. }).then(() => {
  179. document.body.removeChild(vm.$el)
  180. vm.$destroy()
  181. }).then(done)
  182. })
  183. it('create empty vnode when render return null', () => {
  184. const child = {
  185. functional: true,
  186. render () {
  187. return null
  188. }
  189. }
  190. const vm = new Vue({
  191. components: {
  192. child
  193. }
  194. })
  195. const h = vm.$createElement
  196. const vnode = h('child')
  197. expect(vnode).toEqual(createEmptyVNode())
  198. })
  199. // #7282
  200. it('should normalize top-level arrays', () => {
  201. const Foo = {
  202. functional: true,
  203. render (h) {
  204. return [h('span', 'hi'), null]
  205. }
  206. }
  207. const vm = new Vue({
  208. template: `<div><foo/></div>`,
  209. components: { Foo }
  210. }).$mount()
  211. expect(vm.$el.innerHTML).toBe('<span>hi</span>')
  212. })
  213. it('should work when used as named slot and returning array', () => {
  214. const Foo = {
  215. template: `<div><slot name="test"/></div>`
  216. }
  217. const Bar = {
  218. functional: true,
  219. render: h => ([
  220. h('div', 'one'),
  221. h('div', 'two'),
  222. h(Baz)
  223. ])
  224. }
  225. const Baz = {
  226. functional: true,
  227. render: h => h('div', 'three')
  228. }
  229. const vm = new Vue({
  230. template: `<foo><bar slot="test"/></foo>`,
  231. components: { Foo, Bar }
  232. }).$mount()
  233. expect(vm.$el.innerHTML).toBe('<div>one</div><div>two</div><div>three</div>')
  234. })
  235. it('should apply namespace when returning arrays', () => {
  236. const Child = {
  237. functional: true,
  238. render: h => ([h('foo'), h('bar')])
  239. }
  240. const vm = new Vue({
  241. template: `<svg><child/></svg>`,
  242. components: { Child }
  243. }).$mount()
  244. expect(vm.$el.childNodes[0].namespaceURI).toContain('svg')
  245. expect(vm.$el.childNodes[1].namespaceURI).toContain('svg')
  246. })
  247. it('should work with render fns compiled from template', done => {
  248. // code generated via vue-template-es2015-compiler
  249. const render = function (_h, _vm) {
  250. const _c = _vm._c
  251. return _c(
  252. 'div',
  253. [
  254. _c('h2', { staticClass: 'red' }, [_vm._v(_vm._s(_vm.props.msg))]),
  255. _vm._t('default'),
  256. _vm._t('slot2'),
  257. _vm._t('scoped', null, { msg: _vm.props.msg }),
  258. _vm._m(0),
  259. _c('div', { staticClass: 'clickable', on: { click: _vm.parent.fn }}, [
  260. _vm._v('click me')
  261. ])
  262. ],
  263. 2
  264. )
  265. }
  266. const staticRenderFns = [
  267. function (_h, _vm) {
  268. const _c = _vm._c
  269. return _c('div', [_vm._v('Some '), _c('span', [_vm._v('text')])])
  270. }
  271. ]
  272. const child = {
  273. functional: true,
  274. _compiled: true,
  275. render,
  276. staticRenderFns
  277. }
  278. const parent = new Vue({
  279. components: {
  280. child
  281. },
  282. data: {
  283. msg: 'hello'
  284. },
  285. template: `
  286. <div>
  287. <child :msg="msg">
  288. <span>{{ msg }}</span>
  289. <div slot="slot2">Second slot</div>
  290. <template slot="scoped" slot-scope="scope">{{ scope.msg }}</template>
  291. </child>
  292. </div>
  293. `,
  294. methods: {
  295. fn () {
  296. this.msg = 'bye'
  297. }
  298. }
  299. }).$mount()
  300. function assertMarkup () {
  301. expect(parent.$el.innerHTML).toBe(
  302. `<div>` +
  303. `<h2 class="red">${parent.msg}</h2>` +
  304. `<span>${parent.msg}</span> ` +
  305. `<div>Second slot</div>` +
  306. parent.msg +
  307. // static
  308. `<div>Some <span>text</span></div>` +
  309. `<div class="clickable">click me</div>` +
  310. `</div>`
  311. )
  312. }
  313. assertMarkup()
  314. triggerEvent(parent.$el.querySelector('.clickable'), 'click')
  315. waitForUpdate(assertMarkup).then(done)
  316. })
  317. // #8468
  318. it('should normalize nested arrays when use functional components with v-for', () => {
  319. const Foo = {
  320. functional: true,
  321. props: {
  322. name: {}
  323. },
  324. render (h, context) {
  325. return [h('span', 'hi'), h('span', context.props.name)]
  326. }
  327. }
  328. const vm = new Vue({
  329. template: `<div><foo v-for="name in names" :name="name" /></div>`,
  330. data: {
  331. names: ['foo', 'bar']
  332. },
  333. components: { Foo }
  334. }).$mount()
  335. expect(vm.$el.innerHTML).toBe('<span>hi</span><span>foo</span><span>hi</span><span>bar</span>')
  336. })
  337. })