From 2b6f02d591e57383706909f2bab0523d1e30c989 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sun, 30 Sep 2018 00:43:33 +0800 Subject: [PATCH] add tests --- __fixtures__/objectProps.vue | 8 +- src/__test__/__snapshots__/index.test.ts.snap | 9 +++ src/__test__/index.test.ts | 81 ++++++++++++++++++- src/helpers.ts | 46 +++++------ src/index.ts | 6 +- 5 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 src/__test__/__snapshots__/index.test.ts.snap diff --git a/__fixtures__/objectProps.vue b/__fixtures__/objectProps.vue index 544655e..91a108d 100644 --- a/__fixtures__/objectProps.vue +++ b/__fixtures__/objectProps.vue @@ -8,7 +8,7 @@ export default { a: String, b: [Number, String], c: { - type: String, + type: Object, default: function () { return { val: 1 @@ -19,7 +19,11 @@ export default { return true } }, - d: 'null' + d: 'null', + e: { + type: Function, + default: function() {} + } } } diff --git a/src/__test__/__snapshots__/index.test.ts.snap b/src/__test__/__snapshots__/index.test.ts.snap new file mode 100644 index 0000000..33cc847 --- /dev/null +++ b/src/__test__/__snapshots__/index.test.ts.snap @@ -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 () {}"`; diff --git a/src/__test__/index.test.ts b/src/__test__/index.test.ts index 43220df..dcb8910 100644 --- a/src/__test__/index.test.ts +++ b/src/__test__/index.test.ts @@ -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[]) => { @@ -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) }) diff --git a/src/helpers.ts b/src/helpers.ts index 0a13a04..e8fca04 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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) } diff --git a/src/index.ts b/src/index.ts index fc14885..afcb281 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)) { @@ -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)