Skip to content

Commit

Permalink
refactor(s2s-utils): refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
akameco committed Nov 11, 2017
1 parent 1fc6093 commit cbd8fba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 59 deletions.
43 changes: 2 additions & 41 deletions packages/s2s-utils/__snapshots__/index.test.js.snap
Expand Up @@ -4,48 +4,9 @@ exports[`snapshot createImportDeclaration when string 1`] = `"import { local } f

exports[`snapshot createImportDeclaration when string array 1`] = `"import { local1, local2 } from \\"./source\\";"`;

exports[`snapshot defaultImport 1`] = `
Object {
"source": Object {
"type": "StringLiteral",
"value": "source",
},
"specifiers": Array [
Object {
"local": Object {
"name": "local",
"type": "Identifier",
},
"type": "ImportDefaultSpecifier",
},
],
"type": "ImportDeclaration",
}
`;
exports[`snapshot defaultImport 1`] = `"import defaultlocal from \\"source\\";"`;

exports[`snapshot typeImport 1`] = `
Object {
"importKind": "type",
"source": Object {
"type": "StringLiteral",
"value": "source",
},
"specifiers": Array [
Object {
"imported": Object {
"name": "imported",
"type": "Identifier",
},
"local": Object {
"name": "local",
"type": "Identifier",
},
"type": "ImportSpecifier",
},
],
"type": "ImportDeclaration",
}
`;
exports[`snapshot typeImport 1`] = `"import type { imported as local } from \\"source\\";"`;

exports[`template 1`] = `
Node {
Expand Down
46 changes: 28 additions & 18 deletions packages/s2s-utils/index.test.js
@@ -1,5 +1,5 @@
// @flow
import generator from 'babel-generator'
import generate from 'babel-generator'
import * as utils from '.'

test('getImportPath same folder', () => {
Expand Down Expand Up @@ -32,20 +32,30 @@ test('return parent path name', () => {
expect(utils.getParentDirName('ok/hello/world')).toBe('hello')
})

test('snapshot defaultImport', () => {
expect(utils.defaultImport('local', 'source')).toMatchSnapshot()
})

test('snapshot typeImport', () => {
expect(utils.typeImport('local', 'imported', 'source')).toMatchSnapshot()
})

test('snapshot createImportDeclaration when string', () => {
const ast = utils.createImportDeclaration('local', './source')
expect(generator(ast).code).toMatchSnapshot()
})

test('snapshot createImportDeclaration when string array', () => {
const ast = utils.createImportDeclaration(['local1', 'local2'], './source')
expect(generator(ast).code).toMatchSnapshot()
})
testAst([
{
title: 'snapshot createImportDeclaration when string array',
ast: utils.createImportDeclaration(['local1', 'local2'], './source'),
},
{
title: 'snapshot createImportDeclaration when string',
ast: utils.createImportDeclaration('local', './source'),
},
{
title: 'snapshot typeImport',
ast: utils.typeImport('local', 'imported', 'source'),
},
{
title: 'snapshot defaultImport',
ast: utils.defaultImport('defaultlocal', 'source'),
},
])

function testAst(tests: Array<{ title: string, ast: * }>) {
for (const { title, ast } of tests) {
test(title, () => {
const { code } = generate(ast)
expect(code).toMatchSnapshot(title)
})
}
}

0 comments on commit cbd8fba

Please sign in to comment.