Skip to content

Commit

Permalink
test(loader): 添加更多测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2019
1 parent 5111f7b commit c0dbc22
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 9 deletions.
33 changes: 33 additions & 0 deletions packages/taro-loader/__tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ describe('app loader', () => {
App(createReactApp(React, app, ReactDOM.render));
`))
})

test('没有 export default 应该报错', async () => {
const result = await compile('basic_1.txt', { type: 'app', framework: 'react' })
// expect(true).toBe(false)
expect(result).toBe(pretty(`
import ReactDOM from "react-dom";
import React from "react";
import { createReactApp } from "@tarojs/runtime";
import { app } from "./app";
App(createReactApp(React, app, ReactDOM.render));
`))
})

test('不重复添加依赖', async () => {
const result = await compile('react-imported.txt', { type: 'app', framework: 'react' })
expect(result).toBe(pretty(`
import ReactDOM from "react-dom";
import { createReactApp } from "@tarojs/runtime";
import React from "react";
import { app } from "./app";
App(createReactApp(React, app, ReactDOM.render));
`))
})
})

describe('vue', () => {
Expand All @@ -26,5 +49,15 @@ describe('app loader', () => {
App(createVueApp(Vue, app));
`))
})

test('不重复添加依赖', async () => {
const result = await compile('vue-imported.txt', { type: 'app', framework: 'vue' })
expect(result).toBe(pretty(`
import { createVueApp } from "@tarojs/runtime";
import Vue from "vue";
import { app } from "./app";
App(createVueApp(Vue, app));
`))
})
})
})
15 changes: 11 additions & 4 deletions packages/taro-loader/__tests__/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ export async function compile (fixture, { type, framework }) {
module: {
rules: [{
test: /\.txt$/,
use: {
loader: path.resolve(__dirname, `../lib/${type}.js`),
options: {
framework
use: [
{
loader: path.resolve(__dirname, `../lib/${type}.js`),
options: {
framework
}
}
]
}, {
test: /\.js$/,
use: {
loader: 'babel-loader'
}
}]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 'b'
4 changes: 4 additions & 0 deletions packages/taro-loader/__tests__/fixtures/react-imported-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { app } from './app'
export default app
3 changes: 3 additions & 0 deletions packages/taro-loader/__tests__/fixtures/react-imported.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react'
import { app } from './app'
export default app
9 changes: 9 additions & 0 deletions packages/taro-loader/__tests__/fixtures/react.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

class A extends Component {
render () {
return React.createElement('div')
}
}

export default connect({})(A)
9 changes: 9 additions & 0 deletions packages/taro-loader/__tests__/fixtures/react2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

class A extends Component {
render () {
return React.createElement('div')
}
}

export default A
9 changes: 9 additions & 0 deletions packages/taro-loader/__tests__/fixtures/syntax-tsx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const a: string = ''

class A extends Component {
render () {
return <div className={a} />
}
}

export default A
3 changes: 3 additions & 0 deletions packages/taro-loader/__tests__/fixtures/vue-imported.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Vue from 'vue'
import { app } from './app'
export default app
33 changes: 33 additions & 0 deletions packages/taro-loader/__tests__/general.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { compile } from './compile'

describe('general', () => {
describe('not export default', () => {
test('app', async () => {
async function check () {
await compile('not-export-default.txt', { type: 'app', framework: 'vue' })
}
await expect(check()).rejects.toBeTruthy()
})

test('page', async () => {
async function check () {
await compile('not-export-default.txt', { type: 'page', framework: 'vue' })
}
await expect(check()).rejects.toBeTruthy()
})

test('react', async () => {
async function check () {
await compile('not-export-default.txt', { type: 'app', framework: 'react' })
}
await expect(check()).rejects.toBeTruthy()
})
})

describe.skip('syntax', () => {
test('tsx', async () => {
const result = await compile('syntax-tsx.txt', { type: 'page', framework: 'react' })
expect(result).toBe('')
})
})
})
46 changes: 46 additions & 0 deletions packages/taro-loader/__tests__/page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,51 @@ describe('page loader', () => {
Page(createPageConfig(app));
`))
})

test('react component', async () => {
const result = await compile('react.txt', { type: 'page', framework: 'react' })

expect(result).toBe(pretty(`
import { createPageConfig } from "@tarojs/runtime";
import React from "react";
class A extends Component {
render() {
return React.createElement("div");
}
}
Page(createPageConfig(connect({})(A)));
`))
})

test('react component 2', async () => {
const result = await compile('react2.txt', { type: 'page', framework: 'react' })

expect(result).toBe(pretty(`
import { createPageConfig } from "@tarojs/runtime";
import React from "react";
class A extends Component {
render() {
return React.createElement("div");
}
}
Page(createPageConfig(connect({})(A)));
`))
})
})

describe('vue', () => {
test('basic', async () => {
const result = await compile('basic_1.txt', { type: 'page', framework: 'vue' })

expect(result).toBe(pretty(`
import { createPageConfig } from "@tarojs/runtime";
import { app } from "./app";
Page(createPageConfig(app));
`))
})
})
})
13 changes: 10 additions & 3 deletions packages/taro-loader/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ class AppLoader extends Loader {

this.needToImportMainModule = true

let reactDOMImported = false

traverse(this.ast, {
Program: {
enter: (path) => {
reactDOMImported = !!path.scope.getBinding('ReactDOM')
},
exit: this.ensureMainModuleImported.bind(this)
}
})
Expand All @@ -31,9 +36,11 @@ class AppLoader extends Loader {

if (isReact) {
if (this.framework === 'react') {
this.insertToTheFront(
t.importDeclaration([t.importDefaultSpecifier(t.identifier('ReactDOM'))], t.stringLiteral('react-dom'))
)
if (!reactDOMImported) {
this.insertToTheFront(
t.importDeclaration([t.importDefaultSpecifier(t.identifier('ReactDOM'))], t.stringLiteral('react-dom'))
)
}

render = t.memberExpression(t.identifier('ReactDOM'), t.identifier('render'))
} else {
Expand Down
13 changes: 13 additions & 0 deletions packages/taro-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getOptions } from 'loader-utils'
import * as webpack from 'webpack'
import appLoader from './app'
import pageLoader from './page'

export default function (this: webpack.loader.LoaderContext, source: string) {
const options = getOptions(this)
if (options.type === 'app') {
appLoader.call(this, source)
} else {
pageLoader.call(this, source)
}
}
2 changes: 1 addition & 1 deletion packages/taro-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Loader {
}

if (this.exportDefaultDecl == null) {
this.context.emitError(`文件: ${this.context.resourcePath} 没有找到 export default 语句!`)
this.context.emitError(new Error(`文件: ${this.context.resourcePath} 没有找到 export default 语句!`))
}

return file
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-loader/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PageLoader extends Loader {
}

private injectReactComponent (classDecl: NodePath<t.ClassDeclaration | t.ClassExpression>) {
if (this.framework !== 'vue') {
if (this.framework === 'vue') {
return
}

Expand Down

0 comments on commit c0dbc22

Please sign in to comment.