apiSetup.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { h, ref, reactive, isReactive, toRef, isRef } from 'v3'
  2. import { nextTick } from 'core/util'
  3. import { effect } from 'v3/reactivity/effect'
  4. import Vue from 'vue'
  5. function renderToString(comp: any) {
  6. const vm = new Vue(comp).$mount()
  7. return vm.$el.outerHTML
  8. }
  9. describe('api: setup context', () => {
  10. it('should expose return values to template render context', () => {
  11. const Comp = {
  12. setup() {
  13. return {
  14. // ref should auto-unwrap
  15. ref: ref('foo'),
  16. // object exposed as-is
  17. object: reactive({ msg: 'bar' }),
  18. // primitive value exposed as-is
  19. value: 'baz'
  20. }
  21. },
  22. render() {
  23. return h('div', `${this.ref} ${this.object.msg} ${this.value}`)
  24. }
  25. }
  26. expect(renderToString(Comp)).toMatch(`<div>foo bar baz</div>`)
  27. })
  28. it('should support returning render function', () => {
  29. const Comp = {
  30. setup() {
  31. return () => {
  32. return h('div', 'hello')
  33. }
  34. }
  35. }
  36. expect(renderToString(Comp)).toMatch(`<div>hello</div>`)
  37. })
  38. it('props', async () => {
  39. const count = ref(0)
  40. let dummy
  41. const Parent = {
  42. render: () => h(Child, { props: { count: count.value } })
  43. }
  44. const Child = {
  45. props: { count: Number },
  46. setup(props) {
  47. effect(() => {
  48. dummy = props.count
  49. })
  50. return () => h('div', props.count)
  51. }
  52. }
  53. const vm = new Vue(Parent).$mount()
  54. expect(vm.$el.outerHTML).toMatch(`<div>0</div>`)
  55. expect(dummy).toBe(0)
  56. // props should be reactive
  57. count.value++
  58. await nextTick()
  59. expect(vm.$el.outerHTML).toMatch(`<div>1</div>`)
  60. expect(dummy).toBe(1)
  61. })
  62. it('context.attrs', async () => {
  63. const toggle = ref(true)
  64. const Parent = {
  65. render: () =>
  66. h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
  67. }
  68. const Child = {
  69. // explicit empty props declaration
  70. // puts everything received in attrs
  71. // disable implicit fallthrough
  72. inheritAttrs: false,
  73. setup(_props: any, { attrs }: any) {
  74. return () => h('div', { attrs })
  75. }
  76. }
  77. const vm = new Vue(Parent).$mount()
  78. expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
  79. // should update even though it's not reactive
  80. toggle.value = false
  81. await nextTick()
  82. expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
  83. })
  84. // vuejs/core #4161
  85. it('context.attrs in child component slots', async () => {
  86. const toggle = ref(true)
  87. const Wrapper = {
  88. template: `<div><slot/></div>`
  89. }
  90. const Child = {
  91. inheritAttrs: false,
  92. setup(_: any, { attrs }: any) {
  93. return () => {
  94. return h(Wrapper, [h('div', { attrs })])
  95. }
  96. }
  97. }
  98. const Parent = {
  99. render: () =>
  100. h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
  101. }
  102. const vm = new Vue(Parent).$mount()
  103. expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
  104. // should update even though it's not reactive
  105. toggle.value = false
  106. await nextTick()
  107. expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
  108. })
  109. it('context.attrs in child component scoped slots', async () => {
  110. const toggle = ref(true)
  111. const Wrapper = {
  112. template: `<div><slot/></div>`
  113. }
  114. const Child = {
  115. inheritAttrs: false,
  116. setup(_: any, { attrs }: any) {
  117. return () => {
  118. return h(Wrapper, {
  119. scopedSlots: {
  120. default: () => h('div', { attrs })
  121. }
  122. })
  123. }
  124. }
  125. }
  126. const Parent = {
  127. render: () =>
  128. h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
  129. }
  130. const vm = new Vue(Parent).$mount()
  131. expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
  132. // should update even though it's not reactive
  133. toggle.value = false
  134. await nextTick()
  135. expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
  136. })
  137. it('context.slots', async () => {
  138. const id = ref('foo')
  139. const Child = {
  140. setup(_props: any, { slots }: any) {
  141. // #12672 behavior consistency with Vue 3: should be able to access
  142. // slots directly in setup()
  143. expect(slots.foo()).toBeTruthy()
  144. return () => h('div', [...slots.foo(), ...slots.bar()])
  145. }
  146. }
  147. const Parent = {
  148. components: { Child },
  149. setup() {
  150. return { id }
  151. },
  152. template: `<Child>
  153. <template #foo>{{ id }}</template>
  154. <template #bar>bar</template>
  155. </Child>`
  156. }
  157. const vm = new Vue(Parent).$mount()
  158. expect(vm.$el.outerHTML).toMatch(`<div>foobar</div>`)
  159. // should update even though it's not reactive
  160. id.value = 'baz'
  161. await nextTick()
  162. expect(vm.$el.outerHTML).toMatch(`<div>bazbar</div>`)
  163. })
  164. it('context.emit', async () => {
  165. const count = ref(0)
  166. const spy = vi.fn()
  167. const Child = {
  168. props: {
  169. count: {
  170. type: Number,
  171. default: 1
  172. }
  173. },
  174. setup(props, { emit }) {
  175. return () =>
  176. h(
  177. 'div',
  178. {
  179. on: { click: () => emit('inc', props.count + 1) }
  180. },
  181. props.count
  182. )
  183. }
  184. }
  185. const Parent = {
  186. components: { Child },
  187. setup: () => ({
  188. count,
  189. onInc(newVal: number) {
  190. spy()
  191. count.value = newVal
  192. }
  193. }),
  194. template: `<Child :count="count" @inc="onInc" />`
  195. }
  196. const vm = new Vue(Parent).$mount()
  197. expect(vm.$el.outerHTML).toMatch(`<div>0</div>`)
  198. // emit should trigger parent handler
  199. triggerEvent(vm.$el as HTMLElement, 'click')
  200. expect(spy).toHaveBeenCalled()
  201. await nextTick()
  202. expect(vm.$el.outerHTML).toMatch(`<div>1</div>`)
  203. })
  204. it('directive resolution', () => {
  205. const spy = vi.fn()
  206. new Vue({
  207. setup: () => ({
  208. __sfc: true,
  209. vDir: {
  210. inserted: spy
  211. }
  212. }),
  213. template: `<div v-dir />`
  214. }).$mount()
  215. expect(spy).toHaveBeenCalled()
  216. })
  217. // #12743
  218. it('directive resolution for shorthand', () => {
  219. const spy = vi.fn()
  220. new Vue({
  221. setup: () => ({
  222. __sfc: true,
  223. vDir: spy
  224. }),
  225. template: `<div v-dir />`
  226. }).$mount()
  227. expect(spy).toHaveBeenCalled()
  228. })
  229. // #12561
  230. it('setup props should be reactive', () => {
  231. const msg = ref('hi')
  232. const Child = {
  233. props: ['msg'],
  234. setup: props => {
  235. expect(isReactive(props)).toBe(true)
  236. expect(isRef(toRef(props, 'foo'))).toBe(true)
  237. return () => {}
  238. }
  239. }
  240. new Vue({
  241. setup() {
  242. return h => h(Child, { props: { msg } })
  243. }
  244. }).$mount()
  245. })
  246. it('should not track dep accessed in setup', async () => {
  247. const spy = vi.fn()
  248. const msg = ref('hi')
  249. const Child = {
  250. setup: () => {
  251. msg.value
  252. return () => {}
  253. }
  254. }
  255. new Vue({
  256. setup() {
  257. return h => {
  258. spy()
  259. return h(Child)
  260. }
  261. }
  262. }).$mount()
  263. expect(spy).toHaveBeenCalledTimes(1)
  264. msg.value = 'bye'
  265. await nextTick()
  266. expect(spy).toHaveBeenCalledTimes(1)
  267. })
  268. it('context.listeners', async () => {
  269. let _listeners
  270. const Child = {
  271. setup(_, { listeners }) {
  272. _listeners = listeners
  273. return () => {}
  274. }
  275. }
  276. const Parent = {
  277. data: () => ({ log: () => 1 }),
  278. template: `<Child @foo="log" />`,
  279. components: { Child }
  280. }
  281. const vm = new Vue(Parent).$mount()
  282. expect(_listeners.foo()).toBe(1)
  283. vm.log = () => 2
  284. await nextTick()
  285. expect(_listeners.foo()).toBe(2)
  286. })
  287. })