defineVaporCustomElement.test-d.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {
  2. type VaporElementConstructor,
  3. defineVaporComponent,
  4. defineVaporCustomElement,
  5. } from 'vue'
  6. import { describe, expectType, test } from '../utils'
  7. describe('defineVaporCustomElement using defineVaporComponent return type', () => {
  8. test('with object emits', () => {
  9. const Comp1Vapor = defineVaporComponent({
  10. props: {
  11. a: String,
  12. },
  13. emits: {
  14. click: () => true,
  15. },
  16. })
  17. const Comp = defineVaporCustomElement(Comp1Vapor)
  18. expectType<VaporElementConstructor>(Comp)
  19. const instance = new Comp()
  20. expectType<string | undefined>(instance.a)
  21. instance.a = ''
  22. })
  23. test('with array emits', () => {
  24. const Comp1Vapor = defineVaporComponent({
  25. props: {
  26. a: Number,
  27. },
  28. emits: ['click'],
  29. })
  30. const Comp = defineVaporCustomElement(Comp1Vapor)
  31. expectType<VaporElementConstructor>(Comp)
  32. const instance = new Comp()
  33. expectType<number | undefined>(instance.a)
  34. instance.a = 42
  35. })
  36. test('with required props', () => {
  37. const Comp1Vapor = defineVaporComponent({
  38. props: {
  39. a: { type: Number, required: true },
  40. },
  41. })
  42. const Comp = defineVaporCustomElement(Comp1Vapor)
  43. expectType<VaporElementConstructor>(Comp)
  44. const instance = new Comp()
  45. expectType<number>(instance.a)
  46. instance.a = 42
  47. })
  48. test('with default props', () => {
  49. const Comp1Vapor = defineVaporComponent({
  50. props: {
  51. a: {
  52. type: Number,
  53. default: 1,
  54. validator: () => true,
  55. },
  56. },
  57. emits: ['click'],
  58. })
  59. const Comp = defineVaporCustomElement(Comp1Vapor)
  60. expectType<VaporElementConstructor>(Comp)
  61. const instance = new Comp()
  62. expectType<number>(instance.a)
  63. instance.a = 42
  64. })
  65. test('with extra options', () => {
  66. const Comp1Vapor = defineVaporComponent({
  67. props: {
  68. a: {
  69. type: Number,
  70. default: 1,
  71. validator: () => true,
  72. },
  73. },
  74. emits: ['click'],
  75. })
  76. const Comp = defineVaporCustomElement(Comp1Vapor, {
  77. shadowRoot: false,
  78. styles: [`div { color: red; }`],
  79. nonce: 'xxx',
  80. shadowRootOptions: {
  81. delegatesFocus: false,
  82. },
  83. configureApp: app => {
  84. app.provide('a', 1)
  85. },
  86. })
  87. expectType<VaporElementConstructor>(Comp)
  88. const instance = new Comp()
  89. expectType<number>(instance.a)
  90. instance.a = 42
  91. })
  92. })
  93. describe('defineVaporCustomElement with direct setup function', () => {
  94. test('basic setup function', () => {
  95. const Comp = defineVaporCustomElement((props: { msg: string }) => {
  96. expectType<string>(props.msg)
  97. return []
  98. })
  99. expectType<VaporElementConstructor<{ msg: string }>>(Comp)
  100. const instance = new Comp()
  101. expectType<string>(instance.msg)
  102. })
  103. test('setup function with emits', () => {
  104. const Comp = defineVaporCustomElement(
  105. (props: { msg: string }, ctx) => {
  106. ctx.emit('foo')
  107. return []
  108. },
  109. {
  110. emits: ['foo'],
  111. },
  112. )
  113. expectType<VaporElementConstructor<{ msg: string }>>(Comp)
  114. const instance = new Comp()
  115. expectType<string>(instance.msg)
  116. })
  117. test('setup function with extra options', () => {
  118. const Comp = defineVaporCustomElement(
  119. (props: { msg: string }, ctx) => {
  120. ctx.emit('foo')
  121. return []
  122. },
  123. {
  124. name: 'Foo',
  125. emits: ['foo'],
  126. inheritAttrs: false,
  127. shadowRoot: false,
  128. styles: [`div { color: red; }`],
  129. nonce: 'xxx',
  130. shadowRootOptions: {
  131. delegatesFocus: false,
  132. },
  133. configureApp: app => {
  134. app.provide('a', 1)
  135. },
  136. },
  137. )
  138. expectType<VaporElementConstructor<{ msg: string }>>(Comp)
  139. const instance = new Comp()
  140. expectType<string>(instance.msg)
  141. })
  142. })
  143. describe('defineVaporCustomElement with options object', () => {
  144. test('with object props', () => {
  145. const Comp = defineVaporCustomElement({
  146. props: {
  147. foo: String,
  148. bar: {
  149. type: Number,
  150. required: true,
  151. },
  152. },
  153. setup(props) {
  154. expectType<string | undefined>(props.foo)
  155. expectType<number>(props.bar)
  156. },
  157. })
  158. expectType<VaporElementConstructor>(Comp)
  159. const instance = new Comp()
  160. expectType<string | undefined>(instance.foo)
  161. expectType<number>(instance.bar)
  162. })
  163. test('with array props', () => {
  164. const Comp = defineVaporCustomElement({
  165. props: ['foo', 'bar'],
  166. setup(props) {
  167. expectType<any>(props.foo)
  168. expectType<any>(props.bar)
  169. },
  170. })
  171. expectType<VaporElementConstructor>(Comp)
  172. const instance = new Comp()
  173. expectType<any>(instance.foo)
  174. expectType<any>(instance.bar)
  175. })
  176. test('with emits', () => {
  177. const Comp = defineVaporCustomElement({
  178. props: {
  179. value: String,
  180. },
  181. emits: {
  182. change: (val: string) => true,
  183. },
  184. setup(props, { emit }) {
  185. emit('change', 'test')
  186. // @ts-expect-error
  187. emit('change', 123)
  188. // @ts-expect-error
  189. emit('unknown')
  190. },
  191. })
  192. expectType<VaporElementConstructor>(Comp)
  193. const instance = new Comp()
  194. expectType<string | undefined>(instance.value)
  195. })
  196. test('with extra options', () => {
  197. const Comp = defineVaporCustomElement(
  198. {
  199. props: {
  200. value: String,
  201. },
  202. emits: {
  203. change: (val: string) => true,
  204. },
  205. setup(props, { emit }) {
  206. emit('change', 'test')
  207. // @ts-expect-error
  208. emit('change', 123)
  209. // @ts-expect-error
  210. emit('unknown')
  211. },
  212. },
  213. {
  214. shadowRoot: false,
  215. configureApp: app => {
  216. app.provide('a', 1)
  217. },
  218. },
  219. )
  220. expectType<VaporElementConstructor>(Comp)
  221. const instance = new Comp()
  222. expectType<string | undefined>(instance.value)
  223. })
  224. })