Skip to content

Commit

Permalink
test(transformer): 增加循环中使用 ref 的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Dec 25, 2018
1 parent 117e5d3 commit ee3f07e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/taro-transformer-wx/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ describe('ref', () => {
expect(template).toMatch(` + index}}"`)
})

test('内置组件 + 其它复杂表达式', () => {
const { template, ast, code } = transform({
...baseOptions,
isRoot: true,
code: buildComponent(`
const array = [{ list: [] }, { list: [] }]
return (
<View>{array.map((item, index) => {
return <CoverView style={item} ref={(node) => this.coverView[index] = node}>{item.list.map(item2 => <Text>{item2}</Text>)}</CoverView>
})}</View>
)
`, `coverView = []`)
})
// console.log(instance)
const instance = evalClass(ast)
expect(instance.coverView).toEqual(['test-ref', 'test-ref'])
})

test('自定义组件组件', () => {
const { template, ast, code } = transform({
...baseOptions,
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,11 +1109,13 @@ export class RenderParser {
if (ref.type === 'component') {
args.push(t.stringLiteral('component'))
}
const callGetElementById = t.callExpression(t.identifier(GEL_ELEMENT_BY_ID), args)
const refDecl = buildConstVariableDeclaration(refDeclName,
t.logicalExpression('&&', t.identifier('__scope'), t.callExpression(t.identifier(GEL_ELEMENT_BY_ID), args))
process.env.NODE_ENV === 'test' ? callGetElementById : t.logicalExpression('&&', t.identifier('__scope'), callGetElementById)
)
const callRef = t.callExpression(ref.fn, [t.identifier(refDeclName)])
const callRefFunc = t.expressionStatement(
t.logicalExpression('&&', t.identifier(refDeclName), t.callExpression(ref.fn, [t.identifier(refDeclName)]))
process.env.NODE_ENV === 'test' ? callRef : t.logicalExpression('&&', t.identifier(refDeclName), callRef)
)
body.push(refDecl, callRefFunc)
}
Expand Down

0 comments on commit ee3f07e

Please sign in to comment.