createComponent.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { createComponent } from '../src/component'
  2. import { value } from '@vue/observer'
  3. import { PropType } from '../src/componentProps'
  4. test('createComponent type inference', () => {
  5. createComponent({
  6. props: {
  7. a: Number,
  8. // required should make property non-void
  9. b: {
  10. type: String,
  11. required: true
  12. },
  13. // default value should infer type and make it non-void
  14. bb: {
  15. default: 'hello'
  16. },
  17. // explicit type casting
  18. cc: (Array as any) as PropType<string[]>,
  19. // required + type casting
  20. dd: {
  21. type: (Array as any) as PropType<string[]>,
  22. required: true
  23. }
  24. } as const, // required to narrow for conditional check
  25. setup(props) {
  26. props.a && props.a * 2
  27. props.b.slice()
  28. props.bb.slice()
  29. props.cc && props.cc.push('hoo')
  30. props.dd.push('dd')
  31. return {
  32. c: value(1),
  33. d: {
  34. e: value('hi')
  35. }
  36. }
  37. },
  38. render({ state, props }) {
  39. state.c * 2
  40. state.d.e.slice()
  41. props.a && props.a * 2
  42. props.b.slice()
  43. props.bb.slice()
  44. props.cc && props.cc.push('hoo')
  45. props.dd.push('dd')
  46. this.a && this.a * 2
  47. this.b.slice()
  48. this.bb.slice()
  49. this.c * 2
  50. this.d.e.slice()
  51. this.cc && this.cc.push('hoo')
  52. this.dd.push('dd')
  53. }
  54. })
  55. // rename this file to .tsx to test TSX props inference
  56. // ;(<MyComponent a={1} b="foo"/>)
  57. })
  58. test('type inference w/ optional props declaration', () => {
  59. createComponent({
  60. setup(props) {
  61. props.anything
  62. return {
  63. a: 1
  64. }
  65. },
  66. render({ props, state }) {
  67. props.foobar
  68. state.a * 2
  69. this.a * 2
  70. // should not make state and this indexable
  71. // state.foobar
  72. // this.foobar
  73. }
  74. })
  75. })
  76. // test('type inference w/ array props declaration', () => {
  77. // createComponent({
  78. // props: ['a', 'b'],
  79. // setup(props) {
  80. // props.a
  81. // props.b
  82. // return {
  83. // c: 1
  84. // }
  85. // },
  86. // render({ props, state }) {
  87. // props.a
  88. // props.b
  89. // state.c
  90. // this.a
  91. // this.b
  92. // this.c
  93. // }
  94. // })
  95. // })