apiSetupHelpers.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import {
  2. ComponentInternalInstance,
  3. createApp,
  4. defineComponent,
  5. getCurrentInstance,
  6. h,
  7. nodeOps,
  8. onMounted,
  9. render,
  10. serializeInner,
  11. SetupContext,
  12. Suspense
  13. } from '@vue/runtime-test'
  14. import {
  15. defineEmits,
  16. defineProps,
  17. defineExpose,
  18. withDefaults,
  19. useAttrs,
  20. useSlots,
  21. mergeDefaults,
  22. withAsyncContext
  23. } from '../src/apiSetupHelpers'
  24. describe('SFC <script setup> helpers', () => {
  25. test('should warn runtime usage', () => {
  26. defineProps()
  27. expect(`defineProps() is a compiler-hint`).toHaveBeenWarned()
  28. defineEmits()
  29. expect(`defineEmits() is a compiler-hint`).toHaveBeenWarned()
  30. defineExpose()
  31. expect(`defineExpose() is a compiler-hint`).toHaveBeenWarned()
  32. withDefaults({}, {})
  33. expect(`withDefaults() is a compiler-hint`).toHaveBeenWarned()
  34. })
  35. test('useSlots / useAttrs (no args)', () => {
  36. let slots: SetupContext['slots'] | undefined
  37. let attrs: SetupContext['attrs'] | undefined
  38. const Comp = {
  39. setup() {
  40. slots = useSlots()
  41. attrs = useAttrs()
  42. return () => {}
  43. }
  44. }
  45. const passedAttrs = { id: 'foo' }
  46. const passedSlots = {
  47. default: () => {},
  48. x: () => {}
  49. }
  50. render(h(Comp, passedAttrs, passedSlots), nodeOps.createElement('div'))
  51. expect(typeof slots!.default).toBe('function')
  52. expect(typeof slots!.x).toBe('function')
  53. expect(attrs).toMatchObject(passedAttrs)
  54. })
  55. test('useSlots / useAttrs (with args)', () => {
  56. let slots: SetupContext['slots'] | undefined
  57. let attrs: SetupContext['attrs'] | undefined
  58. let ctx: SetupContext | undefined
  59. const Comp = defineComponent({
  60. setup(_, _ctx) {
  61. slots = useSlots()
  62. attrs = useAttrs()
  63. ctx = _ctx
  64. return () => {}
  65. }
  66. })
  67. render(h(Comp), nodeOps.createElement('div'))
  68. expect(slots).toBe(ctx!.slots)
  69. expect(attrs).toBe(ctx!.attrs)
  70. })
  71. test('mergeDefaults', () => {
  72. const merged = mergeDefaults(
  73. {
  74. foo: null,
  75. bar: { type: String, required: false }
  76. },
  77. {
  78. foo: 1,
  79. bar: 'baz'
  80. }
  81. )
  82. expect(merged).toMatchObject({
  83. foo: { default: 1 },
  84. bar: { type: String, required: false, default: 'baz' }
  85. })
  86. mergeDefaults({}, { foo: 1 })
  87. expect(
  88. `props default key "foo" has no corresponding declaration`
  89. ).toHaveBeenWarned()
  90. })
  91. describe('withAsyncContext', () => {
  92. // disable options API because applyOptions() also resets currentInstance
  93. // and we want to ensure the logic works even with Options API disabled.
  94. beforeEach(() => {
  95. __FEATURE_OPTIONS_API__ = false
  96. })
  97. afterEach(() => {
  98. __FEATURE_OPTIONS_API__ = true
  99. })
  100. test('basic', async () => {
  101. const spy = jest.fn()
  102. let beforeInstance: ComponentInternalInstance | null = null
  103. let afterInstance: ComponentInternalInstance | null = null
  104. let resolve: (msg: string) => void
  105. const Comp = defineComponent({
  106. async setup() {
  107. beforeInstance = getCurrentInstance()
  108. const msg = await withAsyncContext(
  109. new Promise(r => {
  110. resolve = r
  111. })
  112. )
  113. // register the lifecycle after an await statement
  114. onMounted(spy)
  115. afterInstance = getCurrentInstance()
  116. return () => msg
  117. }
  118. })
  119. const root = nodeOps.createElement('div')
  120. render(h(() => h(Suspense, () => h(Comp))), root)
  121. expect(spy).not.toHaveBeenCalled()
  122. resolve!('hello')
  123. // wait a macro task tick for all micro ticks to resolve
  124. await new Promise(r => setTimeout(r))
  125. // mount hook should have been called
  126. expect(spy).toHaveBeenCalled()
  127. // should retain same instance before/after the await call
  128. expect(beforeInstance).toBe(afterInstance)
  129. expect(serializeInner(root)).toBe('hello')
  130. })
  131. test('error handling', async () => {
  132. const spy = jest.fn()
  133. let beforeInstance: ComponentInternalInstance | null = null
  134. let afterInstance: ComponentInternalInstance | null = null
  135. let reject: () => void
  136. const Comp = defineComponent({
  137. async setup() {
  138. beforeInstance = getCurrentInstance()
  139. try {
  140. await withAsyncContext(
  141. new Promise((r, rj) => {
  142. reject = rj
  143. })
  144. )
  145. } catch (e) {
  146. // ignore
  147. }
  148. // register the lifecycle after an await statement
  149. onMounted(spy)
  150. afterInstance = getCurrentInstance()
  151. return () => ''
  152. }
  153. })
  154. const root = nodeOps.createElement('div')
  155. render(h(() => h(Suspense, () => h(Comp))), root)
  156. expect(spy).not.toHaveBeenCalled()
  157. reject!()
  158. // wait a macro task tick for all micro ticks to resolve
  159. await new Promise(r => setTimeout(r))
  160. // mount hook should have been called
  161. expect(spy).toHaveBeenCalled()
  162. // should retain same instance before/after the await call
  163. expect(beforeInstance).toBe(afterInstance)
  164. })
  165. test('should not leak instance on multiple awaits', async () => {
  166. let resolve: (val?: any) => void
  167. let beforeInstance: ComponentInternalInstance | null = null
  168. let afterInstance: ComponentInternalInstance | null = null
  169. let inBandInstance: ComponentInternalInstance | null = null
  170. let outOfBandInstance: ComponentInternalInstance | null = null
  171. const ready = new Promise(r => {
  172. resolve = r
  173. })
  174. async function doAsyncWork() {
  175. // should still have instance
  176. inBandInstance = getCurrentInstance()
  177. await Promise.resolve()
  178. // should not leak instance
  179. outOfBandInstance = getCurrentInstance()
  180. }
  181. const Comp = defineComponent({
  182. async setup() {
  183. beforeInstance = getCurrentInstance()
  184. // first await
  185. await withAsyncContext(Promise.resolve())
  186. // setup exit, instance set to null, then resumed
  187. await withAsyncContext(doAsyncWork())
  188. afterInstance = getCurrentInstance()
  189. return () => {
  190. resolve()
  191. return ''
  192. }
  193. }
  194. })
  195. const root = nodeOps.createElement('div')
  196. render(h(() => h(Suspense, () => h(Comp))), root)
  197. await ready
  198. expect(inBandInstance).toBe(beforeInstance)
  199. expect(outOfBandInstance).toBeNull()
  200. expect(afterInstance).toBe(beforeInstance)
  201. expect(getCurrentInstance()).toBeNull()
  202. })
  203. test('should not leak on multiple awaits + error', async () => {
  204. let resolve: (val?: any) => void
  205. const ready = new Promise(r => {
  206. resolve = r
  207. })
  208. const Comp = defineComponent({
  209. async setup() {
  210. await withAsyncContext(Promise.resolve())
  211. await withAsyncContext(Promise.reject())
  212. },
  213. render() {}
  214. })
  215. const app = createApp(() => h(Suspense, () => h(Comp)))
  216. app.config.errorHandler = () => {
  217. resolve()
  218. return false
  219. }
  220. const root = nodeOps.createElement('div')
  221. app.mount(root)
  222. await ready
  223. expect(getCurrentInstance()).toBeNull()
  224. })
  225. })
  226. })