|
1 |
| -// TODO: add tests for this transform |
| 1 | +import { |
| 2 | + DynamicFlag, |
| 3 | + type ForIRNode, |
| 4 | + IRNodeTypes, |
| 5 | + type IfIRNode, |
| 6 | + transformChildren, |
| 7 | + transformElement, |
| 8 | + transformRef, |
| 9 | + transformVFor, |
| 10 | + transformVIf, |
| 11 | +} from '../../src' |
| 12 | +import { makeCompile } from './_utils' |
| 13 | + |
| 14 | +const compileWithTransformRef = makeCompile({ |
| 15 | + nodeTransforms: [ |
| 16 | + transformVIf, |
| 17 | + transformVFor, |
| 18 | + transformRef, |
| 19 | + transformElement, |
| 20 | + transformChildren, |
| 21 | + ], |
| 22 | +}) |
| 23 | + |
2 | 24 | describe('compiler: template ref transform', () => {
|
3 |
| - test.todo('basic') |
| 25 | + test('static ref', () => { |
| 26 | + const { ir, code } = compileWithTransformRef(`<div ref="foo" />`) |
| 27 | + |
| 28 | + expect(ir.block.dynamic.children[0]).toMatchObject({ |
| 29 | + id: 0, |
| 30 | + flags: DynamicFlag.REFERENCED, |
| 31 | + }) |
| 32 | + expect(ir.template).toEqual(['<div></div>']) |
| 33 | + expect(ir.block.operation).lengthOf(1) |
| 34 | + expect(ir.block.operation[0]).toMatchObject({ |
| 35 | + type: IRNodeTypes.SET_REF, |
| 36 | + element: 0, |
| 37 | + value: { |
| 38 | + content: 'foo', |
| 39 | + isStatic: true, |
| 40 | + loc: { |
| 41 | + start: { line: 1, column: 10, offset: 9 }, |
| 42 | + end: { line: 1, column: 15, offset: 14 }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | + |
| 47 | + expect(code).matchSnapshot() |
| 48 | + expect(code).contains('_setRef(n0, "foo")') |
| 49 | + }) |
| 50 | + |
| 51 | + test('dynamic ref', () => { |
| 52 | + const { ir, code } = compileWithTransformRef(`<div :ref="foo" />`) |
| 53 | + |
| 54 | + expect(ir.block.dynamic.children[0]).toMatchObject({ |
| 55 | + id: 0, |
| 56 | + flags: DynamicFlag.REFERENCED, |
| 57 | + }) |
| 58 | + expect(ir.template).toEqual(['<div></div>']) |
| 59 | + expect(ir.block.operation).lengthOf(1) |
| 60 | + expect(ir.block.operation[0]).toMatchObject({ |
| 61 | + type: IRNodeTypes.SET_REF, |
| 62 | + element: 0, |
| 63 | + value: { |
| 64 | + content: 'foo', |
| 65 | + isStatic: false, |
| 66 | + loc: { |
| 67 | + start: { line: 1, column: 12, offset: 11 }, |
| 68 | + end: { line: 1, column: 15, offset: 14 }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }) |
| 72 | + expect(code).matchSnapshot() |
| 73 | + expect(code).contains('_setRef(n0, _ctx.foo)') |
| 74 | + }) |
| 75 | + |
| 76 | + test('ref + v-if', () => { |
| 77 | + const { ir, code } = compileWithTransformRef( |
| 78 | + `<div ref="foo" v-if="true" />`, |
| 79 | + ) |
| 80 | + |
| 81 | + expect(ir.block.operation).lengthOf(1) |
| 82 | + expect(ir.block.operation[0].type).toBe(IRNodeTypes.IF) |
| 83 | + |
| 84 | + const { positive } = ir.block.operation[0] as IfIRNode |
| 85 | + |
| 86 | + expect(positive.operation).lengthOf(1) |
| 87 | + expect(positive.operation[0]).toMatchObject({ |
| 88 | + type: IRNodeTypes.SET_REF, |
| 89 | + element: 2, |
| 90 | + value: { |
| 91 | + content: 'foo', |
| 92 | + isStatic: true, |
| 93 | + loc: { |
| 94 | + start: { line: 1, column: 10, offset: 9 }, |
| 95 | + end: { line: 1, column: 15, offset: 14 }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }) |
| 99 | + |
| 100 | + expect(code).matchSnapshot() |
| 101 | + expect(code).contains('_setRef(n2, "foo")') |
| 102 | + }) |
| 103 | + |
| 104 | + test('ref + v-for', () => { |
| 105 | + const { ir, code } = compileWithTransformRef( |
| 106 | + `<div ref="foo" v-for="item in [1,2,3]" />`, |
| 107 | + ) |
| 108 | + |
| 109 | + const { render } = ir.block.operation[0] as ForIRNode |
| 110 | + expect(render.operation).lengthOf(1) |
| 111 | + expect(render.operation[0]).toMatchObject({ |
| 112 | + type: IRNodeTypes.SET_REF, |
| 113 | + element: 2, |
| 114 | + value: { |
| 115 | + content: 'foo', |
| 116 | + isStatic: true, |
| 117 | + loc: { |
| 118 | + start: { line: 1, column: 10, offset: 9 }, |
| 119 | + end: { line: 1, column: 15, offset: 14 }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + refFor: true, |
| 123 | + }) |
| 124 | + expect(code).matchSnapshot() |
| 125 | + expect(code).contains('_setRef(n2, "foo", true)') |
| 126 | + }) |
4 | 127 | })
|
0 commit comments