|
| 1 | +import test from 'ava' |
| 2 | +import { transform } from '@babel/core' |
| 3 | +import plugin from '../dist/plugin.testing' |
| 4 | +import vModelPlugin from '../../babel-sugar-v-model/dist/plugin.testing' |
| 5 | + |
| 6 | +const transpile = src => |
| 7 | + new Promise((resolve, reject) => { |
| 8 | + transform( |
| 9 | + src, |
| 10 | + { |
| 11 | + plugins: [vModelPlugin], |
| 12 | + }, |
| 13 | + (err, result) => { |
| 14 | + if (err) { |
| 15 | + return reject(err) |
| 16 | + } |
| 17 | + transform( |
| 18 | + result.code, |
| 19 | + { |
| 20 | + plugins: [plugin], |
| 21 | + }, |
| 22 | + (err, result) => { |
| 23 | + if (err) { |
| 24 | + return reject(err) |
| 25 | + } |
| 26 | + resolve(result.code) |
| 27 | + }, |
| 28 | + ) |
| 29 | + }, |
| 30 | + ) |
| 31 | + }) |
| 32 | + |
| 33 | +const tests = [ |
| 34 | + { |
| 35 | + name: 'Ignores non vModel arguments', |
| 36 | + from: `const a = { setup: () => { return () => <A x="y" a:b={c} /> } }`, |
| 37 | + to: `const a = { |
| 38 | + setup: () => { |
| 39 | + return () => <A x="y" a:b={c} />; |
| 40 | + } |
| 41 | +};`, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: 'Generic component vModel', |
| 45 | + from: `const a = { setup: () => { return () => <MyComponent vModel={a.b} /> } }`, |
| 46 | + to: `import { getCurrentInstance } from "@vue/composition-api"; |
| 47 | +const a = { |
| 48 | + setup: () => { |
| 49 | + return () => <MyComponent model={{ |
| 50 | + value: a.b, |
| 51 | + callback: $$v => { |
| 52 | + getCurrentInstance().$set(a, "b", $$v); |
| 53 | + } |
| 54 | + }} />; |
| 55 | + } |
| 56 | +};`, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'Component vModel_number', |
| 60 | + from: `const a = { setup: () => { return () => <MyComponent vModel_number={a.b} /> } }`, |
| 61 | + to: `import { getCurrentInstance } from "@vue/composition-api"; |
| 62 | +const a = { |
| 63 | + setup: () => { |
| 64 | + return () => <MyComponent model={{ |
| 65 | + value: a.b, |
| 66 | + callback: $$v => { |
| 67 | + getCurrentInstance().$set(a, "b", getCurrentInstance()._n($$v)); |
| 68 | + } |
| 69 | + }} />; |
| 70 | + } |
| 71 | +};`, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'Ignore outside of setup()', |
| 75 | + from: `const A = <MyComponent vModel={a[b]} />`, |
| 76 | + to: `const A = <MyComponent model={{ |
| 77 | + value: a[b], |
| 78 | + callback: $$v => { |
| 79 | + this.$set(a, b, $$v); |
| 80 | + } |
| 81 | +}} />;`, |
| 82 | + }, |
| 83 | +] |
| 84 | + |
| 85 | +tests.map(({ name, from, to, NODE_ENV }) => { |
| 86 | + test.serial(name, async t => { |
| 87 | + const nodeEnvCopy = process.env.NODE_ENV |
| 88 | + if (NODE_ENV) { |
| 89 | + process.env.NODE_ENV = NODE_ENV |
| 90 | + } |
| 91 | + t.is(await transpile(from), to) |
| 92 | + if (NODE_ENV) { |
| 93 | + process.env.NODE_ENV = nodeEnvCopy |
| 94 | + } |
| 95 | + }) |
| 96 | +}) |
0 commit comments