Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and adding more test. #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Envision = require('../envision.js');
const program = require('commander');

program
.version('1.0.8')
.version('1.0.9')
.arguments('<root-file>')
.action((rootFile) => Envision.parse(rootFile))
.parse(process.argv);
7 changes: 5 additions & 2 deletions envision.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const requireCall = (node) => node.init.callee && node.init.callee.name === 'req
const relativePath = (file) => (file.charAt(0) === '.');

// visitors for walk.simple call
const variableDeclaratorVisitors = (rootDirectory, hierarchy, chain) => ({
const declaratorVisitors = (rootDirectory, hierarchy, chain) => ({
VariableDeclarator: (node, state) => {
if (!match(node, state) || !requireCall(node)) return;
const file = node.init.arguments[0].value;
Expand Down Expand Up @@ -70,7 +70,7 @@ const jsxElementVisitors = (ast) => ({
if (!componentChain || componentChain === chain) return;
hierarchy.push(componentChain);
const state = node.openingElement.name.name;
walk.simple(ast, variableDeclaratorVisitors(rootDirectory, hierarchy, componentChain), base, state)
walk.simple(ast, declaratorVisitors(rootDirectory, hierarchy, componentChain), base, state)
}
})

Expand Down Expand Up @@ -121,9 +121,12 @@ const parse = (rootFile) => {
}

module.exports = {
base,
match,
requireCall,
relativePath,
declaratorVisitors,
jsxElementVisitors,
templateHTML,
processHierarchy,
parse
Expand Down
36 changes: 36 additions & 0 deletions test/test.envision.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const { expect } = require('chai');
const acorn = require('acorn-jsx');
const walk = require('acorn/dist/walk');
const Envision = require('../envision.js');

describe('Envision', () => {

describe('base', () => {

it('should be a new custom base object with added properties for traversing JSX elements', () => {
const base = Envision.base;
expect(base).to.be.an('object');
expect(base).to.include.keys('JSXElement', 'JSXExpressionContainer');
expect(base.JSXElement).to.be.a('function');
expect(base.JSXExpressionContainer).to.be.a('function');
})

})

describe('match', () => {

it('should return true if a node\'s identifier\'s name matches name', () => {
Expand Down Expand Up @@ -54,6 +67,29 @@ describe('Envision', () => {

})

describe('declaratorVisitors', () => {

it('should return a visitors object', () => {
const visitors = Envision.declaratorVisitors();
expect(visitors).to.be.an('object');
expect(visitors).to.include.keys('VariableDeclarator', 'ImportDeclaration');
expect(visitors.VariableDeclarator).to.be.a('function');
expect(visitors.ImportDeclaration).to.be.a('function');
})

})

describe('jsxElementVisitors', () => {

it('should return a visitors object', () => {
const visitors = Envision.jsxElementVisitors();
expect(visitors).to.be.an('object');
expect(visitors).to.include.keys('JSXElement');
expect(visitors.JSXElement).to.be.a('function');
})

})

describe('templateHTML', () => {

it('should return the generated HTML file with the tree rendering', () => {
Expand Down