Skip to content

Commit

Permalink
refactor: update test
Browse files Browse the repository at this point in the history
* test: remove slash

* test: add cases function

* refactor: use ||

* chore(npm): remove slash

* chore(prettier): ignore coverage
  • Loading branch information
akameco committed Dec 16, 2017
1 parent 92c578c commit bda17fa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 84 deletions.
1 change: 1 addition & 0 deletions .prettierignore
@@ -1,2 +1,3 @@
**/flow-typed/**
coverage
lib
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -64,7 +64,6 @@
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^6.0.0",
"prettier": "^1.9.2",
"slash": "^1.0.0"
"prettier": "^1.9.2"
}
}
136 changes: 63 additions & 73 deletions src/__tests__/index.test.js
@@ -1,15 +1,9 @@
// @flow
import path from 'path'
import slash from 'slash'
import pluginTester from 'babel-plugin-tester'
import plugin from '../'

const filename = path.resolve(
slash(__dirname),
'..',
'__fixtures__',
'messages.js'
)
const filename = path.resolve(__dirname, '..', '__fixtures__', 'messages.js')

const defaultTest = {
title: 'default',
Expand Down Expand Up @@ -232,77 +226,73 @@ export default defineMessages({
leadingCommentWithDescriptionTest,
]

type PTestOpts = {
title: string,
tests: $ReadOnlyArray<{ title: string, code: string }>,
}

function pTest(opts: PTestOpts) {
cases([
{ title: 'default', tests },
{
title: 'removePrefix = "src"',
tests: [defaultTest],
pluginOptions: { removePrefix: 'src' },
},
{
title: 'removePrefix = "src/" -- with slash',
tests: [defaultTest],
pluginOptions: { removePrefix: 'src/' },
},
{
title: 'filebase = true',
tests: [defaultTest],
pluginOptions: { filebase: true },
},
{
title: 'includeExportName = true',
tests: [defaultTest, multiExportTest],
pluginOptions: { includeExportName: true },
},
{
title: 'includeExportName = all',
tests: [defaultTest, multiExportTest],
pluginOptions: { includeExportName: 'all' },
},
{
title: 'removePrefix = true, includeExportName = true',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: true, includeExportName: true },
},
{
title: 'removePrefix = false',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: false },
},
{
title: 'removePrefix = true, includeExportName = all',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: true, includeExportName: 'all' },
},
{
title: 'extractComments = false',
tests: [defaultTest, leadingCommentTest, leadingCommentWithDescriptionTest],
pluginOptions: { extractComments: false },
},
])

function cases(
testCases: {
title: string,
tests: $ReadOnlyArray<{ title: string, code: string }>,
}[]
) {
const defaultOpts = {
title: '',
plugin,
snapshot: true,
babelOptions: { filename },
tests: [],
}
opts.tests = opts.tests.map(t => {
return { ...t, title: `${opts.title} - ${t.title}` }
})
pluginTester({ ...defaultOpts, ...opts })
for (const testCase of testCases) {
testCase.tests = testCase.tests.map(t => ({
...t,
title: `${testCase.title} - ${t.title}`,
}))
pluginTester({ ...defaultOpts, ...testCase })
}
}

pTest({ title: 'default', tests })

pTest({
title: 'removePrefix = "src"',
tests: [defaultTest],
pluginOptions: { removePrefix: 'src' },
})

pTest({
title: 'removePrefix = "src/" -- with slash',
tests: [defaultTest],
pluginOptions: { removePrefix: 'src/' },
})

pTest({
title: 'filebase = true',
tests: [defaultTest],
pluginOptions: { filebase: true },
})

pTest({
title: 'includeExportName = true',
tests: [defaultTest, multiExportTest],
pluginOptions: { includeExportName: true },
})

pTest({
title: 'includeExportName = all',
tests: [defaultTest, multiExportTest],
pluginOptions: { includeExportName: 'all' },
})

pTest({
title: 'removePrefix = true, includeExportName = true',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: true, includeExportName: true },
})

pTest({
title: 'removePrefix = false',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: false },
})

pTest({
title: 'removePrefix = true, includeExportName = all',
tests: [defaultTest, multiExportTest],
pluginOptions: { removePrefix: true, includeExportName: 'all' },
})

pTest({
title: 'extractComments = false',
tests: [defaultTest, leadingCommentTest, leadingCommentWithDescriptionTest],
pluginOptions: { extractComments: false },
})
14 changes: 5 additions & 9 deletions src/index.js
Expand Up @@ -72,15 +72,11 @@ const isLiteral = node => t.isStringLiteral(node) || t.isTemplateLiteral(node)

const isValidate = (path: Object, state: State): boolean => {
const callee = path.get('callee')
if (!callee.isIdentifier()) {
return false
}

if (!isImportLocalName(callee.node.name, state)) {
return false
}

if (!path.get('arguments.0')) {
if (
!callee.isIdentifier() ||
!isImportLocalName(callee.node.name, state) ||
!path.get('arguments.0')
) {
return false
}

Expand Down

0 comments on commit bda17fa

Please sign in to comment.