Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang committed Sep 28, 2018
1 parent 3c7b5ba commit 421b07b
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 42 deletions.
15 changes: 15 additions & 0 deletions __fixtures__/invalidProps.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div></div>
</template>

<script>
export default {
data () {
return {
props: {
a: String
}
}
}
}
</script>
11 changes: 11 additions & 0 deletions __fixtures__/validProps.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div></div>
</template>

<script>
export default {
props: {
a: String
}
}
</script>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"node": ">=8"
},
"scripts": {
"test": "yarn lint && jest",
"test": "yarn lint && jest --verbose",
"build": "yarn lint && bili",
"postbuild": "find dist -type d -name __test__ -exec rm -r {} +",
"prepublishOnly": "yarn build",
Expand Down
36 changes: 36 additions & 0 deletions src/__test__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import traverse from '@babel/traverse'
import { getAST } from './utils'
import { isVueComponent, isPropsOption } from '../helpers'

describe('helpers', () => {
const ast1 = getAST('validProps.vue')
const ast2 = getAST('invalidProps.vue')

test('The default export should be a Vue component', () => {
traverse(ast1, {
ExportDefaultDeclaration(path: any) {
expect(isVueComponent(path.node)).toBe(true)
}
})
})

test('The props property is a Vue component option', () => {
traverse(ast1, {
ObjectProperty(path: any) {
if (path.node.key.name === 'props') {
expect(isPropsOption(path)).toBe(true)
}
}
})
})

test('The props property is not a Vue component option', () => {
traverse(ast2, {
ObjectProperty(path: any) {
if (path.node.key.name === 'props') {
expect(isPropsOption(path)).toBe(false)
}
}
})
})
})
9 changes: 2 additions & 7 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import vuese, { ParserOptions } from '../index'
import sfcToEstree from '../sfcToEstree'
import * as path from 'path'
import * as fs from 'fs'

const p = path.resolve(__dirname, '../../__fixtures__/common.vue')
const source = fs.readFileSync(p, 'utf-8')
import { getAST } from './utils'

test('get the correct props value', () => {
const ast = sfcToEstree(source)
const ast = getAST('common.vue')
const options: ParserOptions = {
onProp: origValue => {
expect(origValue).toEqual({ a: String })
Expand Down
9 changes: 2 additions & 7 deletions src/__test__/sfcToEstree.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import sfcToEstree from '../sfcToEstree'
import * as path from 'path'
import * as fs from 'fs'
import * as bt from '@babel/types'

const p = path.resolve(__dirname, '../../__fixtures__/common.vue')
const source = fs.readFileSync(p, 'utf-8')
import { getAST } from './utils'

test('The type of AST should be File', () => {
const ast = sfcToEstree(source)
const ast = getAST('common.vue')
expect(bt.isFile(ast)).toBe(true)
})
11 changes: 11 additions & 0 deletions src/__test__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as path from 'path'
import * as fs from 'fs'
import sfcToEstree from '../sfcToEstree'

export function getAST(fileName: string): object {
const p = path.resolve(__dirname, `../../__fixtures__/${fileName}`)
const source = fs.readFileSync(p, 'utf-8')
return sfcToEstree(source)
}

test('placeholder', () => {})
36 changes: 36 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import generate from '@babel/generator'
import * as bt from '@babel/types'

/**
* If a node satisfies the following conditions, then we will use this node as a Vue component.
* 1. It is a default export
* 2. others...
*/
export function isVueComponent(node: object): boolean {
return bt.isExportDefaultDeclaration(node)
}

export function getValueFromGenerate(node: any) {
let code: string = 'return'
const { code: genCode } = generate(node)
code += genCode
const fn = new Function(code)
try {
return fn()
} catch (e) {
console.error(e)
}
}

export function isPropsOption(path: any): boolean {
if (
bt.isObjectProperty(path.node) &&
path.parentPath &&
path.parentPath.parentPath &&
isVueComponent(path.parentPath.parentPath.node)
) {
const keyPath = path.get('key')
return keyPath.node.name === 'props'
}
return false
}
13 changes: 0 additions & 13 deletions src/helpers/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/helpers/isVueComponent.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import traverse from '@babel/traverse'
import { getValueFromGenerate } from './helpers/index'
import { getValueFromGenerate, isPropsOption } from './helpers'

const mainTraveres = {
ObjectProperty(path: any) {
const keyPath = path.get('key')
if (keyPath.node.name === 'props') {
if (isPropsOption(path)) {
const valuePath = path.get('value')
const propsValue = getValueFromGenerate(valuePath.node)
const { onProp } = (this as any).options
Expand All @@ -19,7 +18,7 @@ export interface ParserOptions {
}
}

export default function(ast: object, options: ParserOptions = {}) {
export default function(ast: any, options: ParserOptions = {}) {
traverse(ast, {
ExportDefaultDeclaration(path: any) {
path.traverse(mainTraveres, {
Expand Down

0 comments on commit 421b07b

Please sign in to comment.