Skip to content

Commit 79a902e

Browse files
author
Mykhailo Miroshnikov
committed
MAGETWO-35118: JavaScript Unit Test Framework supports theme feature
- Added massive configuration capabilities - Specs can now be executed by calling "grunt:%theme_name%" from Magento root - All spec tasks are now located under dev/tests/js/jasmine/spec_runner/tasks
1 parent 49b7413 commit 79a902e

File tree

15 files changed

+273
-144
lines changed

15 files changed

+273
-144
lines changed

Gruntfile.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,11 @@ module.exports = function (grunt) {
8888
],
8989

9090
spec: function (theme) {
91-
var tasks = [],
92-
themes = require(configDir + '/themes');
91+
var runner = require('./dev/tests/js/jasmine/spec_runner');
9392

94-
function tasksFor(theme) {
95-
return [
96-
'connect:' + theme,
97-
'jasmine:' + theme
98-
];
99-
}
100-
101-
if (!theme) {
102-
Object.keys(themes).forEach(function (theme) {
103-
tasks = tasks.concat(tasksFor(theme));
104-
});
105-
} else {
106-
tasks = tasksFor(theme);
107-
}
93+
runner.init(grunt, { theme: theme });
10894

109-
grunt.task.run(tasks);
95+
grunt.task.run(runner.getTasks());
11096
}
11197
}, function (task, name) {
11298
grunt.registerTask(name, task);

dev/tests/js/jasmine/assets/apply/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* See COPYING.txt for license details.
44
*/
55
define([
6-
'tests/tools',
6+
'tests/assets/tools',
77
'tests/assets/apply/components/fn',
88
'text!./config.json',
99
'text!./templates/node.html'

dev/tests/js/jasmine/assets/script/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* See COPYING.txt for license details.
44
*/
55
define([
6-
'tests/tools',
6+
'tests/assets/tools',
77
'text!./config.json',
88
'text!./templates/selector.html',
99
'text!./templates/virtual.html'
File renamed without changes.

dev/tests/js/jasmine/require.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
'use strict';
7+
68
require.config({
79
baseUrl: './',
810
bundles: {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
'use strict';
7+
8+
var tasks = [],
9+
_ = require('underscore');
10+
11+
function init(grunt, options) {
12+
var _ = require('underscore'),
13+
stripJsonComments = require('strip-json-comments'),
14+
path = require('path'),
15+
config,
16+
themes;
17+
18+
config = grunt.file.read(__dirname + '/settings.json');
19+
config = stripJsonComments(config);
20+
config = JSON.parse(config);
21+
22+
themes = require(path.resolve(process.cwd(), config.themes));
23+
24+
if (options.theme) {
25+
themes = _.pick(themes, options.theme);
26+
}
27+
28+
tasks = Object.keys(themes);
29+
30+
config.themes = themes;
31+
32+
enableTasks(grunt, config);
33+
}
34+
35+
function enableTasks(grunt, config) {
36+
var jasmine = require('./tasks/jasmine'),
37+
connect = require('./tasks/connect');
38+
39+
jasmine.init(config);
40+
connect.init(config);
41+
42+
grunt.initConfig({
43+
jasmine: jasmine.getTasks(),
44+
connect: connect.getTasks()
45+
});
46+
}
47+
48+
function getTasks() {
49+
tasks = tasks.map(function (theme) {
50+
return [
51+
'connect:' + theme,
52+
'jasmine:' + theme
53+
]
54+
});
55+
56+
return _.flatten(tasks);
57+
}
58+
59+
module.exports = {
60+
init: init,
61+
getTasks: getTasks
62+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"host": "http://localhost:<%= port %>",
3+
"port": 8000,
4+
"root": "dev/tests/js/jasmine",
5+
6+
/**
7+
* Path to themes configuration module. Relative to Magento root.
8+
* This node is replaced by formatted theme configuration by 'dev/tests/jasmine/spec_runner' module
9+
*/
10+
"themes": "dev/tools/grunt/configs/themes",
11+
12+
"files": {
13+
/**
14+
* Path to RequireJS library. Relative to "server.base" config.
15+
*/
16+
"requireJs": "requirejs/require.js",
17+
18+
/**
19+
* Overriden "grunt-contrib-jasmine" SpecRunner template.
20+
*/
21+
"template": "<%= root %>/spec_runner/template.html",
22+
23+
/**
24+
* These files are included to the page in <head> right after "require.js" in declared sequence.
25+
*/
26+
"requirejsConfigs": [
27+
"pub/static/_requirejs/<%= area %>/<%= name %>/<%= locale %>/requirejs-config.js",
28+
"<%= root %>/require.conf.js",
29+
"<%= root %>/tests/lib/**/*.conf.js",
30+
"<%= root %>/tests/app/code/**/base/**/*.conf.js",
31+
"<%= root %>/tests/app/code/**/<%= area %>/**/*.conf.js",
32+
"<%= root %>/tests/app/design/<%= area %>/<%= name %>/**/*.conf.js"
33+
],
34+
35+
/**
36+
* Files that contain tests. These are loaded to the page via RequireJS after all RequireJS configuration files have been loaded to the page.
37+
* The sequence is ignored.
38+
*/
39+
"specs": [
40+
"<%= root %>/tests/lib/**/*.test.js",
41+
"<%= root %>/tests/app/code/**/base/**/*.test.js",
42+
"<%= root %>/tests/app/code/**/<%= area %>/**/*.test.js",
43+
"<%= root %>/tests/app/design/<%= area %>/<%= name %>/**/*.test.js"
44+
]
45+
},
46+
"server": {
47+
/**
48+
* Directory to serve files from
49+
*/
50+
"base": "pub/static/<%= area %>/<%= name %>/<%= locale %>",
51+
52+
/**
53+
* Strings, mentioned here are interpreted as regular expressions. Use this option to override server's
54+
* default behaviour and serve matched urls "as is" from Magento root.
55+
*/
56+
"serveAsIs": [
57+
"^\/_SpecRunner.html",
58+
"^\/dev\/tests",
59+
"^\/.grunt",
60+
"^\/pub\/static"
61+
],
62+
"options": {
63+
/**
64+
* All options mentioned here are defaults for "connect" grunt task.
65+
* "debug" option enables server logs
66+
* "keepalive" makes "connect" task pause with set up spec server, which you can fetch by %host%:%port%/_SpecRunner.html address in browser
67+
*/
68+
"debug": false,
69+
"keepalive": false
70+
}
71+
}
72+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
'use strict';
7+
8+
var tasks = {};
9+
10+
function init(config) {
11+
var serveStatic = require('serve-static'),
12+
grunt = require('grunt'),
13+
_ = require('underscore'),
14+
path = require('path'),
15+
ignoredPaths, middleware, themes, files, port;
16+
17+
port = config.port;
18+
files = config.files;
19+
themes = config.themes;
20+
ignoredPaths = config.server.serveAsIs;
21+
22+
function serveAsIs(path) {
23+
return ignoredPaths.some(function (ignoredPath) {
24+
return new RegExp(ignoredPath).test(path);
25+
});
26+
}
27+
28+
middleware = function (connect, options, middlewares) {
29+
var server = serveStatic(process.cwd());
30+
31+
middlewares.unshift(function (req, res, next) {
32+
var url = req.url;
33+
34+
if (serveAsIs(url)) {
35+
return server.apply(null, arguments);
36+
}
37+
38+
return next();
39+
});
40+
41+
return middlewares;
42+
}
43+
44+
_.each(themes, function (themeData, themeName) {
45+
var options = {
46+
base: _.template(config.server.base)(themeData),
47+
port: port++,
48+
middleware: middleware
49+
};
50+
51+
_.defaults(options, config.server.options);
52+
53+
tasks[themeName] = { options: options };
54+
});
55+
}
56+
57+
function getTasks() {
58+
return tasks;
59+
}
60+
61+
module.exports = {
62+
init: init,
63+
getTasks: getTasks
64+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
'use strict';
7+
8+
var tasks = {},
9+
_ = require('underscore');
10+
11+
function init(config) {
12+
var grunt = require('grunt'),
13+
expand = grunt.file.expand.bind(grunt.file),
14+
themes, root, host, port, files;
15+
16+
root = config.root;
17+
port = config.port;
18+
files = config.files;
19+
host = _.template(config.host)({ port: port });
20+
themes = config.themes;
21+
22+
_.each(themes, function (themeData, themeName) {
23+
var specs,
24+
configs,
25+
render;
26+
27+
_.extend(themeData, { root: root });
28+
29+
render = renderTemplate.bind(null, themeData);
30+
specs = files.specs.map(render);
31+
specs = expand(specs).map(cutJsExtension);
32+
configs = files.requirejsConfigs.map(render);
33+
34+
tasks[themeName] = {
35+
src: configs,
36+
options: {
37+
host: host,
38+
template: render(files.template),
39+
vendor: files.requireJs,
40+
41+
/**
42+
* @todo rename "helpers" to "specs" (implies overriding grunt-contrib-jasmine code)
43+
*/
44+
helpers: specs
45+
}
46+
}
47+
});
48+
}
49+
50+
function renderTemplate(data, template) {
51+
return _.template(template)(data);
52+
}
53+
54+
function cutJsExtension(path) {
55+
return path.replace(/\.js$/, '');
56+
}
57+
58+
function getTasks() {
59+
return tasks;
60+
}
61+
62+
module.exports = {
63+
init: init,
64+
getTasks: getTasks
65+
};

0 commit comments

Comments
 (0)