From a5ed43bec237ef9330fa8ce332efd6afa73e0601 Mon Sep 17 00:00:00 2001 From: Brian Lai Date: Thu, 24 Sep 2015 09:54:31 -0700 Subject: [PATCH] added tests --- package.json | 2 + src/illustrator.js | 14 +++ test/index.js | 9 +- test/main.test.js | 268 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 239 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index c4a47eb..9206228 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,10 @@ "documentation" ], "dependencies": { + "acorn-jsx-walk": "^1.0.1", "babel-runtime": "^5.8.24", "dox": "^0.8.0", + "escodegen-jsx": "0.1.0-1.4.2dev", "globby": "^3.0.0", "mkdirp": "^0.5.1", "react-docgen": "^2.1.0", diff --git a/src/illustrator.js b/src/illustrator.js index 0d994b3..074fdc1 100644 --- a/src/illustrator.js +++ b/src/illustrator.js @@ -3,6 +3,9 @@ import fs from 'fs'; import path from 'path'; import dox from 'dox'; +import walk from 'acorn-jsx-walk'; +import {simple as walkNode} from 'acorn-jsx-walk/lib/walk'; +import escodegen from 'escodegen-jsx'; import {parse as parseRectDoc} from 'react-docgen'; import {find, toRelativeJsPath} from './util'; @@ -71,6 +74,17 @@ export default class Illustrator { source: this.store.componentSource }, this.store.componentDoc) : null; + walk(this.store.exampleSource, { + MethodDefinition: (node) => { + if (node.key.name === 'render') { + console.log(node.value.body.body) + walkNode(node, { + JSXElement: node => console.log(escodegen.generate(node)) + }) + } + } + }); + var example = { name: this.getCommentTag('name').string, path: path.resolve(this.store.examplePath), diff --git a/test/index.js b/test/index.js index 75711e6..d35d534 100644 --- a/test/index.js +++ b/test/index.js @@ -2,14 +2,9 @@ import sinon from 'sinon' import chai from 'chai' import sinonChai from 'sinon-chai' import sinonAsPromised from 'sinon-as-promised' +import chaiAsPromised from 'chai-as-promised' chai.should() chai.use(sinonChai) +chai.use(chaiAsPromised) -// beforeEach(function () { -// this.sinon = sinon.sandbox.create() -// }) - -// afterEach(function () { -// this.sinon.restore() -// }) diff --git a/test/main.test.js b/test/main.test.js index b65521c..a903cf6 100644 --- a/test/main.test.js +++ b/test/main.test.js @@ -6,56 +6,230 @@ import * as main from '../src' describe('index', function () { describe('#illustrate', function () { - it('should run', function () { - var parseAndValidateOptions; - var globby; - var generateManifest; + let parseAndValidateOptions; + let globby; + let illustrateOne; + let generateManifest; + let aggregate; + let options; + let patterns; + let paths; + let illustrations; + let result; + beforeEach(function () { main.__set__('parseAndValidateOptions', parseAndValidateOptions = this.sinon.stub()); main.__set__('globby', globby = this.sinon.stub()); main.__set__('generateManifest', generateManifest = this.sinon.stub()); + main.__set__('aggregate', aggregate = this.sinon.stub()); + main.__set__('illustrateOne', illustrateOne = this.sinon.stub()); + }); - var options = {}; - var patterns = [path.resolve(__dirname, 'fixtures', '**', 'examples', '*.js')]; - // return main.illustrate(patterns, options) - // .then(function (items) { - // items.should.have.length(3); - // }) - // .catch(function (error) { - // throw error; - // }) - // ; + afterEach(function () { + main.__ResetDependency__('parseAndValidateOptions'); + main.__ResetDependency__('globby'); + main.__ResetDependency__('generateManifest'); + main.__ResetDependency__('aggregate'); + main.__ResetDependency__('illustrateOne'); }) - }) -}) - -// var path = require('path'); - -// var tap = require('tap'); -// var sinon = require('sinon'); -// var rewire = require('rewire'); -// var main = rewire('../src'); - -// // --- - -// tap.test('#illustrate', function (t) { -// var parseAndValidateOptions; -// var globby; -// var generateManifest; - -// main.__set__('parseAndValidateOptions', parseAndValidateOptions = sinon.stub()); -// main.__set__('globby', globby = sinon.mock(main.__get__('globby'))); -// main.__set__('generateManifest', generateManifest = sinon.stub()); - -// var options = {}; -// var patterns = [path.resolve(__dirname, 'fixtures', '**', 'examples', '*.js')]; -// var promise = main.illustrate(patterns, options) -// .then(function (items) { -// assert.equal(items.length, 3, 'should illustrate 3 examples'); -// }) -// .catch(function (error) { -// throw error; -// }) -// .then(t.end) -// ; -// }); + + describe('with default options', function () { + beforeEach(function () { + options = {}; + patterns = ['foo']; + paths = ['fux', 'fuux', 'fuuux']; + illustrations = paths.map((p, i) => { + return { + name: `${p}-name-${i}`, + path: `${p}-path-${i}`, + source: `${p}-source-${i}` + }; + }); + result = illustrations.map(() => ({})); + + globby.resolves(paths); + parseAndValidateOptions.returns(options); + illustrations.forEach((i, index) => illustrateOne.onCall(index).returns(i)); + aggregate.resolves(result); + + return this.result = main.illustrate(patterns, options); + }); + + it('should have parsed and validated options', function () { + return parseAndValidateOptions.should.have.been.calledWith(options); + }); + + it('should have example glob patterns', function () { + return globby.should.have.been.calledWith(patterns); + }); + + it('should not generate manifest', function () { + return generateManifest.should.notCalled; + }); + + it('should have called illustrateOne for each path covered by glob pattern', function () { + return paths.forEach((p, i) => illustrateOne.getCall(i).should.have.been.calledWith(p, options)); + }); + + it('should aggregate illustrations', function () { + return aggregate.should.have.been.calledWith(illustrations); + }); + + it('should resolves with aggregated results', function () { + return this.result.should.become(result) + }); + }); + + describe('with outputFormat set to `manifest`', function () { + beforeEach(function () { + options = {outputFormat: 'manifest'}; + patterns = ['foo']; + paths = ['fux', 'fuux', 'fuuux']; + illustrations = paths.map((p, i) => { + return { + name: `${p}-name-${i}`, + path: `${p}-path-${i}`, + source: `${p}-source-${i}` + }; + }); + result = illustrations.map(() => ({})); + + globby.resolves(paths); + parseAndValidateOptions.returns(options); + illustrations.forEach((i, index) => illustrateOne.onCall(index).returns(i)); + generateManifest.resolves(result); + aggregate.resolves(result); + + return this.result = main.illustrate(patterns, options); + }); + + it('should have parsed and validated options', function () { + return parseAndValidateOptions.should.have.been.calledWith(options); + }); + + it('should have example glob patterns', function () { + return globby.should.have.been.calledWith(patterns); + }); + + it('should have generated manifest', function () { + return generateManifest.should.have.been.calledWith(options, result); + }); + + it('should have called illustrateOne for each path covered by glob pattern', function () { + return paths.forEach((p, i) => illustrateOne.getCall(i).should.have.been.calledWith(p, options)); + }); + + it('should aggregate illustrations', function () { + return aggregate.should.have.been.calledWith(illustrations); + }); + + it('should resolves with aggregated results', function () { + return this.result.should.become(result) + }); + }); + }); + + describe('#illustrateOne', function () { + let Illustrator; + let illustrator; + let processExample; + let component; + let processComponent; + let run; + let file; + let options; + let result; + + beforeEach(function () { + main.__set__('Illustrator', Illustrator = this.sinon.stub()); + processExample = this.sinon.stub(); + processComponent = this.sinon.stub(); + run = this.sinon.stub(); + + Illustrator.returns(illustrator = { + processExample, + processComponent, + run + }); + }); + + afterEach(function () { + main.__ResetDependency__('Illustrator'); + }); + + describe('when `component` is specified in example', function () { + beforeEach(function () { + file = 'foo'; + options = {}; + illustrator.component = 'bar'; + + processExample.resolves(); + processComponent.resolves(); + run.resolves(result = {}); + + return this.result = main.illustrateOne(file, options); + }); + + it('should create an Illustrator instance', function () { + return Illustrator.should.have.been.calledWith(options); + }); + + it('should have called illustrator.processExample', function () { + return processExample.should.have.been.calledWith(file); + }); + + it('should have called illustrator.processComponent', function () { + return processComponent.should.have.been.calledWith(illustrator.component); + }); + + it('should have called illustrator.run', function () { + return run.should.have.been.calledOnce; + }); + + it('should resolves', function () { + return this.result.should.become(result); + }); + }); + + describe('when `component` is not specified in example', function () { + beforeEach(function () { + file = 'foo'; + options = {}; + + processExample.resolves(); + run.resolves(result = {}); + + return this.result = main.illustrateOne(file, options); + }); + + it('should create an Illustrator instance', function () { + return Illustrator.should.have.been.calledWith(options); + }); + + it('should have called illustrator.processExample', function () { + return processExample.should.have.been.calledWith(file); + }); + + it('should not call illustrator.processComponent', function () { + return processComponent.should.have.not.been.called; + }); + + it('should have called illustrator.run', function () { + return run.should.have.been.calledOnce; + }); + + it('should resolves', function () { + return this.result.should.become(result); + }); + }); + }); + + describe('#parseAndValidateOptions', function () { + var pathResolve; + var options; + + beforeEach(function () { + main.__set__('path', {resolve: pathResolve = this.sinon.stub()}); + }); + }); +});