From 1e9c25dff3e326860be9fcf4b11dfdbbbe18dc8b Mon Sep 17 00:00:00 2001
From: Mykhailo Miroshnikov <mmiroshnikov@ebay.com>
Date: Fri, 30 Jan 2015 13:51:47 +0200
Subject: [PATCH] MAGETWO-31592: Javascript Unit Test Framework Update

 - Change jasmine task configuration
 - Add shared requirejs config file
 - Add adminhtml and frontend area's requirejs config files in both
   integration and unit testsuites
 - Add shim.js to compensate lack of Function.prototype.bind in PhantomJS
   environment
 - Various small code improvements
---
 Gruntfile.js                                  | 11 ++--
 dev/tests/js/framework/spec_runner.js         | 52 +++++++++----------
 .../PageCache/frontend/js/pageCacheSpec.js    | 19 -------
 .../Magento/Ui/adminhtml/datepickerSpec.js    |  6 +--
 dev/tests/js/spec/integration/config.js       |  6 ---
 .../js/spec/integration/config/adminhtml.js   | 10 ++++
 .../js/spec/integration/config/frontend.js    | 10 ++++
 dev/tests/js/spec/require.config.js           | 14 +++++
 dev/tests/js/spec/shim.js                     |  6 +++
 dev/tests/js/spec/unit/config.js              |  6 ---
 dev/tests/js/spec/unit/config/adminhtml.js    | 10 ++++
 dev/tests/js/spec/unit/config/frontend.js     | 10 ++++
 12 files changed, 93 insertions(+), 67 deletions(-)
 delete mode 100644 dev/tests/js/spec/integration/config.js
 create mode 100644 dev/tests/js/spec/integration/config/adminhtml.js
 create mode 100644 dev/tests/js/spec/integration/config/frontend.js
 create mode 100644 dev/tests/js/spec/require.config.js
 create mode 100644 dev/tests/js/spec/shim.js
 delete mode 100644 dev/tests/js/spec/unit/config.js
 create mode 100644 dev/tests/js/spec/unit/config/adminhtml.js
 create mode 100644 dev/tests/js/spec/unit/config/frontend.js

diff --git a/Gruntfile.js b/Gruntfile.js
index be56ac1ed3b..05ef19e2e5c 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -9,8 +9,7 @@ module.exports = function (grunt) {
 
     //  Required plugins
     //  _____________________________________________
-    var specRunner = require('./dev/tests/js/framework/spec_runner');
-    specRunner.init(grunt);
+    var specRunner = require('./dev/tests/js/framework/spec_runner')(grunt);
 
     require('./dev/tools/grunt/tasks/mage-minify')(grunt);
 
@@ -489,10 +488,10 @@ module.exports = function (grunt) {
                 template: require('grunt-template-jasmine-requirejs'),
                 ignoreEmpty: true
             },
-            'backend-unit':           specRunner.build('unit', 'adminhtml', 8000),
-            'backend-integration':    specRunner.build('integration', 'adminhtml', 8000),
-            'frontend-unit':          specRunner.build('unit', 'frontend', 3000),
-            'frontend-integration':   specRunner.build('integration', 'frontend', 3000)
+            'backend-unit':           specRunner.configure('unit', 'adminhtml', 8000),
+            'backend-integration':    specRunner.configure('integration', 'adminhtml', 8000),
+            'frontend-unit':          specRunner.configure('unit', 'frontend', 3000),
+            'frontend-integration':   specRunner.configure('integration', 'frontend', 3000)
         }
     });
 
diff --git a/dev/tests/js/framework/spec_runner.js b/dev/tests/js/framework/spec_runner.js
index 153ae56f36b..d5b6d733208 100644
--- a/dev/tests/js/framework/spec_runner.js
+++ b/dev/tests/js/framework/spec_runner.js
@@ -2,14 +2,33 @@
  * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
  */
 
-/* global __dirname: true */
-
 /**
- * Initializes 'specRunner' grunt task.
+ * Creates jasmine configuration object
  *
- * @param  {Object} grunt
+ * @param  {String} type - type of tests
+ * @param  {String} dir - area dir
+ * @param  {Number} port - port to run on
+ * @return {Object}
  */
-module.exports.init = function (grunt) {
+function buildConfig(type, dir, port) {
+    'use strict';
+
+    return {
+        src: '<%= path.spec %>/shim.js',
+        options: {
+            host: 'http://localhost:' + port,
+            specs: '<%= path.spec %>/' + type + '/**/' + dir + '/**/*Spec.js',
+            templateOptions: {
+                requireConfigFile: [
+                    '<%= path.spec %>/require.config.js',
+                    '<%= path.spec %>/' + type + '/config/' + dir + '.js'
+                ]
+            }
+        }
+    };
+}
+
+module.exports = function (grunt) {
     'use strict';
 
     var connect     = require('connect'),
@@ -167,27 +186,6 @@ module.exports.init = function (grunt) {
     function canModify(url) {
         return url.match(/^\/(\.grunt)|(dev\/tests)|(dev\\tests)|(_SpecRunner\.html)/) === null;
     }
-};
 
-/**
- * Creates jasmine configuration object
- *
- * @param  {String} type - type of tests
- * @param  {String} dir - area dir
- * @param  {Number} port - port to run on
- * @return {Object}
- */
-module.exports.build = function (type, dir, port) {
-    'use strict';
-
-    return {
-        src: '<%= path.spec %>/env.js',
-        options: {
-            host: 'http://localhost:' + port,
-            specs: '<%= path.spec %>/' + type + '/**/' + dir + '/**/*Spec.js',
-            templateOptions: {
-                requireConfigFile: '<%= path.spec %>/' + type + '/config.js'
-            }
-        }
-    };
+    return { configure: buildConfig };
 };
diff --git a/dev/tests/js/spec/integration/Magento/PageCache/frontend/js/pageCacheSpec.js b/dev/tests/js/spec/integration/Magento/PageCache/frontend/js/pageCacheSpec.js
index a1c8a63fab3..14a609c9361 100644
--- a/dev/tests/js/spec/integration/Magento/PageCache/frontend/js/pageCacheSpec.js
+++ b/dev/tests/js/spec/integration/Magento/PageCache/frontend/js/pageCacheSpec.js
@@ -16,25 +16,6 @@ define([
 ], function ($) {
     'use strict';
 
-    if (!Function.prototype.bind) {
-        /**
-         * @param   {Object} bind
-         * @returns {Function}
-         */
-        Function.prototype.bind = function (bind) {
-            var self = this;
-
-            /**
-             * @returns {Function}
-             */
-            return function () {
-                var args = Array.prototype.slice.call(arguments);
-
-                return self.apply(bind || null, args);
-            };
-        };
-    }
-
     describe('Testing html-comments-parser $.fn.comments behavior', function () {
         var element,
             iframe,
diff --git a/dev/tests/js/spec/integration/Magento/Ui/adminhtml/datepickerSpec.js b/dev/tests/js/spec/integration/Magento/Ui/adminhtml/datepickerSpec.js
index bef0c0a93a2..68e9566528e 100644
--- a/dev/tests/js/spec/integration/Magento/Ui/adminhtml/datepickerSpec.js
+++ b/dev/tests/js/spec/integration/Magento/Ui/adminhtml/datepickerSpec.js
@@ -19,11 +19,11 @@ define([
 
             $(document.body).append(element);
 
-            ko.applyBindingsToNode(element, { datepicker: observable });
+            ko.applyBindingsToNode(element[0], { datepicker: observable });
         });
 
         afterEach(function () {
-            $(element).remove();
+            element.remove();
         });
 
         it('writes picked date\'s value to assigned observable', function () {
@@ -33,7 +33,7 @@ define([
                 dateFormat,
                 result;
 
-            dateFormat  = $(element).datepicker('option', 'dateFormat');
+            dateFormat  = element.datepicker('option', 'dateFormat');
             todayDate   = moment().format(dateFormat);
 
             openBtn  = $('img.ui-datepicker-trigger');
diff --git a/dev/tests/js/spec/integration/config.js b/dev/tests/js/spec/integration/config.js
deleted file mode 100644
index ade7f6387b1..00000000000
--- a/dev/tests/js/spec/integration/config.js
+++ /dev/null
@@ -1,6 +0,0 @@
-require.config({
-    deps: [
-        'jquery/jquery',
-        'underscore'
-    ]
-});
\ No newline at end of file
diff --git a/dev/tests/js/spec/integration/config/adminhtml.js b/dev/tests/js/spec/integration/config/adminhtml.js
new file mode 100644
index 00000000000..50f98a25b68
--- /dev/null
+++ b/dev/tests/js/spec/integration/config/adminhtml.js
@@ -0,0 +1,10 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+require.config({
+    paths: {
+        'jquery/ui': 'jquery/jquery-ui-1.9.2'
+    }
+});
\ No newline at end of file
diff --git a/dev/tests/js/spec/integration/config/frontend.js b/dev/tests/js/spec/integration/config/frontend.js
new file mode 100644
index 00000000000..8791f57a90c
--- /dev/null
+++ b/dev/tests/js/spec/integration/config/frontend.js
@@ -0,0 +1,10 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+require.config({
+    paths: {
+        'jquery/ui': 'jquery/jquery-ui'    
+    }
+})
\ No newline at end of file
diff --git a/dev/tests/js/spec/require.config.js b/dev/tests/js/spec/require.config.js
new file mode 100644
index 00000000000..f6f3838ed90
--- /dev/null
+++ b/dev/tests/js/spec/require.config.js
@@ -0,0 +1,14 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+require.config({
+    paths: {
+        'ko': 'ko/ko',
+        'domReady': 'requirejs/domReady'
+    },
+    shim: {
+        'jquery/ui': ['jquery']
+    }
+});
\ No newline at end of file
diff --git a/dev/tests/js/spec/shim.js b/dev/tests/js/spec/shim.js
new file mode 100644
index 00000000000..d816eb315da
--- /dev/null
+++ b/dev/tests/js/spec/shim.js
@@ -0,0 +1,6 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+Function.prototype.bind=(function(){}).bind||function(b){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");}function c(){}var a=[].slice,f=a.call(arguments,1),e=this,d=function(){return e.apply(this instanceof c?this:b||window,f.concat(a.call(arguments)));};c.prototype=this.prototype;d.prototype=new c();return d;};
\ No newline at end of file
diff --git a/dev/tests/js/spec/unit/config.js b/dev/tests/js/spec/unit/config.js
deleted file mode 100644
index ade7f6387b1..00000000000
--- a/dev/tests/js/spec/unit/config.js
+++ /dev/null
@@ -1,6 +0,0 @@
-require.config({
-    deps: [
-        'jquery/jquery',
-        'underscore'
-    ]
-});
\ No newline at end of file
diff --git a/dev/tests/js/spec/unit/config/adminhtml.js b/dev/tests/js/spec/unit/config/adminhtml.js
new file mode 100644
index 00000000000..50f98a25b68
--- /dev/null
+++ b/dev/tests/js/spec/unit/config/adminhtml.js
@@ -0,0 +1,10 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+require.config({
+    paths: {
+        'jquery/ui': 'jquery/jquery-ui-1.9.2'
+    }
+});
\ No newline at end of file
diff --git a/dev/tests/js/spec/unit/config/frontend.js b/dev/tests/js/spec/unit/config/frontend.js
new file mode 100644
index 00000000000..8791f57a90c
--- /dev/null
+++ b/dev/tests/js/spec/unit/config/frontend.js
@@ -0,0 +1,10 @@
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+require.config({
+    paths: {
+        'jquery/ui': 'jquery/jquery-ui'    
+    }
+})
\ No newline at end of file
-- 
GitLab