-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer): 只有 render 开头的函数才是 render props
close #4180
- Loading branch information
Showing
2 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
176 changes: 176 additions & 0 deletions
176
packages/taro-transformer-wx/__tests__/render-props.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import transform from '../src' | ||
import { buildComponent, baseCode, baseOptions, evalClass, Custom, prettyPrint } from './utils' | ||
|
||
describe('render props', () => { | ||
test('从 props 而来的函数', () => { | ||
const { template, ast, code } = transform({ | ||
...baseOptions, | ||
isRoot: true, | ||
code: buildComponent(` | ||
const {goods } = this.props; | ||
return ( | ||
<View | ||
> | ||
{goods.sku_type && ( | ||
<View> | ||
{goods.replace('')} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
`, `coverView = [];text = []`) | ||
}) | ||
// console.log(instance) | ||
expect(template).toMatch( | ||
prettyPrint(` | ||
<block> | ||
<view> | ||
<block wx:if="{{goods.sku_type}}"> | ||
<view>{{anonymousState__temp}}</view> | ||
</block> | ||
</view> | ||
</block> | ||
`) | ||
) | ||
}) | ||
|
||
test('从 props 而来的成员表达式函数', () => { | ||
const { template, ast, code } = transform({ | ||
...baseOptions, | ||
isRoot: true, | ||
code: buildComponent(` | ||
const {goods } = this.props; | ||
return ( | ||
<View | ||
> | ||
{goods.sku_type && ( | ||
<View> | ||
{goods.sku_type.replace('')} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
`, `coverView = [];text = []`) | ||
}) | ||
// console.log(instance) | ||
expect(template).toMatch( | ||
prettyPrint(` | ||
<block> | ||
<view> | ||
<block wx:if="{{goods.sku_type}}"> | ||
<view>{{anonymousState__temp}}</view> | ||
</block> | ||
</view> | ||
</block> | ||
`) | ||
) | ||
}) | ||
|
||
test('从 props 而来的 render props', () => { | ||
const { template, ast, code } = transform({ | ||
...baseOptions, | ||
isRoot: true, | ||
code: buildComponent(` | ||
const {goods, renderGoods } = this.props; | ||
return ( | ||
<View | ||
> | ||
{goods.sku_type && ( | ||
<View> | ||
{renderGoods()} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
`, `coverView = [];text = []`) | ||
}) | ||
// console.log(instance) | ||
expect(template).toMatch( | ||
prettyPrint(` | ||
<block> | ||
<view> | ||
<block wx:if="{{goods.sku_type}}"> | ||
<view> | ||
<slot name="goods"></slot> | ||
</view> | ||
</block> | ||
</view> | ||
</block> | ||
`) | ||
) | ||
}) | ||
|
||
test('从 props 而来的 render props 带参数', () => { | ||
const { template, ast, code } = transform({ | ||
...baseOptions, | ||
isRoot: true, | ||
code: buildComponent(` | ||
const {goods, renderGoods } = this.props; | ||
return ( | ||
<View | ||
> | ||
{goods.sku_type && ( | ||
<View> | ||
{renderGoods(goods)} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
`, `coverView = [];text = []`) | ||
}) | ||
// console.log(instance) | ||
expect(template).toMatch( | ||
prettyPrint(` | ||
<block> | ||
<view> | ||
<block wx:if="{{goods.sku_type}}"> | ||
<view> | ||
<slot name="goods"></slot> | ||
</view> | ||
</block> | ||
</view> | ||
</block> | ||
`) | ||
) | ||
}) | ||
|
||
test('从 props 多层解构而来的 render props 带参数', () => { | ||
const { template, ast, code } = transform({ | ||
...baseOptions, | ||
isRoot: true, | ||
code: buildComponent(` | ||
const props = this.props | ||
const {goods, renderGoods } = props; | ||
return ( | ||
<View | ||
> | ||
{goods.sku_type && ( | ||
<View> | ||
{renderGoods(goods)} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
`, `coverView = [];text = []`) | ||
}) | ||
// console.log(instance) | ||
expect(template).toMatch( | ||
prettyPrint(` | ||
<block> | ||
<view> | ||
<block wx:if="{{goods.sku_type}}"> | ||
<view> | ||
<slot name="goods"></slot> | ||
</view> | ||
</block> | ||
</view> | ||
</block> | ||
`) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters