From dfe50882d44cd2f904124fe36eb554076df5a0f1 Mon Sep 17 00:00:00 2001
From: George Gelashvili and Tira Odhner <pair+ggelashvili+aodhner@pivotal.io>
Date: Tue, 7 Mar 2017 15:04:30 -0500
Subject: [PATCH 1/6] Add jasmine

- it runs with karma from the commandline
- it is outside of the /web folder so that we can exclude tests and test dependencies from the packaged app.
---
 .gitignore                   |   1 +
 karma.conf.js                |  75 ++++++++++++++++++++++++++++
 package.json                 |  10 ++++
 test/javascript/test-main.js | 115 +++++++++++++++++++++++++++++++++++++++++++
 web/regression/README        |  16 ++++++
 5 files changed, 217 insertions(+)
 create mode 100644 karma.conf.js
 create mode 100644 package.json
 create mode 100644 test/javascript/test-main.js

diff --git a/.gitignore b/.gitignore
index 0119b5cf..3ccd1782 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,3 +36,4 @@ runtime/release/
 runtime/ui_BrowserWindow.h
 web/config_local.py
 web/regression/test_config.json
+node_modules/
diff --git a/karma.conf.js b/karma.conf.js
new file mode 100644
index 00000000..2f984904
--- /dev/null
+++ b/karma.conf.js
@@ -0,0 +1,75 @@
+// Karma configuration
+// Generated on Wed Mar 01 2017 14:19:28 GMT-0500 (EST)
+
+module.exports = function(config) {
+  config.set({
+
+    // base path that will be used to resolve all patterns (eg. files, exclude)
+    basePath: '',
+
+
+    // frameworks to use
+    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
+    frameworks: ['jasmine', 'requirejs'],
+
+
+    // list of files / patterns to load in the browser
+    files: [
+      {pattern: 'test/javascript/**/*.js', included: false},
+      {pattern: 'web/pgadmin/static/vendor/**/*.js', included: false},
+      {pattern: 'web/pgadmin/static/js/**/*.js', included: false},
+      'test/javascript/test-main.js'
+    ],
+
+
+    // list of files to exclude
+    exclude: [
+      'web/pgadmin/static/js/pgadmin.js',
+      'web/pgadmin/static/vendor/**/*[Tt]est.js',
+      'web/pgadmin/static/vendor/**/*[Ss]pec.js'
+    ],
+
+
+    // preprocess matching files before serving them to the browser
+    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
+    preprocessors: {
+    },
+
+
+    // test results reporter to use
+    // possible values: 'dots', 'progress'
+    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
+    reporters: ['progress'],
+
+
+    // web server port
+    port: 9876,
+
+
+    // enable / disable colors in the output (reporters and logs)
+    colors: true,
+
+
+    // level of logging
+    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
+    logLevel: config.LOG_INFO,
+
+
+    // enable / disable watching file and executing tests whenever any file changes
+    autoWatch: false,
+
+
+    // start these browsers
+    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
+    browsers: ['PhantomJS'],
+
+
+    // Continuous Integration mode
+    // if true, Karma captures browsers, runs the tests and exits
+    singleRun: false,
+
+    // Concurrency level
+    // how many browser should be started simultaneous
+    concurrency: Infinity
+  })
+}
diff --git a/package.json b/package.json
new file mode 100644
index 00000000..80e4e5ad
--- /dev/null
+++ b/package.json
@@ -0,0 +1,10 @@
+{
+  "devDependencies": {
+    "jasmine-core": "^2.5.2",
+    "karma": "^1.5.0",
+    "karma-jasmine": "^1.1.0",
+    "karma-phantomjs-launcher": "^1.0.2",
+    "karma-requirejs": "^1.1.0",
+    "requirejs": "^2.3.3"
+  }
+}
diff --git a/test/javascript/test-main.js b/test/javascript/test-main.js
new file mode 100644
index 00000000..b0726a0d
--- /dev/null
+++ b/test/javascript/test-main.js
@@ -0,0 +1,115 @@
+var allTestFiles = [];
+var TEST_REGEXP = /(spec|test)\.js$/i;
+
+// Get a list of all the test files to include
+Object.keys(window.__karma__.files).forEach(function (file) {
+  if (TEST_REGEXP.test(file)) {
+    // Normalize paths to RequireJS module names.
+    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
+    // then do not normalize the paths
+    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
+    allTestFiles.push(normalizedTestModule)
+  }
+});
+
+var sourcesDir = '/base/web/pgadmin/static/';
+require.config({
+  // Karma serves files under /base, which is the basePath from your config file
+  baseUrl: '/base',
+
+  paths: {
+    'alertify': sourcesDir + 'vendor/alertifyjs/alertify',
+    'jquery': sourcesDir + 'vendor/jquery/jquery-1.11.2',
+    'jquery.ui': sourcesDir + 'vendor/jquery-ui/jquery-ui-1.11.3',
+    'jquery.event.drag': sourcesDir + 'vendor/jquery-ui/jquery.event.drag-2.2',
+    'underscore': sourcesDir + 'vendor/underscore/underscore',
+    'slickgrid': sourcesDir + 'vendor/slickgrid/slick.core',
+    'slickgrid/slick.grid': sourcesDir + 'vendor/slickgrid/slick.grid',
+    'slickgrid/slick.rowselectionmodel': sourcesDir + 'vendor/slickgrid/plugins/slick.rowselectionmodel',
+    'translations': '/base/test/javascript/fake_translations',
+    'sources': sourcesDir + 'js'
+  },
+
+  shim: {
+    'underscore': {
+      exports: '_'
+    },
+    "slickgrid": {
+      "deps": [
+        'jquery', "jquery.ui", "jquery.event.drag"
+      ],
+      "exports": 'window.Slick'
+    },
+    "slickgrid/slick.grid": {
+      "deps": [
+        'jquery', "jquery.ui", "jquery.event.drag", "slickgrid"
+      ],
+      "exports": 'window.Slick.Grid'
+    },
+    "slickgrid/slick.rowselectionmodel": {
+      "deps": [
+        "jquery"
+      ],
+      "exports": 'window.Slick.RowSelectionModel'
+    },
+    "backbone": {
+      "deps": ['underscore', 'jquery'],
+      "exports": 'Backbone'
+    },
+    "backbone.paginator": {
+      "deps": ['underscore', 'jquery', 'backbone']
+    },
+    "bootstrap": {
+      "deps": ['jquery'],
+    },
+    "backgrid": {
+      "deps": ['backform'],
+      "exports": 'Backgrid',
+    },
+    "backgrid.select.all": {
+      "deps": ['backgrid']
+    },
+    "backgrid.paginator": {
+      "deps": ['backgrid', 'backbone.paginator']
+    },
+    "backgrid.filter": {
+      "deps": ['backgrid']
+    },
+    "backgrid.sizeable.columns": {
+      "deps": ['backgrid']
+    },
+    "bootstrap.switch": {
+      "deps": ['jquery', 'bootstrap'],
+      "exports": 'jQuery.fn.bootstrapSwitch'
+    },
+    "select2": {
+      "deps": ['jquery'],
+      "exports": 'jQuery.fn.select2'
+    },
+    "bootstrap.datepicker": {
+      "deps": ['jquery', 'bootstrap'],
+      "exports": 'jQuery.fn.datepicker'
+    },
+    "bootstrap.datetimepicker": {
+      "deps": ['jquery', 'bootstrap', 'moment'],
+      "exports": 'jQuery.fn.datetimepicker'
+    },
+    "pgadmin.backgrid": {
+      "deps": ["backgrid", "bootstrap.datetimepicker", "bootstrap.switch"],
+    },
+    "pgadmin.backform": {
+      "deps": ['backform', "pgadmin.backgrid", "select2"],
+    },
+    "jquery.event.drag": {
+      "deps": ['jquery'], "exports": 'jQuery.fn.drag'
+    },
+    "jquery.ui": {"deps": ['jquery']}
+  },
+
+  // dynamically load all test files
+  deps: allTestFiles,
+
+  // we have to kickoff jasmine, as it is asynchronous
+  callback: window.__karma__.start
+});
+
diff --git a/web/regression/README b/web/regression/README
index 6a543cdb..6e3723d5 100644
--- a/web/regression/README
+++ b/web/regression/README
@@ -113,6 +113,8 @@ Test Data Details
 Execution:
 -----------
 
+Python Tests
+
 - For feature tests to run as part of the entire test suite, Chrome and
   chromedriver need to be installed; get chromedriver from
   https://sites.google.com/a/chromium.org/chromedriver/downloads or a
@@ -153,3 +155,17 @@ Execution:
 
     Example: exclude multiple packages:
     run 'python runtests.py --exclude browser.server_groups.servers.databases,browser.server_groups.servers.tablespaces'
+
+Javascript Tests
+    brew install nodejs
+    npm install
+
+    you may wish to install the karma cli, so it is accessible globally:
+    npm install -g karma-cli
+
+    Otherwise you can use:
+    ./node_modules/karma/bin/karma
+
+    to run tests:
+    karma start --single-run
+
-- 
2.12.0

