2
0

reactivity.test-d.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {
  2. ref,
  3. readonly,
  4. shallowReadonly,
  5. describe,
  6. expectError,
  7. expectType,
  8. Ref,
  9. reactive,
  10. markRaw
  11. } from './index'
  12. describe('should support DeepReadonly', () => {
  13. const r = readonly({ obj: { k: 'v' } })
  14. // @ts-expect-error
  15. expectError((r.obj = {}))
  16. // @ts-expect-error
  17. expectError((r.obj.k = 'x'))
  18. })
  19. // #4180
  20. describe('readonly ref', () => {
  21. const r = readonly(ref({ count: 1 }))
  22. expectType<Ref>(r)
  23. })
  24. describe('should support markRaw', () => {
  25. class Test<T> {
  26. item = {} as Ref<T>
  27. }
  28. const test = new Test<number>()
  29. const plain = {
  30. ref: ref(1)
  31. }
  32. const r = reactive({
  33. class: {
  34. raw: markRaw(test),
  35. reactive: test
  36. },
  37. plain: {
  38. raw: markRaw(plain),
  39. reactive: plain
  40. }
  41. })
  42. expectType<Test<number>>(r.class.raw)
  43. // @ts-expect-error it should unwrap
  44. expectType<Test<number>>(r.class.reactive)
  45. expectType<Ref<number>>(r.plain.raw.ref)
  46. // @ts-expect-error it should unwrap
  47. expectType<Ref<number>>(r.plain.reactive.ref)
  48. })
  49. describe('shallowReadonly ref unwrap', () => {
  50. const r = shallowReadonly({ count: { n: ref(1) } })
  51. // @ts-expect-error
  52. r.count = 2
  53. expectType<Ref>(r.count.n)
  54. r.count.n.value = 123
  55. })
  56. // #3819
  57. describe('should unwrap tuple correctly', () => {
  58. const readonlyTuple = [ref(0)] as const
  59. const reactiveReadonlyTuple = reactive(readonlyTuple)
  60. expectType<Ref<number>>(reactiveReadonlyTuple[0])
  61. const tuple: [Ref<number>] = [ref(0)]
  62. const reactiveTuple = reactive(tuple)
  63. expectType<Ref<number>>(reactiveTuple[0])
  64. })