2021-06-30 03:22:26 +08:00
|
|
|
import { BindingTypes } from '@vue/compiler-core'
|
|
|
|
import { compileSFCScript as compile, assertCode } from './utils'
|
|
|
|
|
2021-08-23 10:21:42 +08:00
|
|
|
// this file only tests integration with SFC - main test case for the ref
|
2021-12-12 00:04:38 +08:00
|
|
|
// transform can be found in <root>/packages/reactivity-transform/__tests__
|
2021-08-24 04:00:46 +08:00
|
|
|
describe('sfc ref transform', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
function compileWithReactivityTransform(src: string) {
|
|
|
|
return compile(src, { reactivityTransform: true })
|
2021-06-30 03:22:26 +08:00
|
|
|
}
|
|
|
|
|
2021-08-23 10:21:42 +08:00
|
|
|
test('$ unwrapping', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
const { content, bindings } = compileWithReactivityTransform(`<script setup>
|
2021-08-23 10:21:42 +08:00
|
|
|
import { ref, shallowRef } from 'vue'
|
|
|
|
let foo = $(ref())
|
|
|
|
let a = $(ref(1))
|
|
|
|
let b = $(shallowRef({
|
|
|
|
count: 0
|
|
|
|
}))
|
|
|
|
let c = () => {}
|
|
|
|
let d
|
|
|
|
</script>`)
|
|
|
|
expect(content).not.toMatch(`$(ref())`)
|
|
|
|
expect(content).not.toMatch(`$(ref(1))`)
|
|
|
|
expect(content).not.toMatch(`$(shallowRef({`)
|
|
|
|
expect(content).toMatch(`let foo = (ref())`)
|
|
|
|
expect(content).toMatch(`let a = (ref(1))`)
|
|
|
|
expect(content).toMatch(`
|
|
|
|
let b = (shallowRef({
|
|
|
|
count: 0
|
|
|
|
}))
|
|
|
|
`)
|
|
|
|
// normal declarations left untouched
|
|
|
|
expect(content).toMatch(`let c = () => {}`)
|
|
|
|
expect(content).toMatch(`let d`)
|
|
|
|
expect(content).toMatch(`return { foo, a, b, c, d, ref, shallowRef }`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.SETUP_REF,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_LET,
|
|
|
|
d: BindingTypes.SETUP_LET,
|
|
|
|
ref: BindingTypes.SETUP_CONST,
|
|
|
|
shallowRef: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-11 22:19:58 +08:00
|
|
|
test('$ref & $shallowRef declarations', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
const { content, bindings } = compileWithReactivityTransform(`<script setup>
|
2021-07-15 03:35:34 +08:00
|
|
|
let foo = $ref()
|
|
|
|
let a = $ref(1)
|
2021-08-11 22:19:58 +08:00
|
|
|
let b = $shallowRef({
|
2021-06-30 03:22:26 +08:00
|
|
|
count: 0
|
2021-07-15 03:35:34 +08:00
|
|
|
})
|
2021-06-30 03:22:26 +08:00
|
|
|
let c = () => {}
|
|
|
|
let d
|
|
|
|
</script>`)
|
2021-08-11 22:19:58 +08:00
|
|
|
expect(content).toMatch(
|
|
|
|
`import { ref as _ref, shallowRef as _shallowRef } from 'vue'`
|
|
|
|
)
|
2021-07-15 03:35:34 +08:00
|
|
|
expect(content).not.toMatch(`$ref()`)
|
|
|
|
expect(content).not.toMatch(`$ref(1)`)
|
2021-08-11 22:19:58 +08:00
|
|
|
expect(content).not.toMatch(`$shallowRef({`)
|
2021-07-15 03:35:34 +08:00
|
|
|
expect(content).toMatch(`let foo = _ref()`)
|
|
|
|
expect(content).toMatch(`let a = _ref(1)`)
|
2021-06-30 03:22:26 +08:00
|
|
|
expect(content).toMatch(`
|
2021-08-11 22:19:58 +08:00
|
|
|
let b = _shallowRef({
|
2021-06-30 03:22:26 +08:00
|
|
|
count: 0
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
// normal declarations left untouched
|
|
|
|
expect(content).toMatch(`let c = () => {}`)
|
|
|
|
expect(content).toMatch(`let d`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
foo: BindingTypes.SETUP_REF,
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_LET,
|
|
|
|
d: BindingTypes.SETUP_LET
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-24 04:00:46 +08:00
|
|
|
test('usage in normal <script>', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
const { content } = compileWithReactivityTransform(`<script>
|
2021-08-24 04:00:46 +08:00
|
|
|
export default {
|
|
|
|
setup() {
|
|
|
|
let count = $ref(0)
|
|
|
|
const inc = () => count++
|
|
|
|
return $$({ count })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
expect(content).not.toMatch(`$ref(0)`)
|
|
|
|
expect(content).toMatch(`import { ref as _ref } from 'vue'`)
|
|
|
|
expect(content).toMatch(`let count = _ref(0)`)
|
|
|
|
expect(content).toMatch(`count.value++`)
|
|
|
|
expect(content).toMatch(`return ({ count })`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2021-09-02 04:55:30 +08:00
|
|
|
test('usage /w typescript', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
const { content } = compileWithReactivityTransform(`
|
2021-09-02 04:55:30 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
let msg = $ref<string | number>('foo');
|
|
|
|
let bar = $ref <string | number>('bar');
|
|
|
|
</script>
|
|
|
|
`)
|
|
|
|
expect(content).toMatch(`import { ref as _ref`)
|
|
|
|
expect(content).toMatch(`let msg = _ref<string | number>('foo')`)
|
|
|
|
expect(content).toMatch(`let bar = _ref <string | number>('bar')`)
|
|
|
|
assertCode(content)
|
|
|
|
})
|
|
|
|
|
2021-08-24 04:00:46 +08:00
|
|
|
test('usage with normal <script> + <script setup>', () => {
|
2021-12-23 16:56:36 +08:00
|
|
|
const { content, bindings } = compileWithReactivityTransform(`<script>
|
2021-08-24 04:00:46 +08:00
|
|
|
let a = $ref(0)
|
|
|
|
let c = $ref(0)
|
|
|
|
</script>
|
|
|
|
<script setup>
|
|
|
|
let b = $ref(0)
|
|
|
|
let c = 0
|
|
|
|
function change() {
|
|
|
|
a++
|
|
|
|
b++
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
</script>`)
|
|
|
|
// should dedupe helper imports
|
|
|
|
expect(content).toMatch(`import { ref as _ref } from 'vue'`)
|
|
|
|
|
|
|
|
expect(content).toMatch(`let a = _ref(0)`)
|
|
|
|
expect(content).toMatch(`let b = _ref(0)`)
|
|
|
|
|
|
|
|
// root level ref binding declared in <script> should be inherited in <script setup>
|
|
|
|
expect(content).toMatch(`a.value++`)
|
|
|
|
expect(content).toMatch(`b.value++`)
|
|
|
|
// c shadowed
|
|
|
|
expect(content).toMatch(`c++`)
|
|
|
|
assertCode(content)
|
|
|
|
expect(bindings).toStrictEqual({
|
|
|
|
a: BindingTypes.SETUP_REF,
|
|
|
|
b: BindingTypes.SETUP_REF,
|
|
|
|
c: BindingTypes.SETUP_REF,
|
|
|
|
change: BindingTypes.SETUP_CONST
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-30 03:22:26 +08:00
|
|
|
describe('errors', () => {
|
|
|
|
test('defineProps/Emit() referencing ref declarations', () => {
|
|
|
|
expect(() =>
|
|
|
|
compile(
|
|
|
|
`<script setup>
|
2021-07-15 03:35:34 +08:00
|
|
|
let bar = $ref(1)
|
2021-06-30 03:22:26 +08:00
|
|
|
defineProps({
|
|
|
|
bar
|
|
|
|
})
|
|
|
|
</script>`,
|
2021-12-23 16:56:36 +08:00
|
|
|
{ reactivityTransform: true }
|
2021-06-30 03:22:26 +08:00
|
|
|
)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
|
|
|
|
|
|
|
expect(() =>
|
|
|
|
compile(
|
|
|
|
`<script setup>
|
2021-07-15 03:35:34 +08:00
|
|
|
let bar = $ref(1)
|
2021-06-30 03:22:26 +08:00
|
|
|
defineEmits({
|
|
|
|
bar
|
|
|
|
})
|
|
|
|
</script>`,
|
2021-12-23 16:56:36 +08:00
|
|
|
{ reactivityTransform: true }
|
2021-06-30 03:22:26 +08:00
|
|
|
)
|
|
|
|
).toThrow(`cannot reference locally declared variables`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|