Skip to content

Commit

Permalink
Merge pull request #468 from yintyuan/release-3.6.8-app
Browse files Browse the repository at this point in the history
feat: taroize补充plugin路径转换的测试用例,以及对script.test.ts测试的优化
  • Loading branch information
Vector-Hope authored Dec 6, 2023
2 parents 8f6de88 + 4940383 commit 62b4136
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 61 deletions.
52 changes: 39 additions & 13 deletions packages/taroize/__tests__/__snapshots__/script.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
}
};
Component({});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
@withWeapp(cacheOptions.getOptionsFromCache())
class _C extends React.Component {
render() {
return <View>{wxs_date.date}</View>;
Expand All @@ -30,7 +30,7 @@ module.exports = _C;
`;

exports[`parseScript app.js 1`] = `
import "@tarojs/components";
import { Block } from "@tarojs/components";
import React from "react";
import Taro from "@tarojs/taro";
const {
Expand All @@ -51,7 +51,7 @@ export default App;
`;

exports[`parseScript getAPP || getCurrentPages 1`] = `
import "@tarojs/components";
import { Block } from "@tarojs/components";
import React from "react";
import Taro from "@tarojs/taro";
const {
Expand All @@ -72,25 +72,51 @@ class App extends React.Component {
export default App;
`;

exports[`parseScript page页面通过插件url跳转到插件页面 1`] = `
import { Block, Button } from "@tarojs/components";
import React from "react";
import Taro from "@tarojs/taro";
const {
default: withWeapp
} = require("@tarojs/with-weapp");
Page({
pluginUrl() {
Taro.navigateTo({
url: "/hello-plugin/pages/hello-page"
});
}
});
@withWeapp(cacheOptions.getOptionsFromCache())
class _C extends React.Component {
render() {
return <Button onClick={this.pluginUrl}>跳转到plugin</Button>;
}
}
export default _C;
`;

exports[`parseScript wxml expression 1`] = `
import { Block, View } from "@tarojs/components";
import React from "react";
import Taro from "@tarojs/taro";
const {
default: withWeapp
} = require("@tarojs/with-weapp");
App({
Page({
data: {
srt: 'Hello Word!'
}
});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
@withWeapp(cacheOptions.getOptionsFromCache())
class _C extends React.Component {
render() {
return this.props.children;
const {
str
} = this.data;
return <View>{str}</View>;
}
}
export default App;
export default _C;
`;

exports[`parseScript wxml jsxText 1`] = `
Expand All @@ -100,12 +126,12 @@ import Taro from "@tarojs/taro";
const {
default: withWeapp
} = require("@tarojs/with-weapp");
App({});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
Page({});
@withWeapp(cacheOptions.getOptionsFromCache())
class _C extends React.Component {
render() {
return this.props.children;
return <Block>123</Block>;
}
}
export default App;
export default _C;
`;
129 changes: 81 additions & 48 deletions packages/taroize/__tests__/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as t from '@babel/types'

import { parseScript } from '../src/script'
import { parseWXML } from '../src/wxml'
import { parse } from '../src'
import { generateMinimalEscapeCode, removeBackslashesSerializer } from './util'

expect.addSnapshotSerializer(removeBackslashesSerializer)
Expand All @@ -11,27 +8,23 @@ jest.mock('fs', () => ({
appendFile: jest.fn(),
}))

interface Option {
script: string
scriptPath: string
wxml: object
wxses: []
refIds: Set<string>
isApp: boolean
}
describe('parseScript', () => {
let option: any

const option: Option = {
script: '',
scriptPath: '',
wxml: {},
wxses: [],
refIds: new Set<string>(),
isApp: false
}
beforeAll(() => {
option = {
script: '',
scriptPath: '',
wxml: '',
path: '',
rootPath: '',
framework: 'react',
isApp: false,
logFilePath: '',
pluginInfo: {}
}
})

describe('parseScript', () => {
// 调用 parseWXML 需要用到的参数,因parseWXML会先获取缓存,所有每个用例的path需要保持其唯一性
let path = ''
// 解析app.js
test('app.js', () => {
option.script = `
Expand All @@ -41,10 +34,12 @@ describe('parseScript', () => {
}
})
`
option.wxml = { type: 'NullLiteral' }
const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true)
// app.js 没有对应的wxml文件
option.wxml = undefined
option.path = 'app_js'
option.isApp = true
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(ast).toBeTruthy()
expect(code).toMatchSnapshot()
})

Expand All @@ -58,38 +53,39 @@ describe('parseScript', () => {
}
})
`
option.wxml = { type: 'NullLiteral' }
const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true)
// app.js 没有对应的wxml文件
option.wxml = undefined
option.path = 'getApp_or_getCurrentPages'
option.isApp = true
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})

// 当wxml是纯文本 parseWXML的返回值 { wxml } 影响js转换
// 当wxml是纯文本
test('wxml jsxText', () => {
option.script = `App({})`
const wxmlStr = `123`
// 调用 parseWXML 会首先获取缓存,所以第一个参数path建议保持唯一
path = 'wxml_jsxText'
const { wxml } = parseWXML(path, wxmlStr)
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true)
option.script = `Page({})`
option.wxml = `123`
option.path = 'wxml_jsxText'
option.isApp = false
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})

// 当wxml存在变量 parseWXML的返回值 { wxml } 影响js转换
// 当wxml存在变量
test('wxml expression', () => {
option.script = `
App({
Page({
data:{
srt:'Hello Word!',
}
})
`
const wxmlStr = `<view>{{str}}</view>`
// 调用 parseWXML 会首先获取缓存,所以第一个参数path建议保持唯一
path = 'wxml_expression'
const { wxml } = parseWXML(path, wxmlStr)
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true)
option.wxml = '<view>{{str}}</view>'
option.path = 'wxml_expression'
option.isApp = false
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})
Expand All @@ -101,11 +97,11 @@ describe('parseScript', () => {
date:'2023-11-11'
}
*/
const wxmlStr = `
option.wxml = `
<wxs src="../../utils/req.wxs" module="wxs_date"/>
<view>{{wxs_date.date}}</view>
`
path = 'CommonJS_module_exports'
option.path = 'CommonJS_module_exports'
option.script = `
module.exports = {
data: {
Expand All @@ -116,12 +112,49 @@ describe('parseScript', () => {
},
}
`
const { wxml, wxses }:any = parseWXML(path,wxmlStr)
option.wxml = wxml
option.wxses = wxses
const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true)
option.isApp = false
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(ast).toBeTruthy()
expect(code).toMatchSnapshot()
})

test('page页面通过插件url跳转到插件页面', () => {
/**
* plugin的目录结构以及内容
* /plugin/plugin.json:"{
"pages": {
"hello-page": "pages/hello-page"
},
"main": "index.js"
}",
/plugin/pages/hello-page.wxml:"<text>This is a plugin page!</text>",
/plugin/pages/hello-page.js:"Page({})",
*/
// 插件的解析在 taro-cli-convertor 中进行
option.pluginInfo = {
pluginRoot: '/wxProject/plugin',
pluginName: 'hello-plugin',
pages: new Set(['pages/hello-page']),
pagesMap: new Map([['hello-page', 'pages/hello-page']]),
publicComponents: {},
entryFilePath: '/wxProject/plugin/index.js',
}
option.path = '/wxProject/miniprogram/pages/index'
option.rootPath = '/wxProject/miniprogram'
option.script = `
Page({
pluginUrl() {
wx.navigateTo({
url: 'plugin://hello-plugin/hello-page',
})
}
})`
option.scriptPath = '/wxProject/miniprogram/pages/index/index.js'
option.wxml = `<button bindtap="pluginUrl">跳转到plugin</button>`
option.isApp = false
const { ast } = parse(option)
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})
})

0 comments on commit 62b4136

Please sign in to comment.