apiOptions.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import {
  2. h,
  3. nodeOps,
  4. render,
  5. serializeInner,
  6. triggerEvent,
  7. TestElement,
  8. nextTick,
  9. renderToString,
  10. ref
  11. } from '@vue/runtime-test'
  12. describe('api: options', () => {
  13. test('data', async () => {
  14. const Comp = {
  15. data() {
  16. return {
  17. foo: 1
  18. }
  19. },
  20. render() {
  21. return h(
  22. 'div',
  23. {
  24. onClick: () => {
  25. this.foo++
  26. }
  27. },
  28. this.foo
  29. )
  30. }
  31. }
  32. const root = nodeOps.createElement('div')
  33. render(h(Comp), root)
  34. expect(serializeInner(root)).toBe(`<div>1</div>`)
  35. triggerEvent(root.children[0] as TestElement, 'click')
  36. await nextTick()
  37. expect(serializeInner(root)).toBe(`<div>2</div>`)
  38. })
  39. test('computed', async () => {
  40. const Comp = {
  41. data() {
  42. return {
  43. foo: 1
  44. }
  45. },
  46. computed: {
  47. bar() {
  48. return this.foo + 1
  49. },
  50. baz() {
  51. return this.bar + 1
  52. }
  53. },
  54. render() {
  55. return h(
  56. 'div',
  57. {
  58. onClick: () => {
  59. this.foo++
  60. }
  61. },
  62. this.bar + this.baz
  63. )
  64. }
  65. }
  66. const root = nodeOps.createElement('div')
  67. render(h(Comp), root)
  68. expect(serializeInner(root)).toBe(`<div>5</div>`)
  69. triggerEvent(root.children[0] as TestElement, 'click')
  70. await nextTick()
  71. expect(serializeInner(root)).toBe(`<div>7</div>`)
  72. })
  73. test('methods', async () => {
  74. const Comp = {
  75. data() {
  76. return {
  77. foo: 1
  78. }
  79. },
  80. methods: {
  81. inc() {
  82. this.foo++
  83. }
  84. },
  85. render() {
  86. return h(
  87. 'div',
  88. {
  89. onClick: this.inc
  90. },
  91. this.foo
  92. )
  93. }
  94. }
  95. const root = nodeOps.createElement('div')
  96. render(h(Comp), root)
  97. expect(serializeInner(root)).toBe(`<div>1</div>`)
  98. triggerEvent(root.children[0] as TestElement, 'click')
  99. await nextTick()
  100. expect(serializeInner(root)).toBe(`<div>2</div>`)
  101. })
  102. test('watch', async () => {
  103. function returnThis() {
  104. return this
  105. }
  106. const spyA = jest.fn(returnThis)
  107. const spyB = jest.fn(returnThis)
  108. const spyC = jest.fn(returnThis)
  109. let ctx: any
  110. const Comp = {
  111. data() {
  112. return {
  113. foo: 1,
  114. bar: 2,
  115. baz: {
  116. qux: 3
  117. }
  118. }
  119. },
  120. watch: {
  121. // string method name
  122. foo: 'onFooChange',
  123. // direct function
  124. bar: spyB,
  125. baz: {
  126. handler: spyC,
  127. deep: true
  128. }
  129. },
  130. methods: {
  131. onFooChange: spyA
  132. },
  133. render() {
  134. ctx = this
  135. }
  136. }
  137. const root = nodeOps.createElement('div')
  138. render(h(Comp), root)
  139. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  140. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  141. }
  142. assertCall(spyA, 0, [1, undefined])
  143. assertCall(spyB, 0, [2, undefined])
  144. assertCall(spyC, 0, [{ qux: 3 }, undefined])
  145. expect(spyA).toHaveReturnedWith(ctx)
  146. expect(spyB).toHaveReturnedWith(ctx)
  147. expect(spyC).toHaveReturnedWith(ctx)
  148. ctx.foo++
  149. await nextTick()
  150. expect(spyA).toHaveBeenCalledTimes(2)
  151. assertCall(spyA, 1, [2, 1])
  152. ctx.bar++
  153. await nextTick()
  154. expect(spyB).toHaveBeenCalledTimes(2)
  155. assertCall(spyB, 1, [3, 2])
  156. ctx.baz.qux++
  157. await nextTick()
  158. expect(spyC).toHaveBeenCalledTimes(2)
  159. // new and old objects have same identity
  160. assertCall(spyC, 1, [{ qux: 4 }, { qux: 4 }])
  161. })
  162. test('provide/inject', () => {
  163. const Root = {
  164. data() {
  165. return {
  166. a: 1
  167. }
  168. },
  169. provide() {
  170. return {
  171. a: this.a
  172. }
  173. },
  174. render() {
  175. return [h(ChildA), h(ChildB), h(ChildC), h(ChildD)]
  176. }
  177. }
  178. const ChildA = {
  179. inject: ['a'],
  180. render() {
  181. return this.a
  182. }
  183. }
  184. const ChildB = {
  185. // object alias
  186. inject: { b: 'a' },
  187. render() {
  188. return this.b
  189. }
  190. }
  191. const ChildC = {
  192. inject: {
  193. b: {
  194. from: 'a'
  195. }
  196. },
  197. render() {
  198. return this.b
  199. }
  200. }
  201. const ChildD = {
  202. inject: {
  203. b: {
  204. from: 'c',
  205. default: 2
  206. }
  207. },
  208. render() {
  209. return this.b
  210. }
  211. }
  212. expect(renderToString(h(Root))).toBe(`<!---->1112<!---->`)
  213. })
  214. test('lifecycle', () => {})
  215. test('mixins', () => {
  216. const calls: string[] = []
  217. const mixinA = {
  218. data() {
  219. return {
  220. a: 1
  221. }
  222. },
  223. mounted() {
  224. calls.push('mixinA')
  225. }
  226. }
  227. const mixinB = {
  228. data() {
  229. return {
  230. b: 2
  231. }
  232. },
  233. mounted() {
  234. calls.push('mixinB')
  235. }
  236. }
  237. const Comp = {
  238. mixins: [mixinA, mixinB],
  239. data() {
  240. return {
  241. c: 3
  242. }
  243. },
  244. mounted() {
  245. calls.push('comp')
  246. },
  247. render() {
  248. return `${this.a}${this.b}${this.c}`
  249. }
  250. }
  251. expect(renderToString(h(Comp))).toBe(`123`)
  252. expect(calls).toEqual(['mixinA', 'mixinB', 'comp'])
  253. })
  254. test('extends', () => {
  255. const calls: string[] = []
  256. const Base = {
  257. data() {
  258. return {
  259. a: 1
  260. }
  261. },
  262. mounted() {
  263. calls.push('base')
  264. }
  265. }
  266. const Comp = {
  267. extends: Base,
  268. data() {
  269. return {
  270. b: 2
  271. }
  272. },
  273. mounted() {
  274. calls.push('comp')
  275. },
  276. render() {
  277. return `${this.a}${this.b}`
  278. }
  279. }
  280. expect(renderToString(h(Comp))).toBe(`12`)
  281. expect(calls).toEqual(['base', 'comp'])
  282. })
  283. test('accessing setup() state from options', async () => {
  284. const Comp = {
  285. setup() {
  286. return {
  287. count: ref(0)
  288. }
  289. },
  290. data() {
  291. return {
  292. plusOne: this.count + 1
  293. }
  294. },
  295. computed: {
  296. plusTwo() {
  297. return this.count + 2
  298. }
  299. },
  300. methods: {
  301. inc() {
  302. this.count++
  303. }
  304. },
  305. render() {
  306. return h(
  307. 'div',
  308. {
  309. onClick: this.inc
  310. },
  311. `${this.count},${this.plusOne},${this.plusTwo}`
  312. )
  313. }
  314. }
  315. const root = nodeOps.createElement('div')
  316. render(h(Comp), root)
  317. expect(serializeInner(root)).toBe(`<div>0,1,2</div>`)
  318. triggerEvent(root.children[0] as TestElement, 'click')
  319. await nextTick()
  320. expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
  321. })
  322. })