Skip to content

Commit 22a2e59

Browse files
committed
test(compiler): complete templateRef
1 parent 24d0f0d commit 22a2e59

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

packages/compiler/test/transforms/__snapshots__/transformTemplateRef.spec.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ exports[`compiler: template ref transform > dynamic ref 1`] = `
1010
"
1111
`;
1212

13+
exports[`compiler: template ref transform > ref + v-for 1`] = `
14+
"
15+
const _setTemplateRef = _createTemplateRefSetter()
16+
const n0 = _createFor(() => ([1,2,3]), (_for_item0) => {
17+
const n2 = t0()
18+
let r2
19+
_renderEffect(() => r2 = _setTemplateRef(n2, foo, r2, true))
20+
return n2
21+
}, null, 4)
22+
return n0
23+
"
24+
`;
25+
26+
exports[`compiler: template ref transform > ref + v-if 1`] = `
27+
"
28+
const _setTemplateRef = _createTemplateRefSetter()
29+
const n0 = _createIf(() => (true), () => {
30+
const n2 = t0()
31+
let r2
32+
_renderEffect(() => r2 = _setTemplateRef(n2, foo, r2))
33+
return n2
34+
})
35+
return n0
36+
"
37+
`;
38+
1339
exports[`compiler: template ref transform > static ref 1`] = `
1440
"
1541
const _setTemplateRef = _createTemplateRefSetter()

packages/compiler/test/transforms/transformTemplateRef.spec.ts

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ import {
55
transformChildren,
66
transformElement,
77
transformTemplateRef,
8+
transformVFor,
9+
transformVIf,
10+
type ForIRNode,
11+
type IfIRNode,
812
} from '../../src'
913

1014
import { makeCompile } from './_utils'
1115

1216
const compileWithTransformRef = makeCompile({
13-
nodeTransforms: [transformTemplateRef, transformElement, transformChildren],
17+
nodeTransforms: [
18+
transformVIf,
19+
transformVFor,
20+
transformTemplateRef,
21+
transformElement,
22+
transformChildren,
23+
],
1424
})
1525

1626
describe('compiler: template ref transform', () => {
@@ -72,49 +82,49 @@ describe('compiler: template ref transform', () => {
7282
expect(code).contains('_setTemplateRef(n0, foo, r0)')
7383
})
7484

75-
// test('ref + v-if', () => {
76-
// const { ir, code } = compileWithTransformRef(
77-
// `<div ref="foo" v-if="true" />`,
78-
// )
85+
test('ref + v-if', () => {
86+
const { ir, code } = compileWithTransformRef(
87+
`<div ref={foo} v-if={true} />`,
88+
)
89+
expect(code).toMatchSnapshot()
7990

80-
// expect(ir.block.operation).lengthOf(1)
81-
// expect(ir.block.operation[0].type).toBe(IRNodeTypes.IF)
91+
const op = ir.block.dynamic.children[0].operation as IfIRNode
92+
expect(op.type).toBe(IRNodeTypes.IF)
8293

83-
// const { positive } = ir.block.operation[0] as IfIRNode
84-
// expect(positive.operation).toMatchObject([
85-
// {
86-
// type: IRNodeTypes.SET_TEMPLATE_REF,
87-
// element: 2,
88-
// value: {
89-
// content: 'foo',
90-
// isStatic: true,
91-
// },
92-
// effect: false,
93-
// },
94-
// ])
95-
// expect(code).matchSnapshot()
96-
// expect(code).contains('_setRef(n2, "foo")')
97-
// })
94+
const { positive } = op
95+
expect(positive.effect[0].operations).toMatchObject([
96+
{
97+
type: IRNodeTypes.SET_TEMPLATE_REF,
98+
element: 2,
99+
value: {
100+
content: 'foo',
101+
isStatic: false,
102+
},
103+
effect: true,
104+
},
105+
])
106+
expect(code).contains('_setTemplateRef(n2, foo, r2)')
107+
})
98108

99-
// test('ref + v-for', () => {
100-
// const { ir, code } = compileWithTransformRef(
101-
// `<div ref="foo" v-for="item in [1,2,3]" />`,
102-
// )
109+
test('ref + v-for', () => {
110+
const { ir, code } = compileWithTransformRef(
111+
`<div ref={foo} v-for={item in [1,2,3]} />`,
112+
)
113+
expect(code).toMatchSnapshot()
103114

104-
// const { render } = ir.block.operation[0] as ForIRNode
105-
// expect(render.operation).toMatchObject([
106-
// {
107-
// type: IRNodeTypes.SET_TEMPLATE_REF,
108-
// element: 2,
109-
// value: {
110-
// content: 'foo',
111-
// isStatic: true,
112-
// },
113-
// refFor: true,
114-
// effect: false,
115-
// },
116-
// ])
117-
// expect(code).matchSnapshot()
118-
// expect(code).contains('_setRef(n2, "foo", void 0, true)')
119-
// })
115+
const { render } = ir.block.dynamic.children[0].operation as ForIRNode
116+
expect(render.effect[0].operations).toMatchObject([
117+
{
118+
type: IRNodeTypes.SET_TEMPLATE_REF,
119+
element: 2,
120+
value: {
121+
content: 'foo',
122+
isStatic: false,
123+
},
124+
refFor: true,
125+
effect: true,
126+
},
127+
])
128+
expect(code).contains('_setTemplateRef(n2, foo, r2, true)')
129+
})
120130
})

0 commit comments

Comments
 (0)