Added changes for version 2.0

This commit is contained in:
Chris
2015-09-21 20:45:57 +02:00
parent 438341c3fe
commit 023d937877
69 changed files with 1149 additions and 18551 deletions
+97
View File
@@ -0,0 +1,97 @@
'use strict';
let path = require('path');
let expect = require('chai').expect;
let assert = require('yeoman-generator').assert;
let helpers = require('yeoman-generator').test
describe('react-webpack:app', () => {
let defaultPrompts = require('../../../generators/app/prompts.js');
let prompts = {};
for(let p of defaultPrompts) {
prompts[p.name] = p.default;
}
let generator;
let generatorBase = path.join(__dirname, '../../../generators/app');
before((done) => {
helpers.run(generatorBase)
.inTmpDir()
.withOptions({
'skip-welcome-message': true,
'skip-install': true
})
.withPrompts(prompts)
.on('ready', (instance) => {
generator = instance;
})
.on('end', done);
});
describe('#config', () => {
it('should use "css" as default style language', () => {
expect(generator.config.get('style')).to.equal('css');
});
});
describe('#createFiles', () => {
it('should generate dot files', () => {
assert.file([
'.editorconfig',
'.eslintrc',
'.npmignore',
'.yo-rc.json'
]);
});
it('should generate project configuration files', () => {
assert.file([
'LICENSE',
'package.json'
]);
});
it('should generate the webpack configuration', () => {
assert.file([
'cfg/base.js',
'cfg/dev.js',
'cfg/dist.js',
'cfg/test.js',
'server.js',
'webpack.config.js'
]);
});
it('should generate required source files', () => {
assert.file([
'src/actions/README.md',
'src/components/Main.js',
'src/components/run.js',
'src/favicon.ico',
'src/images/yeoman.png',
'src/index.html',
'src/sources/README.md',
'src/stores/README.md',
'src/styles/App.css'
]);
});
it('should generate test configuration and basic tests', () => {
assert.file([
'karma.conf.js',
'test/components/MainTest.js',
'test/helpers/shallowRenderHelper.js',
'test/loadtests.js'
]);
});
});
});
+283
View File
@@ -0,0 +1,283 @@
'use strict';
let path = require('path');
let assert = require('yeoman-generator').assert;
let helpers = require('yeoman-generator').test
describe('react-webpack:component', () => {
let generatorComponent = path.join(__dirname, '../../../generators/component');
/**
* Return a newly generated component with given name and style
* @param {String} name
* @param {String} styleType
* @param {Function} callback
*/
function createGeneratedComponent(name, styleType, callback) {
helpers.run(generatorComponent)
.withArguments([name])
.on('ready', (instance) => {
instance.config.set('style', styleType);
})
.on('end', callback);
}
describe('When using style type "css"', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('mycomponent', 'css', () => {
assert.file([
'src/components/MycomponentComponent.js',
'src/styles/Mycomponent.css',
'test/components/MycomponentComponentTest.js'
]);
done();
});
});
});
describe('Component', () => {
it('should require the created css file', (done) => {
createGeneratedComponent('mycomponent', 'css', () => {
assert.fileContent('src/components/MycomponentComponent.js', 'require(\'styles//Mycomponent.css\');');
done();
});
});
});
describe('Style', () => {
it('should add the components css class to the stylesheet', (done) => {
createGeneratedComponent('mycomponent', 'css', () => {
assert.fileContent('src/styles/Mycomponent.css', '.mycomponent-component');
done();
});
});
});
describe('Test', () => {
it('should import the react component', (done) => {
createGeneratedComponent('mycomponent', 'css', () => {
assert.fileContent('test/components/MycomponentComponentTest.js', 'import MycomponentComponent from \'components//MycomponentComponent.js\';');
done();
});
});
});
}); // End css
describe('When creating a component in a subfolder', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('my/little !special/test', 'css', () => {
assert.file([
'src/components/my/littleSpecial/TestComponent.js',
'src/styles/my/littleSpecial/Test.css',
'test/components/my/littleSpecial/TestComponentTest.js'
]);
done();
});
});
});
}); // End css (subfolder)
describe('When using style type "sass"', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('mycomponent', 'sass', () => {
assert.file([
'src/components/MycomponentComponent.js',
'src/styles/Mycomponent.sass',
'test/components/MycomponentComponentTest.js'
]);
done();
});
});
});
describe('Component', () => {
it('should require the created sass file', (done) => {
createGeneratedComponent('mycomponent', 'sass', () => {
assert.fileContent('src/components/MycomponentComponent.js', 'require(\'styles//Mycomponent.sass\');');
done();
});
});
});
describe('Style', () => {
it('should add the components sass class to the stylesheet', (done) => {
createGeneratedComponent('mycomponent', 'sass', () => {
assert.fileContent('src/styles/Mycomponent.sass', '.mycomponent-component');
done();
});
});
});
describe('Test', () => {
it('should import the react component', (done) => {
createGeneratedComponent('mycomponent', 'sass', () => {
assert.fileContent('test/components/MycomponentComponentTest.js', 'import MycomponentComponent from \'components//MycomponentComponent.js\';');
done();
});
});
});
}); // End sass
describe('When using style type "scss"', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('mycomponent', 'scss', () => {
assert.file([
'src/components/MycomponentComponent.js',
'src/styles/Mycomponent.scss',
'test/components/MycomponentComponentTest.js'
]);
done();
});
});
});
describe('Component', () => {
it('should require the created scss file', (done) => {
createGeneratedComponent('mycomponent', 'scss', () => {
assert.fileContent('src/components/MycomponentComponent.js', 'require(\'styles//Mycomponent.scss\');');
done();
});
});
});
describe('Style', () => {
it('should add the components scss class to the stylesheet', (done) => {
createGeneratedComponent('mycomponent', 'scss', () => {
assert.fileContent('src/styles/Mycomponent.scss', '.mycomponent-component');
done();
});
});
});
describe('Test', () => {
it('should import the react component', (done) => {
createGeneratedComponent('mycomponent', 'scss', () => {
assert.fileContent('test/components/MycomponentComponentTest.js', 'import MycomponentComponent from \'components//MycomponentComponent.js\';');
done();
});
});
});
}); // End scss
describe('When using style type "less"', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('mycomponent', 'less', () => {
assert.file([
'src/components/MycomponentComponent.js',
'src/styles/Mycomponent.less',
'test/components/MycomponentComponentTest.js'
]);
done();
});
});
});
describe('Component', () => {
it('should require the created less file', (done) => {
createGeneratedComponent('mycomponent', 'less', () => {
assert.fileContent('src/components/MycomponentComponent.js', 'require(\'styles//Mycomponent.less\');');
done();
});
});
});
describe('Style', () => {
it('should add the components less class to the stylesheet', (done) => {
createGeneratedComponent('mycomponent', 'less', () => {
assert.fileContent('src/styles/Mycomponent.less', '.mycomponent-component');
done();
});
});
});
describe('Test', () => {
it('should import the react component', (done) => {
createGeneratedComponent('mycomponent', 'less', () => {
assert.fileContent('test/components/MycomponentComponentTest.js', 'import MycomponentComponent from \'components//MycomponentComponent.js\';');
done();
});
});
});
}); // End less
describe('When using style type "stylus"', () => {
describe('Setup', () => {
it('should create the react component, its stylesheet and test file', (done) => {
createGeneratedComponent('mycomponent', 'stylus', () => {
assert.file([
'src/components/MycomponentComponent.js',
'src/styles/Mycomponent.styl',
'test/components/MycomponentComponentTest.js'
]);
done();
});
});
});
describe('Component', () => {
it('should require the created stylus file', (done) => {
createGeneratedComponent('mycomponent', 'stylus', () => {
assert.fileContent('src/components/MycomponentComponent.js', 'require(\'styles//Mycomponent.styl\');');
done();
});
});
});
describe('Style', () => {
it('should add the components stylus class to the stylesheet', (done) => {
createGeneratedComponent('mycomponent', 'stylus', () => {
assert.fileContent('src/styles/Mycomponent.styl', '.mycomponent-component');
done();
});
});
});
describe('Test', () => {
it('should import the react component', (done) => {
createGeneratedComponent('mycomponent', 'less', () => {
assert.fileContent('test/components/MycomponentComponentTest.js', 'import MycomponentComponent from \'components//MycomponentComponent.js\';');
done();
});
});
});
}); // End stylus
});
+2
View File
@@ -0,0 +1,2 @@
--reporter spec
--recursive
-342
View File
@@ -1,342 +0,0 @@
/*global describe, beforeEach, it*/
'use strict';
var path = require('path');
var helpers = require('yeoman-generator').test;
var assert = require('yeoman-generator').assert;
var _ = require('underscore.string');
describe('react-webpack generator', function() {
var react;
var expected = [
'src/favicon.ico',
'src/styles/main.css',
'src/index.html',
'Gruntfile.js',
'webpack.config.js',
'karma.conf.js',
'package.json'
];
var mockPrompts = {};
var genOptions = {
'appPath': 'src',
'skip-install': true,
'skip-welcome-message': true,
'skip-message': true
};
var deps = [
'../../app',
'../../common',
'../../component',
'../../main'
];
beforeEach(function(done) {
helpers.testDirectory(path.join(__dirname, 'temp-test'), function(err) {
if (err) {
return done(err);
}
react = helpers.createGenerator('react-webpack:app', deps, false, genOptions);
helpers.mockPrompt(react, mockPrompts);
done();
});
});
describe('App files', function() {
it('should generate dotfiles', function(done) {
react.run({}, function() {
helpers.assertFile([].concat(expected, [
'.yo-rc.json',
'.editorconfig',
'.gitignore',
'.jshintrc'
]));
done();
});
});
it('should generate app files', function(done) {
react.run({}, function() {
// TODO: Hack, no time to work out why generated
// files not present at point of test...
setTimeout(function() {
helpers.assertFile(expected);
done();
});
});
});
it('should generate expected JS files', function(done) {
react.run({}, function() {
setTimeout(function() {
helpers.assertFile([].concat(expected, [
'src/components/TempTestApp.js',
'src/components/main.js'
]));
done();
});
});
});
it('should generate expected test JS files', function(done) {
react.run({}, function() {
// TODO: Hack, no time to work out why generated
// files not present at point of test...
setTimeout(function() {
helpers.assertFile([].concat(expected, [
'test/helpers/pack/phantomjs-shims.js',
'test/helpers/createComponent.js',
'test/helpers/react/addons.js',
'test/spec/components/TempTestApp.js'
]));
done();
});
});
});
it('should use HMR webpack API inside of configs', function (done) {
react.run({}, function() {
assert.fileContent([
['package.json', /react-hot-loader/],
['Gruntfile.js', /hot:\s*true/],
['webpack.config.js', /react-hot/],
['webpack.config.js', /webpack\.HotModuleReplacementPlugin/],
['webpack.config.js', /webpack\/hot\/only-dev-server/]
]);
done();
});
});
it('should generate JS config with aliases', function(done) {
react.run({}, function() {
assert.fileContent([
// style aliases
['webpack.config.js', /resolve[\S\s]+alias[\S\s]+styles/m],
['karma.conf.js', /resolve[\S\s]+alias[\S\s]+styles/m],
['webpack.dist.config.js', /resolve[\S\s]+alias[\S\s]+styles/m],
// script/components aliases
['webpack.config.js', /resolve[\S\s]+alias[\S\s]+components/m],
['karma.conf.js', /resolve[\S\s]+alias[\S\s]+components/m],
['webpack.dist.config.js', /resolve[\S\s]+alias[\S\s]+components/m]
]);
done();
});
});
it('should not have any flux assets configured', function(done) {
react.run({}, function() {
assert.noFileContent([
['package.json', /flux/],
['package.json', /events/],
['package.json', /object-assign/],
['karma.conf.js', /resolve[\S\s]+alias[\S\s]+stores/m],
['webpack.config.js', /resolve[\S\s]+alias[\S\s]+stores/m],
['webpack.dist.config.js', /resolve[\S\s]+alias[\S\s]+stores/m]
]);
assert.noFile('src/dispatcher/TempTestAppDispatcher.js');
done();
});
});
});
describe('Generator', function () {
it('should contain info about used style lang', function (done) {
react.run({}, function() {
assert.ok(react.config.get('styles-language'));
done();
});
});
it('by default should use css style lang', function (done) {
react.run({}, function() {
assert.equal(react.config.get('styles-language'), 'css');
done();
});
});
var assertStyle = function (lang, done) {
helpers.mockPrompt(react, {
stylesLanguage: lang
});
react.run({}, function() {
assert.equal(react.config.get('styles-language'), lang);
done();
});
};
it('should use sass style lang', function (done) {
assertStyle('sass', done);
});
it('should use scss style lang', function (done) {
assertStyle('scss', done);
});
it('should use less style lang', function (done) {
assertStyle('less', done);
});
it('should use stylus style lang', function (done) {
assertStyle('stylus', done);
});
});
describe('When using Flux', function() {
beforeEach(function(done) {
helpers.mockPrompt(react, {
architecture: 'flux'
});
react.run({}, function() {
done();
})
});
it('should add flux, events, and object-assign packages', function(done) {
assert.fileContent([
['package.json', /flux/],
['package.json', /events/],
['package.json', /object-assign/]
]);
done();
});
it('should add stores and actions alias to karma config', function(done) {
assert.fileContent([
['karma.conf.js', /resolve[\S\s]+alias[\S\s]+stores/m]
]);
done();
});
it('should add stores and actions alias to webpack configs', function(done) {
assert.fileContent([
['webpack.config.js', /resolve[\S\s]+alias[\S\s]+stores/m],
['webpack.dist.config.js', /resolve[\S\s]+alias[\S\s]+stores/m]
]);
done();
});
it('should have a Dispatcher generated', function(done) {
setTimeout(function(){
assert.file('src/dispatcher/TempTestAppDispatcher.js');
done();
});
})
});
describe('When generating a Component', function() {
var generatorTest = function(name, generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) {
var deps = [path.join('../..', generatorType)];
genOptions.appPath = 'src';
var reactGenerator = helpers.createGenerator('react-webpack:' + generatorType, deps, [name], genOptions);
react.run([], function() {
reactGenerator.run([], function() {
helpers.assertFileContent([
[path.join('src', targetDirectory, name + '.js'), new RegExp('var ' + scriptNameFn(name) + suffix, 'g')],
[path.join('src', targetDirectory, name + '.js'), new RegExp('require\\(\'styles\\/' + name + suffix + '\\.[^\']+' + '\'\\)', 'g')],
[path.join('test/spec', targetDirectory, 'TempTestApp' + '.js'), new RegExp('require\\(\'components\\/' + 'TempTestApp' + suffix + '\\.[^\']+' + '\'\\)', 'g')],
[path.join('test/spec', targetDirectory, name + '.js'), new RegExp('import ' + scriptNameFn(name) + ' from \'components\/Foo', 'g')],
[path.join('test/spec', targetDirectory, name + '.js'), new RegExp('describe\\(\'' + specNameFn(name) + suffix + '\'', 'g')]
]);
done();
});
});
}
it('should generate a new component', function(done) {
react.run({}, function() {
generatorTest('Foo', 'component', 'component', 'components', _.capitalize, _.capitalize, '', done);
});
});
it('should generate a subcomponent', function(done) {
react.run({}, function() {
var subComponentNameFn = function () { return 'Bar'; };
generatorTest('Foo/Bar', 'component', 'component', 'components', subComponentNameFn, subComponentNameFn, '', done);
});
});
});
describe('When generating an Action', function() {
beforeEach(function(done){
helpers.mockPrompt(react, {
architecture: 'flux'
});
react.run({}, function() {
var generator =
helpers.createGenerator(
'react-webpack:action',
[path.join('../../action')],
['Test'],
{ appPath: 'src' }
);
react.run([], function() {
generator.run([], function() {
done();
})
});
});
});
it('should generate a new action with tests', function(done) {
assert.fileContent([
['src/actions/TestActionCreators.js', /var TestActionCreators/g],
['test/spec/actions/TestActionCreators.js', /require\('actions\/TestActionCreators.js'\)/g],
['test/spec/actions/TestActionCreators.js', /describe\('TestActionCreators'/g]
]);
done();
});
});
describe('When generating a Store', function() {
beforeEach(function(done) {
helpers.mockPrompt(react, {
architecture: 'flux'
});
react.run({}, function() {
var generator =
helpers.createGenerator(
'react-webpack:store',
[path.join('../../store')],
['Test'],
{ appPath: 'src' }
);
react.run([], function() {
generator.run([], function() {
done();
});
});
});
});
it('should generate a new store with tests', function(done) {
assert.fileContent([
['src/stores/TestStore.js', /var TestStore/g],
['test/spec/stores/TestStore.js', /require\('stores\/TestStore.js'\)/g],
['test/spec/stores/TestStore.js', /describe\('TestStore'/g]
]);
done();
});
});
});
+65
View File
@@ -0,0 +1,65 @@
'use strict';
let expect = require('chai').expect;
let utils = require('../../utils/config');
let originalSettings = require('../../utils/configopts.json');
describe('Utilities:Config', () => {
describe('#getSetting', () => {
it('should return "null" if the key could not be found', () => {
expect(utils.getSetting('bogus')).to.be.null;
});
it('should return a settings object if it exists', () => {
let result = utils.getSetting('style');
expect(result).to.be.an.object;
expect(result).to.deep.equal(originalSettings.style);
});
});
describe('#getChoices', () => {
it('should return "null" if the key could not be found', () => {
expect(utils.getChoices('bogus')).to.be.null;
});
it('should return an array of choices when queried correctly', () => {
let result = utils.getChoices('style');
expect(result).to.be.an.array;
expect(result).to.deep.equal(originalSettings.style.options);
});
});
describe('#getChoiceByKey', () => {
it('should return "null" if the key or the setting could not be found', () => {
expect(utils.getChoiceByKey('bogus', 'unknown')).to.be.null;
expect(utils.getChoiceByKey('style', 'unknown')).to.be.null;
});
it('should return the configured object when it can be found', () => {
expect(utils.getChoiceByKey('style', 'css')).to.equal(originalSettings.style.options[0]);
expect(utils.getChoiceByKey('style', 'less')).to.equal(originalSettings.style.options[3]);
});
});
describe('#getDefaultChoice', () => {
it('should return "null" if the key could not be found', () => {
expect(utils.getDefaultChoice('bogus')).to.be.null;
});
it('should return the default choice when queried correctly', () => {
let result = utils.getDefaultChoice('style');
expect(result).to.equal(originalSettings.style.default);
});
});
});
+97
View File
@@ -0,0 +1,97 @@
'use strict';
let expect = require('chai').expect;
let path = require('path');
let utils = require('../../utils/yeoman');
let baseDir = path.basename(process.cwd());
describe('Utilities:Yeoman', () => {
describe('#getBaseDir', () => {
it('should return the current run directory', () => {
expect(utils.getBaseDir()).to.equal(baseDir);
});
});
describe('#getCleanedPathName', () => {
it('should return normalized paths', () => {
let tests = {
'my/full œ!/path!': 'my/full/path',
'Test': 'test',
'I am a Test Component!': 'iAmATestComponent',
'A very\^specialChary!@componentName with !!!': 'aVerySpecialCharyComponentNameWith'
};
for(let test in tests) {
expect(utils.getCleanedPathName(test)).to.be.equal(tests[test]);
expect(utils.getCleanedPathName(test, 'suffix')).to.be.equal(tests[test] + 'Suffix');
}
});
});
describe('#getAppName', () => {
it('should return a js friendly application name', () => {
let result = utils.getAppName('this is a test using % special / chars!');
expect(result).to.be.equal('thisIsATestUsingSpecialChars');
});
it('should use the current path for creating the appName if the argument is omitted', () => {
let resultWithoutArgs = utils.getAppName();
let resultWithArgs = utils.getAppName(baseDir);
expect(resultWithoutArgs).to.be.equal(resultWithArgs);
});
});
describe('#getComponentStyleName', () => {
it('should return a components css className', () => {
let tests = {
'my/full œ!/path!': 'path-component',
'Test': 'test-component',
'I am a Test Component!': 'i-am-a-test-component-component',
'A very\^specialChary!@componentName with !!!': 'a-very-specialchary-componentname-with-component'
};
for(let test in tests) {
expect(utils.getComponentStyleName(test)).to.be.equal(tests[test]);
}
});
});
describe('#getAllSettingsFromComponentName', () => {
it('should get all required information for component creation from the components name', () => {
let expection = {
style: {
webpackPath: 'styles/my/component/Test.css',
path: 'src/styles/my/component/',
fileName: 'Test.css',
className: 'test-component',
suffix: '.css'
},
component: {
webpackPath: 'components/my/component/TestComponent.js',
path: 'src/components/my/component/',
fileName: 'TestComponent.js',
className: 'TestComponent',
suffix: '.js'
},
test: {
path: 'test/components/my/component/',
fileName: 'TestComponentTest.js'
}
};
expect(utils.getAllSettingsFromComponentName('my/component/test')).to.deep.equal(expection);
});
});
});