|
1 | 1 | import { makeCompile } from './_utils'
|
2 |
| -import { transformChildren, transformElement, transformVModel } from '../../src' |
| 2 | +import { |
| 3 | + IRNodeTypes, |
| 4 | + transformChildren, |
| 5 | + transformElement, |
| 6 | + transformVModel, |
| 7 | +} from '../../src' |
3 | 8 | import { BindingTypes, DOMErrorCodes } from '@vue/compiler-dom'
|
4 | 9 |
|
5 | 10 | const compileWithVModel = makeCompile({
|
@@ -198,4 +203,169 @@ describe('compiler: vModel transform', () => {
|
198 | 203 |
|
199 | 204 | expect(code).toMatchSnapshot()
|
200 | 205 | })
|
| 206 | + |
| 207 | + describe('component', () => { |
| 208 | + test('v-model for component should work', () => { |
| 209 | + const { code, ir } = compileWithVModel('<Comp v-model="foo" />') |
| 210 | + expect(code).toMatchSnapshot() |
| 211 | + expect(code).contains( |
| 212 | + `modelValue: () => (_ctx.foo), |
| 213 | + "onUpdate:modelValue": () => $event => (_ctx.foo = $event)`, |
| 214 | + ) |
| 215 | + expect(ir.block.operation).toMatchObject([ |
| 216 | + { |
| 217 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 218 | + tag: 'Comp', |
| 219 | + props: [ |
| 220 | + [ |
| 221 | + { |
| 222 | + key: { content: 'modelValue', isStatic: true }, |
| 223 | + model: true, |
| 224 | + modelModifiers: [], |
| 225 | + values: [{ content: 'foo', isStatic: false }], |
| 226 | + }, |
| 227 | + ], |
| 228 | + ], |
| 229 | + }, |
| 230 | + ]) |
| 231 | + }) |
| 232 | + |
| 233 | + test('v-model with arguments for component should work', () => { |
| 234 | + const { code, ir } = compileWithVModel('<Comp v-model:bar="foo" />') |
| 235 | + expect(code).toMatchSnapshot() |
| 236 | + expect(code).contains( |
| 237 | + `bar: () => (_ctx.foo), |
| 238 | + "onUpdate:bar": () => $event => (_ctx.foo = $event)`, |
| 239 | + ) |
| 240 | + expect(ir.block.operation).toMatchObject([ |
| 241 | + { |
| 242 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 243 | + tag: 'Comp', |
| 244 | + props: [ |
| 245 | + [ |
| 246 | + { |
| 247 | + key: { content: 'bar', isStatic: true }, |
| 248 | + model: true, |
| 249 | + modelModifiers: [], |
| 250 | + values: [{ content: 'foo', isStatic: false }], |
| 251 | + }, |
| 252 | + ], |
| 253 | + ], |
| 254 | + }, |
| 255 | + ]) |
| 256 | + }) |
| 257 | + |
| 258 | + test('v-model with dynamic arguments for component should work', () => { |
| 259 | + const { code, ir } = compileWithVModel('<Comp v-model:[arg]="foo" />') |
| 260 | + expect(code).toMatchSnapshot() |
| 261 | + expect(code).contains( |
| 262 | + `[_ctx.arg]: () => (_ctx.foo), |
| 263 | + ["onUpdate:" + _ctx.arg]: () => $event => (_ctx.foo = $event)`, |
| 264 | + ) |
| 265 | + expect(ir.block.operation).toMatchObject([ |
| 266 | + { |
| 267 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 268 | + tag: 'Comp', |
| 269 | + props: [ |
| 270 | + [ |
| 271 | + { |
| 272 | + key: { content: 'arg', isStatic: false }, |
| 273 | + values: [{ content: 'foo', isStatic: false }], |
| 274 | + model: true, |
| 275 | + modelModifiers: [], |
| 276 | + }, |
| 277 | + ], |
| 278 | + ], |
| 279 | + }, |
| 280 | + ]) |
| 281 | + }) |
| 282 | + |
| 283 | + test('v-model for component should generate modelModifiers', () => { |
| 284 | + const { code, ir } = compileWithVModel( |
| 285 | + '<Comp v-model.trim.bar-baz="foo" />', |
| 286 | + ) |
| 287 | + expect(code).toMatchSnapshot() |
| 288 | + expect(code).contain( |
| 289 | + `modelModifiers: () => ({ trim: true, "bar-baz": true })`, |
| 290 | + ) |
| 291 | + expect(ir.block.operation).toMatchObject([ |
| 292 | + { |
| 293 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 294 | + tag: 'Comp', |
| 295 | + props: [ |
| 296 | + [ |
| 297 | + { |
| 298 | + key: { content: 'modelValue', isStatic: true }, |
| 299 | + values: [{ content: 'foo', isStatic: false }], |
| 300 | + model: true, |
| 301 | + modelModifiers: ['trim', 'bar-baz'], |
| 302 | + }, |
| 303 | + ], |
| 304 | + ], |
| 305 | + }, |
| 306 | + ]) |
| 307 | + }) |
| 308 | + |
| 309 | + test('v-model with arguments for component should generate modelModifiers', () => { |
| 310 | + const { code, ir } = compileWithVModel( |
| 311 | + '<Comp v-model:foo.trim="foo" v-model:bar.number="bar" />', |
| 312 | + ) |
| 313 | + expect(code).toMatchSnapshot() |
| 314 | + expect(code).contain(`fooModifiers: () => ({ trim: true })`) |
| 315 | + expect(code).contain(`barModifiers: () => ({ number: true })`) |
| 316 | + expect(ir.block.operation).toMatchObject([ |
| 317 | + { |
| 318 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 319 | + tag: 'Comp', |
| 320 | + props: [ |
| 321 | + [ |
| 322 | + { |
| 323 | + key: { content: 'foo', isStatic: true }, |
| 324 | + values: [{ content: 'foo', isStatic: false }], |
| 325 | + model: true, |
| 326 | + modelModifiers: ['trim'], |
| 327 | + }, |
| 328 | + { |
| 329 | + key: { content: 'bar', isStatic: true }, |
| 330 | + values: [{ content: 'bar', isStatic: false }], |
| 331 | + model: true, |
| 332 | + modelModifiers: ['number'], |
| 333 | + }, |
| 334 | + ], |
| 335 | + ], |
| 336 | + }, |
| 337 | + ]) |
| 338 | + }) |
| 339 | + |
| 340 | + test('v-model with dynamic arguments for component should generate modelModifiers ', () => { |
| 341 | + const { code, ir } = compileWithVModel( |
| 342 | + '<Comp v-model:[foo].trim="foo" v-model:[bar].number="bar" />', |
| 343 | + ) |
| 344 | + expect(code).toMatchSnapshot() |
| 345 | + expect(code).contain(`[_ctx.foo + "Modifiers"]: () => ({ trim: true })`) |
| 346 | + expect(code).contain(`[_ctx.bar + "Modifiers"]: () => ({ number: true })`) |
| 347 | + expect(ir.block.operation).toMatchObject([ |
| 348 | + { |
| 349 | + type: IRNodeTypes.CREATE_COMPONENT_NODE, |
| 350 | + tag: 'Comp', |
| 351 | + props: [ |
| 352 | + [ |
| 353 | + { |
| 354 | + key: { content: 'foo', isStatic: false }, |
| 355 | + values: [{ content: 'foo', isStatic: false }], |
| 356 | + model: true, |
| 357 | + modelModifiers: ['trim'], |
| 358 | + }, |
| 359 | + { |
| 360 | + key: { content: 'bar', isStatic: false }, |
| 361 | + values: [{ content: 'bar', isStatic: false }], |
| 362 | + model: true, |
| 363 | + modelModifiers: ['number'], |
| 364 | + }, |
| 365 | + ], |
| 366 | + ], |
| 367 | + }, |
| 368 | + ]) |
| 369 | + }) |
| 370 | + }) |
201 | 371 | })
|
0 commit comments