apiOptions.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import {
  2. h,
  3. nodeOps,
  4. render,
  5. serializeInner,
  6. triggerEvent,
  7. TestElement,
  8. nextTick,
  9. renderToString,
  10. ref,
  11. createComponent,
  12. mockWarn
  13. } from '@vue/runtime-test'
  14. describe('api: options', () => {
  15. test('data', async () => {
  16. const Comp = createComponent({
  17. data() {
  18. return {
  19. foo: 1
  20. }
  21. },
  22. render() {
  23. return h(
  24. 'div',
  25. {
  26. onClick: () => {
  27. this.foo++
  28. }
  29. },
  30. this.foo
  31. )
  32. }
  33. })
  34. const root = nodeOps.createElement('div')
  35. render(h(Comp), root)
  36. expect(serializeInner(root)).toBe(`<div>1</div>`)
  37. triggerEvent(root.children[0] as TestElement, 'click')
  38. await nextTick()
  39. expect(serializeInner(root)).toBe(`<div>2</div>`)
  40. })
  41. test('computed', async () => {
  42. const Comp = createComponent({
  43. data() {
  44. return {
  45. foo: 1
  46. }
  47. },
  48. computed: {
  49. bar(): number {
  50. return this.foo + 1
  51. },
  52. baz(): number {
  53. return this.bar + 1
  54. }
  55. },
  56. render() {
  57. return h(
  58. 'div',
  59. {
  60. onClick: () => {
  61. this.foo++
  62. }
  63. },
  64. this.bar + this.baz
  65. )
  66. }
  67. })
  68. const root = nodeOps.createElement('div')
  69. render(h(Comp), root)
  70. expect(serializeInner(root)).toBe(`<div>5</div>`)
  71. triggerEvent(root.children[0] as TestElement, 'click')
  72. await nextTick()
  73. expect(serializeInner(root)).toBe(`<div>7</div>`)
  74. })
  75. test('methods', async () => {
  76. const Comp = createComponent({
  77. data() {
  78. return {
  79. foo: 1
  80. }
  81. },
  82. methods: {
  83. inc() {
  84. this.foo++
  85. }
  86. },
  87. render() {
  88. return h(
  89. 'div',
  90. {
  91. onClick: this.inc
  92. },
  93. this.foo
  94. )
  95. }
  96. })
  97. const root = nodeOps.createElement('div')
  98. render(h(Comp), root)
  99. expect(serializeInner(root)).toBe(`<div>1</div>`)
  100. triggerEvent(root.children[0] as TestElement, 'click')
  101. await nextTick()
  102. expect(serializeInner(root)).toBe(`<div>2</div>`)
  103. })
  104. test('watch', async () => {
  105. function returnThis(this: any) {
  106. return this
  107. }
  108. const spyA = jest.fn(returnThis)
  109. const spyB = jest.fn(returnThis)
  110. const spyC = jest.fn(returnThis)
  111. let ctx: any
  112. const Comp = {
  113. data() {
  114. return {
  115. foo: 1,
  116. bar: 2,
  117. baz: {
  118. qux: 3
  119. }
  120. }
  121. },
  122. watch: {
  123. // string method name
  124. foo: 'onFooChange',
  125. // direct function
  126. bar: spyB,
  127. baz: {
  128. handler: spyC,
  129. deep: true
  130. }
  131. },
  132. methods: {
  133. onFooChange: spyA
  134. },
  135. render() {
  136. ctx = this
  137. }
  138. }
  139. const root = nodeOps.createElement('div')
  140. render(h(Comp), root)
  141. function assertCall(spy: jest.Mock, callIndex: number, args: any[]) {
  142. expect(spy.mock.calls[callIndex].slice(0, 2)).toMatchObject(args)
  143. }
  144. assertCall(spyA, 0, [1, undefined])
  145. assertCall(spyB, 0, [2, undefined])
  146. assertCall(spyC, 0, [{ qux: 3 }, undefined])
  147. expect(spyA).toHaveReturnedWith(ctx)
  148. expect(spyB).toHaveReturnedWith(ctx)
  149. expect(spyC).toHaveReturnedWith(ctx)
  150. ctx.foo++
  151. await nextTick()
  152. expect(spyA).toHaveBeenCalledTimes(2)
  153. assertCall(spyA, 1, [2, 1])
  154. ctx.bar++
  155. await nextTick()
  156. expect(spyB).toHaveBeenCalledTimes(2)
  157. assertCall(spyB, 1, [3, 2])
  158. ctx.baz.qux++
  159. await nextTick()
  160. expect(spyC).toHaveBeenCalledTimes(2)
  161. // new and old objects have same identity
  162. assertCall(spyC, 1, [{ qux: 4 }, { qux: 4 }])
  163. })
  164. test('provide/inject', () => {
  165. const Root = {
  166. data() {
  167. return {
  168. a: 1
  169. }
  170. },
  171. provide() {
  172. return {
  173. a: this.a
  174. }
  175. },
  176. render() {
  177. return [h(ChildA), h(ChildB), h(ChildC), h(ChildD)]
  178. }
  179. } as any
  180. const ChildA = {
  181. inject: ['a'],
  182. render() {
  183. return this.a
  184. }
  185. } as any
  186. const ChildB = {
  187. // object alias
  188. inject: { b: 'a' },
  189. render() {
  190. return this.b
  191. }
  192. } as any
  193. const ChildC = {
  194. inject: {
  195. b: {
  196. from: 'a'
  197. }
  198. },
  199. render() {
  200. return this.b
  201. }
  202. } as any
  203. const ChildD = {
  204. inject: {
  205. b: {
  206. from: 'c',
  207. default: 2
  208. }
  209. },
  210. render() {
  211. return this.b
  212. }
  213. } as any
  214. expect(renderToString(h(Root))).toBe(`<!---->1112<!---->`)
  215. })
  216. test('lifecycle', async () => {
  217. const count = ref(0)
  218. const root = nodeOps.createElement('div')
  219. const calls: string[] = []
  220. const Root = {
  221. beforeCreate() {
  222. calls.push('root beforeCreate')
  223. },
  224. created() {
  225. calls.push('root created')
  226. },
  227. beforeMount() {
  228. calls.push('root onBeforeMount')
  229. },
  230. mounted() {
  231. calls.push('root onMounted')
  232. },
  233. beforeUpdate() {
  234. calls.push('root onBeforeUpdate')
  235. },
  236. updated() {
  237. calls.push('root onUpdated')
  238. },
  239. beforeUnmount() {
  240. calls.push('root onBeforeUnmount')
  241. },
  242. unmounted() {
  243. calls.push('root onUnmounted')
  244. },
  245. render() {
  246. return h(Mid, { count: count.value })
  247. }
  248. }
  249. const Mid = {
  250. beforeCreate() {
  251. calls.push('mid beforeCreate')
  252. },
  253. created() {
  254. calls.push('mid created')
  255. },
  256. beforeMount() {
  257. calls.push('mid onBeforeMount')
  258. },
  259. mounted() {
  260. calls.push('mid onMounted')
  261. },
  262. beforeUpdate() {
  263. calls.push('mid onBeforeUpdate')
  264. },
  265. updated() {
  266. calls.push('mid onUpdated')
  267. },
  268. beforeUnmount() {
  269. calls.push('mid onBeforeUnmount')
  270. },
  271. unmounted() {
  272. calls.push('mid onUnmounted')
  273. },
  274. render(this: any) {
  275. return h(Child, { count: this.$props.count })
  276. }
  277. }
  278. const Child = {
  279. beforeCreate() {
  280. calls.push('child beforeCreate')
  281. },
  282. created() {
  283. calls.push('child created')
  284. },
  285. beforeMount() {
  286. calls.push('child onBeforeMount')
  287. },
  288. mounted() {
  289. calls.push('child onMounted')
  290. },
  291. beforeUpdate() {
  292. calls.push('child onBeforeUpdate')
  293. },
  294. updated() {
  295. calls.push('child onUpdated')
  296. },
  297. beforeUnmount() {
  298. calls.push('child onBeforeUnmount')
  299. },
  300. unmounted() {
  301. calls.push('child onUnmounted')
  302. },
  303. render(this: any) {
  304. return h('div', this.$props.count)
  305. }
  306. }
  307. // mount
  308. render(h(Root), root)
  309. expect(calls).toEqual([
  310. 'root beforeCreate',
  311. 'root created',
  312. 'root onBeforeMount',
  313. 'mid beforeCreate',
  314. 'mid created',
  315. 'mid onBeforeMount',
  316. 'child beforeCreate',
  317. 'child created',
  318. 'child onBeforeMount',
  319. 'child onMounted',
  320. 'mid onMounted',
  321. 'root onMounted'
  322. ])
  323. calls.length = 0
  324. // update
  325. count.value++
  326. await nextTick()
  327. expect(calls).toEqual([
  328. 'root onBeforeUpdate',
  329. 'mid onBeforeUpdate',
  330. 'child onBeforeUpdate',
  331. 'child onUpdated',
  332. 'mid onUpdated',
  333. 'root onUpdated'
  334. ])
  335. calls.length = 0
  336. // unmount
  337. render(null, root)
  338. expect(calls).toEqual([
  339. 'root onBeforeUnmount',
  340. 'mid onBeforeUnmount',
  341. 'child onBeforeUnmount',
  342. 'child onUnmounted',
  343. 'mid onUnmounted',
  344. 'root onUnmounted'
  345. ])
  346. })
  347. test('mixins', () => {
  348. const calls: string[] = []
  349. const mixinA = {
  350. data() {
  351. return {
  352. a: 1
  353. }
  354. },
  355. created(this: any) {
  356. calls.push('mixinA created')
  357. expect(this.a).toBe(1)
  358. expect(this.b).toBe(2)
  359. expect(this.c).toBe(3)
  360. },
  361. mounted() {
  362. calls.push('mixinA mounted')
  363. }
  364. }
  365. const mixinB = {
  366. data() {
  367. return {
  368. b: 2
  369. }
  370. },
  371. created(this: any) {
  372. calls.push('mixinB created')
  373. expect(this.a).toBe(1)
  374. expect(this.b).toBe(2)
  375. expect(this.c).toBe(3)
  376. },
  377. mounted() {
  378. calls.push('mixinB mounted')
  379. }
  380. }
  381. const Comp = {
  382. mixins: [mixinA, mixinB],
  383. data() {
  384. return {
  385. c: 3
  386. }
  387. },
  388. created(this: any) {
  389. calls.push('comp created')
  390. expect(this.a).toBe(1)
  391. expect(this.b).toBe(2)
  392. expect(this.c).toBe(3)
  393. },
  394. mounted() {
  395. calls.push('comp mounted')
  396. },
  397. render(this: any) {
  398. return `${this.a}${this.b}${this.c}`
  399. }
  400. }
  401. expect(renderToString(h(Comp))).toBe(`123`)
  402. expect(calls).toEqual([
  403. 'mixinA created',
  404. 'mixinB created',
  405. 'comp created',
  406. 'mixinA mounted',
  407. 'mixinB mounted',
  408. 'comp mounted'
  409. ])
  410. })
  411. test('extends', () => {
  412. const calls: string[] = []
  413. const Base = {
  414. data() {
  415. return {
  416. a: 1
  417. }
  418. },
  419. mounted() {
  420. calls.push('base')
  421. }
  422. }
  423. const Comp = {
  424. extends: Base,
  425. data() {
  426. return {
  427. b: 2
  428. }
  429. },
  430. mounted() {
  431. calls.push('comp')
  432. },
  433. render(this: any) {
  434. return `${this.a}${this.b}`
  435. }
  436. }
  437. expect(renderToString(h(Comp))).toBe(`12`)
  438. expect(calls).toEqual(['base', 'comp'])
  439. })
  440. test('accessing setup() state from options', async () => {
  441. const Comp = createComponent({
  442. setup() {
  443. return {
  444. count: ref(0)
  445. }
  446. },
  447. data() {
  448. return {
  449. plusOne: (this as any).count + 1
  450. }
  451. },
  452. computed: {
  453. plusTwo(): number {
  454. return this.count + 2
  455. }
  456. },
  457. methods: {
  458. inc() {
  459. this.count++
  460. }
  461. },
  462. render() {
  463. return h(
  464. 'div',
  465. {
  466. onClick: this.inc
  467. },
  468. `${this.count},${this.plusOne},${this.plusTwo}`
  469. )
  470. }
  471. })
  472. const root = nodeOps.createElement('div')
  473. render(h(Comp), root)
  474. expect(serializeInner(root)).toBe(`<div>0,1,2</div>`)
  475. triggerEvent(root.children[0] as TestElement, 'click')
  476. await nextTick()
  477. expect(serializeInner(root)).toBe(`<div>1,1,3</div>`)
  478. })
  479. describe('warnings', () => {
  480. mockWarn()
  481. test('Expected a function as watch handler', () => {
  482. const Comp = {
  483. watch: {
  484. foo: 'notExistingMethod'
  485. },
  486. render() {}
  487. }
  488. const root = nodeOps.createElement('div')
  489. render(h(Comp), root)
  490. expect(
  491. 'Invalid watch handler specified by key "notExistingMethod"'
  492. ).toHaveBeenWarned()
  493. })
  494. test('Invalid watch option', () => {
  495. const Comp = {
  496. watch: { foo: true },
  497. render() {}
  498. }
  499. const root = nodeOps.createElement('div')
  500. // @ts-ignore
  501. render(h(Comp), root)
  502. expect('Invalid watch option: "foo"').toHaveBeenWarned()
  503. })
  504. })
  505. })