compileScriptRefSugar.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode } from './utils'
  3. // this file only tests integration with SFC - main test case for the ref
  4. // transform can be found in <root>/packages/ref-transform/__tests__
  5. describe('<script setup> ref sugar', () => {
  6. function compileWithRefSugar(src: string) {
  7. return compile(src, { refSugar: true })
  8. }
  9. test('$ unwrapping', () => {
  10. const { content, bindings } = compileWithRefSugar(`<script setup>
  11. import { ref, shallowRef } from 'vue'
  12. let foo = $(ref())
  13. let a = $(ref(1))
  14. let b = $(shallowRef({
  15. count: 0
  16. }))
  17. let c = () => {}
  18. let d
  19. </script>`)
  20. expect(content).not.toMatch(`$(ref())`)
  21. expect(content).not.toMatch(`$(ref(1))`)
  22. expect(content).not.toMatch(`$(shallowRef({`)
  23. expect(content).toMatch(`let foo = (ref())`)
  24. expect(content).toMatch(`let a = (ref(1))`)
  25. expect(content).toMatch(`
  26. let b = (shallowRef({
  27. count: 0
  28. }))
  29. `)
  30. // normal declarations left untouched
  31. expect(content).toMatch(`let c = () => {}`)
  32. expect(content).toMatch(`let d`)
  33. expect(content).toMatch(`return { foo, a, b, c, d, ref, shallowRef }`)
  34. assertCode(content)
  35. expect(bindings).toStrictEqual({
  36. foo: BindingTypes.SETUP_REF,
  37. a: BindingTypes.SETUP_REF,
  38. b: BindingTypes.SETUP_REF,
  39. c: BindingTypes.SETUP_LET,
  40. d: BindingTypes.SETUP_LET,
  41. ref: BindingTypes.SETUP_CONST,
  42. shallowRef: BindingTypes.SETUP_CONST
  43. })
  44. })
  45. test('$ref & $shallowRef declarations', () => {
  46. const { content, bindings } = compileWithRefSugar(`<script setup>
  47. let foo = $ref()
  48. let a = $ref(1)
  49. let b = $shallowRef({
  50. count: 0
  51. })
  52. let c = () => {}
  53. let d
  54. </script>`)
  55. expect(content).toMatch(
  56. `import { ref as _ref, shallowRef as _shallowRef } from 'vue'`
  57. )
  58. expect(content).not.toMatch(`$ref()`)
  59. expect(content).not.toMatch(`$ref(1)`)
  60. expect(content).not.toMatch(`$shallowRef({`)
  61. expect(content).toMatch(`let foo = _ref()`)
  62. expect(content).toMatch(`let a = _ref(1)`)
  63. expect(content).toMatch(`
  64. let b = _shallowRef({
  65. count: 0
  66. })
  67. `)
  68. // normal declarations left untouched
  69. expect(content).toMatch(`let c = () => {}`)
  70. expect(content).toMatch(`let d`)
  71. assertCode(content)
  72. expect(bindings).toStrictEqual({
  73. foo: BindingTypes.SETUP_REF,
  74. a: BindingTypes.SETUP_REF,
  75. b: BindingTypes.SETUP_REF,
  76. c: BindingTypes.SETUP_LET,
  77. d: BindingTypes.SETUP_LET
  78. })
  79. })
  80. describe('errors', () => {
  81. test('defineProps/Emit() referencing ref declarations', () => {
  82. expect(() =>
  83. compile(
  84. `<script setup>
  85. let bar = $ref(1)
  86. defineProps({
  87. bar
  88. })
  89. </script>`,
  90. { refSugar: true }
  91. )
  92. ).toThrow(`cannot reference locally declared variables`)
  93. expect(() =>
  94. compile(
  95. `<script setup>
  96. let bar = $ref(1)
  97. defineEmits({
  98. bar
  99. })
  100. </script>`,
  101. { refSugar: true }
  102. )
  103. ).toThrow(`cannot reference locally declared variables`)
  104. })
  105. })
  106. })