Skip to content
Snippets Groups Projects
Commit ebdd78ed authored by Mykhailo Miroshnikov's avatar Mykhailo Miroshnikov
Browse files

MAGETWO-31592: Javascript Unit Test Framework Update

 - Refactoring
parent 4c872d3d
No related merge requests found
'use strict'; /**
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
*/
module.exports = function (grunt) { module.exports = function (grunt) {
'use strict';
require('./spec_runner')(grunt); require('./spec_runner')(grunt);
...@@ -145,38 +148,33 @@ module.exports = function (grunt) { ...@@ -145,38 +148,33 @@ module.exports = function (grunt) {
template: require('grunt-template-jasmine-requirejs'), template: require('grunt-template-jasmine-requirejs'),
ignoreEmpty: true ignoreEmpty: true
}, },
'backend-unit-testsuite': { 'backend-unit-testsuite': specConfigFor('unit', 'adminhtml', 8000),
options: specConfigFor('unit', 'adminhtml', 8000) 'backend-integration-testsuite': specConfigFor('integration', 'adminhtml', 8000),
}, 'frontend-unit-testsuite': specConfigFor('unit', 'frontend', 3000),
'backend-integration-testsuite': { 'frontend-integration-testsuite': specConfigFor('integration', 'frontend', 3000)
options: specConfigFor('integration', 'adminhtml', 8000)
},
'frontend-unit-testsuite': {
options: specConfigFor('unit', 'frontend', 3000)
},
'frontend-integration-testsuite': {
options: specConfigFor('integration', 'frontend', 3000)
}
} }
}); });
function specConfigFor(type, dir, port) { function specConfigFor(type, dir, port) {
return { return {
host: 'http://localhost:' + port, src: '<%= config.path.spec %>/env.js',
specs: specPathFor(type, dir, 'Spec'), options: {
helpers: specPathFor(type, dir, 'Helper'), host: 'http://localhost:' + port,
templateOptions: { specs: specPathFor(type, dir, 'Spec'),
requireConfigFile: [ helpers: specPathFor(type, dir, 'Helper'),
'lib/web/app-config.js', templateOptions: {
'<%= config.path.spec %>/' + type + '/config.js' requireConfigFile: [
] 'lib/web/app-config.js',
'<%= config.path.spec %>/' + type + '/config.js/'
]
}
} }
} }
}; }
function specPathFor(type, dir, suffix) { function specPathFor(type, dir, suffix) {
return '<%= config.path.spec %>/' + type + '/**/' + dir + '/**/*' + suffix + '.js' return '<%= config.path.spec %>/' + type + '/**/' + dir + '/**/*' + suffix + '.js';
}; }
// Clean var & pub folders // Clean var & pub folders
grunt.registerTask('cleanup', [ grunt.registerTask('cleanup', [
......
/** /**
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com) * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
*/ */
/* global jasmine: true, expect: true, it: true, describe: true */
define([ define([
'Magento_Ui/js/lib/events' 'Magento_Ui/js/lib/events'
], function (EventBus) { ], function (EventBus) {
'use strict'; 'use strict';
describe('EventBus', function () { describe('EventBus', function () {
it('has <on> method', function () { describe('<on> method', function () {
expect('on' in EventBus).toBe(true); it('calls passed callback when event is triggered', function () {
var callback = jasmine.createSpy('callback');
EventBus.on('someEvent', callback);
EventBus.trigger('someEvent');
expect(callback).toHaveBeenCalled();
});
}); });
}); });
}); });
\ No newline at end of file
'use strict'; /**
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
*/
/* global __dirname: true */
module.exports = function (grunt) {
'use strict';
module.exports = function(grunt) {
var connect = require('connect'), var connect = require('connect'),
logger = require('morgan'), logger = require('morgan'),
serveStatic = require('serve-static'), serveStatic = require('serve-static'),
fs = require('fs'); fs = require('fs');
/**
* Defines if passed file path exists
*
* @param {String} path
* @return {Boolean}
*/
function exists(path) {
return fs.existsSync(path);
}
/**
* Restricts url's which lead to '/_SpecRunner.html', '/dev/tests' or '.grunt' folders from being modified
*
* @param {String} url
* @return {Boolean}
*/
function canModify(url) { function canModify(url) {
return url.match(/^\/(\.grunt)|(dev\/tests)|(_SpecRunner\.html)/) === null; return url.match(/^\/(\.grunt)|(dev\/tests)|(_SpecRunner\.html)/) === null;
}; }
grunt.registerMultiTask('specRunner', function(grunt) { grunt.registerMultiTask('specRunner', function () {
var app = connect(), var app = connect(),
options, options,
area, area,
theme,
share, share,
moduleRoot, middlewares;
libPath;
options = this.options({ options = this.options({
port: 3000, port: 3000,
theme: 'blank',
areaDir: 'adminhtml', areaDir: 'adminhtml',
shareDir: 'base', shareDir: 'base',
enableLogs: false enableLogs: false,
middleware: null
}); });
area = options.areaDir; area = options.areaDir;
share = options.shareDir; share = options.shareDir;
moduleRoot = '/app/code/Magento/'; theme = options.theme;
libPath = '/lib/web';
if (options.enableLogs) { if (options.enableLogs) {
app.use(logger('dev')); app.use(logger('dev'));
} }
function assembleUrl(module, area, path) { app.use(function (req, res, next) {
return moduleRoot + module + '/view/' + area + '/web/' + path; var url = req.url,
} match = url.match(/^\/([A-Z][^\/]+)_(\w+)\/(.+)$/),
app,
app.use(function(req, res, next) {
var url = req.url,
match = url.match(/^\/Magento_([^\/]+)(\/.+)$/),
module, module,
path, path,
exist; getModuleUrl,
getThemeUrl;
/**
* Returns path to theme root folder
*
* @return {String}
*/
function themeRoot() {
return [
'/app/design',
area,
app,
theme
].join('/');
}
/**
* Based on 'thematic' parameter, returnes either path to theme's lib,
* or 'lib/web'.
*
* @param {Boolean} thematic
* @return {String}
*/
function lib(thematic) {
return thematic ? themeRoot() + '/web' : '/lib/web';
}
if (match !== null) { if (match !== null) {
module = match[1]; app = match[1];
path = match[2]; module = match[2];
url = assembleUrl(module, area, path), path = match[3];
exist = fs.existsSync(url);
/**
* Assembles modular path. If 'shared' flag provided and is truthy,
* will use share dir instead of area one.
*
* @param {Boolean} shared
* @return {String}
*/
getModuleUrl = function (shared) {
return [
'/app/code',
app,
module,
'view',
shared ? share : area,
'web',
path
].join('/');
};
if (!exist) { /**
url = assembleUrl(module, share, path); * Assembles theme modular path.
} *
* @return {String}
*/
getThemeUrl = function () {
return [
themeRoot(),
app + '_' + module,
'web',
path
].join('/');
};
url = exists(url = getThemeUrl()) ? url : getModuleUrl(true);
} else if (canModify(url)) { } else if (canModify(url)) {
url = libPath + url; url = (exists(url = lib(true)) ? url : lib()) + req.url;
} }
req.url = url; req.url = url;
...@@ -64,8 +140,18 @@ module.exports = function(grunt) { ...@@ -64,8 +140,18 @@ module.exports = function(grunt) {
next(); next();
}); });
if (options.middleware && typeof options.middleware === 'function') {
middlewares = options.middleware(connect, options);
if (Array.isArray(middlewares)) {
middlewares.forEach(function (middleware) {
app.use(middleware);
});
}
}
app.use(serveStatic(__dirname)); app.use(serveStatic(__dirname));
app.listen(options.port); app.listen(options.port);
}); });
} };
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment