component.spec.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import {
  2. type Ref,
  3. inject,
  4. nextTick,
  5. onUpdated,
  6. provide,
  7. ref,
  8. watch,
  9. watchEffect,
  10. } from '@vue/runtime-dom'
  11. import {
  12. createComponent,
  13. createIf,
  14. createTextNode,
  15. renderEffect,
  16. setText,
  17. template,
  18. } from '../src'
  19. import { makeRender } from './_utils'
  20. import type { VaporComponentInstance } from '../src/component'
  21. const define = makeRender()
  22. describe('component', () => {
  23. it('should update parent(hoc) component host el when child component self update', async () => {
  24. const value = ref(true)
  25. let childNode1: Node | null = null
  26. let childNode2: Node | null = null
  27. const { component: Child } = define({
  28. setup() {
  29. return createIf(
  30. () => value.value,
  31. () => (childNode1 = template('<div></div>')()),
  32. () => (childNode2 = template('<span></span>')()),
  33. )
  34. },
  35. })
  36. const { host } = define({
  37. setup() {
  38. return createComponent(Child)
  39. },
  40. }).render()
  41. expect(host.innerHTML).toBe('<div></div><!--if-->')
  42. expect(host.children[0]).toBe(childNode1)
  43. value.value = false
  44. await nextTick()
  45. expect(host.innerHTML).toBe('<span></span><!--if-->')
  46. expect(host.children[0]).toBe(childNode2)
  47. })
  48. it('should create a component with props', () => {
  49. const { component: Comp } = define({
  50. setup() {
  51. return template('<div>', true)()
  52. },
  53. })
  54. const { host } = define({
  55. setup() {
  56. return createComponent(Comp, { id: () => 'foo', class: () => 'bar' })
  57. },
  58. }).render()
  59. expect(host.innerHTML).toBe('<div id="foo" class="bar"></div>')
  60. })
  61. it('should not update Component if only changed props are declared emit listeners', async () => {
  62. const updatedSyp = vi.fn()
  63. const { component: Comp } = define({
  64. emits: ['foo'],
  65. setup() {
  66. onUpdated(updatedSyp)
  67. return template('<div>', true)()
  68. },
  69. })
  70. const toggle = ref(true)
  71. const fn1 = () => {}
  72. const fn2 = () => {}
  73. define({
  74. setup() {
  75. const _on_foo = () => (toggle.value ? fn1() : fn2())
  76. return createComponent(Comp, { onFoo: () => _on_foo })
  77. },
  78. }).render()
  79. expect(updatedSyp).toHaveBeenCalledTimes(0)
  80. toggle.value = false
  81. await nextTick()
  82. expect(updatedSyp).toHaveBeenCalledTimes(0)
  83. })
  84. it('component child synchronously updating parent state should trigger parent re-render', async () => {
  85. const { component: Child } = define({
  86. setup() {
  87. const n = inject<Ref<number>>('foo')!
  88. n.value++
  89. const n0 = template('<div></div>')()
  90. renderEffect(() => setText(n0, n.value))
  91. return n0
  92. },
  93. })
  94. const { host } = define({
  95. setup() {
  96. const n = ref(0)
  97. provide('foo', n)
  98. const n0 = template('<div></div>')()
  99. renderEffect(() => setText(n0, n.value))
  100. return [n0, createComponent(Child)]
  101. },
  102. }).render()
  103. expect(host.innerHTML).toBe('<div>0</div><div>1</div>')
  104. await nextTick()
  105. expect(host.innerHTML).toBe('<div>1</div><div>1</div>')
  106. })
  107. it('component child updating parent state in pre-flush should trigger parent re-render', async () => {
  108. const { component: Child } = define({
  109. props: ['value'],
  110. setup(props: any, { emit }) {
  111. watch(
  112. () => props.value,
  113. val => emit('update', val),
  114. )
  115. const n0 = template('<div></div>')()
  116. renderEffect(() => setText(n0, props.value))
  117. return n0
  118. },
  119. })
  120. const outer = ref(0)
  121. const { host } = define({
  122. setup() {
  123. const inner = ref(0)
  124. const n0 = template('<div></div>')()
  125. renderEffect(() => setText(n0, inner.value))
  126. const n1 = createComponent(Child, {
  127. value: () => outer.value,
  128. onUpdate: () => (val: number) => (inner.value = val),
  129. })
  130. return [n0, n1]
  131. },
  132. }).render()
  133. expect(host.innerHTML).toBe('<div>0</div><div>0</div>')
  134. outer.value++
  135. await nextTick()
  136. expect(host.innerHTML).toBe('<div>1</div><div>1</div>')
  137. })
  138. it('child only updates once when triggered in multiple ways', async () => {
  139. const a = ref(0)
  140. const calls: string[] = []
  141. const { component: Child } = define({
  142. props: ['count'],
  143. setup(props: any) {
  144. onUpdated(() => calls.push('update child'))
  145. return createTextNode(() => [`${props.count} - ${a.value}`])
  146. },
  147. })
  148. const { host } = define({
  149. setup() {
  150. return createComponent(Child, { count: () => a.value })
  151. },
  152. }).render()
  153. expect(host.innerHTML).toBe('0 - 0')
  154. expect(calls).toEqual([])
  155. // This will trigger child rendering directly, as well as via a prop change
  156. a.value++
  157. await nextTick()
  158. expect(host.innerHTML).toBe('1 - 1')
  159. expect(calls).toEqual(['update child'])
  160. })
  161. it(`an earlier update doesn't lead to excessive subsequent updates`, async () => {
  162. const globalCount = ref(0)
  163. const parentCount = ref(0)
  164. const calls: string[] = []
  165. const { component: Child } = define({
  166. props: ['count'],
  167. setup(props: any) {
  168. watch(
  169. () => props.count,
  170. () => {
  171. calls.push('child watcher')
  172. globalCount.value = props.count
  173. },
  174. )
  175. onUpdated(() => calls.push('update child'))
  176. return []
  177. },
  178. })
  179. const { component: Parent } = define({
  180. props: ['count'],
  181. setup(props: any) {
  182. onUpdated(() => calls.push('update parent'))
  183. const n1 = createTextNode(() => [
  184. `${globalCount.value} - ${props.count}`,
  185. ])
  186. const n2 = createComponent(Child, { count: () => parentCount.value })
  187. return [n1, n2]
  188. },
  189. })
  190. const { host } = define({
  191. setup() {
  192. onUpdated(() => calls.push('update root'))
  193. return createComponent(Parent, { count: () => globalCount.value })
  194. },
  195. }).render()
  196. expect(host.innerHTML).toBe(`0 - 0`)
  197. expect(calls).toEqual([])
  198. parentCount.value++
  199. await nextTick()
  200. expect(host.innerHTML).toBe(`1 - 1`)
  201. expect(calls).toEqual(['child watcher', 'update parent'])
  202. })
  203. it('child component props update should not lead to double update', async () => {
  204. const text = ref(0)
  205. const spy = vi.fn()
  206. const { component: Comp } = define({
  207. props: ['text'],
  208. setup(props: any) {
  209. const n1 = template('<h1></h1>')()
  210. renderEffect(() => {
  211. spy()
  212. setText(n1, props.text)
  213. })
  214. return n1
  215. },
  216. })
  217. const { host } = define({
  218. setup() {
  219. return createComponent(Comp, { text: () => text.value })
  220. },
  221. }).render()
  222. expect(host.innerHTML).toBe('<h1>0</h1>')
  223. expect(spy).toHaveBeenCalledTimes(1)
  224. text.value++
  225. await nextTick()
  226. expect(host.innerHTML).toBe('<h1>1</h1>')
  227. expect(spy).toHaveBeenCalledTimes(2)
  228. })
  229. it('unmount component', async () => {
  230. const { host, app, instance } = define(() => {
  231. const count = ref(0)
  232. const t0 = template('<div></div>')
  233. const n0 = t0()
  234. watchEffect(() => {
  235. setText(n0, count.value)
  236. })
  237. renderEffect(() => {})
  238. return n0
  239. }).render()
  240. const i = instance as VaporComponentInstance
  241. // watchEffect + renderEffect + props validation effect
  242. expect(i.scope.effects.length).toBe(3)
  243. expect(host.innerHTML).toBe('<div>0</div>')
  244. app.unmount()
  245. expect(host.innerHTML).toBe('')
  246. expect(i.scope.effects.length).toBe(0)
  247. })
  248. test('should mount component only with template in production mode', () => {
  249. __DEV__ = false
  250. const { component: Child } = define({
  251. render() {
  252. return template('<div> HI </div>', true)()
  253. },
  254. })
  255. const { host } = define({
  256. setup() {
  257. return createComponent(Child, null, null, true)
  258. },
  259. }).render()
  260. expect(host.innerHTML).toBe('<div> HI </div>')
  261. __DEV__ = true
  262. })
  263. it('warn if functional vapor component not return a block', () => {
  264. define(() => {
  265. return () => {}
  266. }).render()
  267. expect(
  268. 'Functional vapor component must return a block directly',
  269. ).toHaveBeenWarned()
  270. })
  271. it('warn if setup return a function and no render function', () => {
  272. define({
  273. setup() {
  274. return () => []
  275. },
  276. }).render()
  277. expect(
  278. 'Vapor component setup() returned non-block value, and has no render function',
  279. ).toHaveBeenWarned()
  280. })
  281. })