Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang committed Sep 29, 2018
1 parent e718c71 commit 2b6f02d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 34 deletions.
8 changes: 6 additions & 2 deletions __fixtures__/objectProps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
a: String,
b: [Number, String],
c: {
type: String,
type: Object,
default: function () {
return {
val: 1
Expand All @@ -19,7 +19,11 @@ export default {
return true
}
},
d: 'null'
d: 'null',
e: {
type: Function,
default: function() {}
}
}
}
</script>
9 changes: 9 additions & 0 deletions src/__test__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The validator function should be used as a string representation 1`] = `
"function () {
return true;
}"
`;

exports[`When the \`type\` definition contains \`Function\`, you should get a string representation of the \`default\` function. 1`] = `"function () {}"`;
81 changes: 77 additions & 4 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import vuese, { ParserOptions, PropsResult } from '../index'
import { getAST } from './utils'

test('Ability to correctly handle props of arrays of type string', () => {
test('Ability to correctly handle props that is an array of string', () => {
const ast = getAST('arrayProps.vue')
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
Expand All @@ -14,10 +14,83 @@ test('Ability to correctly handle props of arrays of type string', () => {
vuese(ast, options)
})

test('Ability to correctly handle props that is object', () => {
const ast = getAST('objectProps.vue')
const ast = getAST('objectProps.vue')
test('Is a prop using a shorthand type', () => {
const options: ParserOptions = {
onProp: () => {}
onProp: (propsRes?: PropsResult[]) => {
expect((propsRes as PropsResult[])[0]).toEqual({
name: 'a',
type: 'String'
})
}
}
vuese(ast, options)
})

test('`prop` defined using a type array', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
expect((propsRes as PropsResult[])[1]).toEqual({
name: 'b',
type: ['Number', 'String']
})
}
}
vuese(ast, options)
})

test('Execute the default function and get the default value correctly', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
const propRes = (propsRes as PropsResult[])[2]
expect(propRes.name).toBe('c')
expect(propRes.default).toEqual({
val: 1
})
}
}
vuese(ast, options)
})

test('Get the `required` value correctly', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
const propRes = (propsRes as PropsResult[])[2]
expect(propRes.required).toBe(true)
}
}
vuese(ast, options)
})

test('The validator function should be used as a string representation', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
const propRes = (propsRes as PropsResult[])[2]
expect(propRes.validator).toMatchSnapshot()
}
}
vuese(ast, options)
})

test('The `prop` that does not satisfy the `prop` writing specification should be treated as no type', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
expect((propsRes as PropsResult[])[3]).toEqual({
name: 'd',
type: null
})
}
}
vuese(ast, options)
})

test('When the `type` definition contains `Function`, you should get a string representation of the `default` function.', () => {
const options: ParserOptions = {
onProp: (propsRes?: PropsResult[]) => {
const propRes = (propsRes as PropsResult[])[4]
expect(propRes.name).toBe('e')
expect(propRes.default).toMatchSnapshot()
}
}
vuese(ast, options)
})
46 changes: 23 additions & 23 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,30 @@ export function normalizeProps(props: string[]): PropsResult[] {
}

const josnCache: [] = []
export function writeFileSync(str: any) {
fs.writeFileSync(
__dirname + '/a.txt',
JSON.stringify(
str,
function(key, value: string | number) {
if (typeof value === 'object' && value !== null) {
if (josnCache.indexOf(value) !== -1) {
// Duplicate reference found
try {
// If this value does not reference a parent it can be deduped
return JSON.parse(JSON.stringify(value))
} catch (error) {
// discard key if value cannot be deduped
return
}
export function writeFileSync(str: any, keep?: boolean) {
const filePath = __dirname + '/a.txt'
const preContent = fs.readFileSync(filePath)
const content = JSON.stringify(
str,
function(key, value: string | number) {
if (typeof value === 'object' && value !== null) {
if (josnCache.indexOf(value) !== -1) {
// Duplicate reference found
try {
// If this value does not reference a parent it can be deduped
return JSON.parse(JSON.stringify(value))
} catch (error) {
// discard key if value cannot be deduped
return
}
// Store value in our collection
josnCache.push(value)
key
}
return value
},
2
)
// Store value in our collection
josnCache.push(value)
key
}
return value
},
2
)
fs.writeFileSync(__dirname + '/a.txt', keep ? preContent + content : content)
}
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ const mainTraveres = {
if (propPath.parentPath === valuePath) {
const name = propPath.node.key.name
const vPath = propPath.get('value')

let type: PropType = null
const result: PropsResult = {
name,
type: null
}

if (isAllowPropsType(vPath.node)) {
result.type = getTypeByPath(vPath)
} else if (bt.isObjectExpression(vPath.node)) {
Expand Down Expand Up @@ -79,10 +76,9 @@ const mainTraveres = {

otherNodes.forEach((node: any) => {
const n = node.key.name

if (n === 'default') {
if (
!hasFunctionTypeDef(type) &&
!hasFunctionTypeDef(result.type) &&
bt.isFunction(node.value)
) {
result.default = runFunction(node.value)
Expand Down

0 comments on commit 2b6f02d

Please sign in to comment.