Просмотр исходного кода

runloop & serializers tests, wip ajax service tests

Rafał Pitoń 9 лет назад
Родитель
Сommit
83ef46a7aa

+ 2 - 1
misago/frontend/bower.json

@@ -7,6 +7,7 @@
     "mithril": "*",
     "mithril": "*",
     "bootstrap": "~3.3.5",
     "bootstrap": "~3.3.5",
     "ace-builds": "~1.2.0",
     "ace-builds": "~1.2.0",
-    "moment": "~2.10.6"
+    "moment": "~2.10.6",
+    "jquery-mockjax": "~2.0.1"
   }
   }
 }
 }

+ 16 - 1
misago/frontend/gulpfile.js

@@ -30,6 +30,10 @@ var ace = [
   'bower_components/ace-builds/src/mode-markdown.js'
   'bower_components/ace-builds/src/mode-markdown.js'
 ];
 ];
 
 
+var testLibs = [
+  'bower_components/jquery-mockjax/dist/jquery.mockjax.js'
+];
+
 gulp.task('lint', function() {
 gulp.task('lint', function() {
   return gulp.src(['misago/*.js', 'misago/**/*.js'])
   return gulp.src(['misago/*.js', 'misago/**/*.js'])
     .pipe(jshint(packageJSON.jshintConfig))
     .pipe(jshint(packageJSON.jshintConfig))
@@ -186,6 +190,12 @@ gulp.task('collecttestimg', ['cleantest', 'copyimg'], function() {
     .pipe(gulp.dest('test/dist/img'));
     .pipe(gulp.dest('test/dist/img'));
 });
 });
 
 
+gulp.task('collecttestslibs', ['cleantest'], function() {
+  return gulp.src(testLibs)
+    .pipe(concat('libs.js'))
+    .pipe(gulp.dest('test/dist'));
+});
+
 gulp.task('collecttestsutils', ['cleantest'], function() {
 gulp.task('collecttestsutils', ['cleantest'], function() {
   return gulp.src('test/utils/**/*.js')
   return gulp.src('test/utils/**/*.js')
     .pipe(jshint(packageJSON.jshintConfig))
     .pipe(jshint(packageJSON.jshintConfig))
@@ -206,6 +216,7 @@ gulp.task('collecttests', ['cleantest'], function() {
 gulp.task('starttestserver', [
 gulp.task('starttestserver', [
     'collecttests',
     'collecttests',
     'collecttestsutils',
     'collecttestsutils',
+    'collecttestslibs',
     'collecttestjs',
     'collecttestjs',
     'collecttestcss',
     'collecttestcss',
     'collecttestfonts',
     'collecttestfonts',
@@ -224,6 +235,10 @@ gulp.task('test', ['starttestserver'], function() {
   gulp.watch([
   gulp.watch([
     'test/tests/**/*.js', 'misago/**/*.js', 'misago/**/*.less'
     'test/tests/**/*.js', 'misago/**/*.js', 'misago/**/*.less'
   ], [
   ], [
-    'collecttests', 'collecttestjs', 'collecttestcss', 'collecttestsutils'
+    'collecttests',
+    'collecttestjs',
+    'collecttestcss',
+    'collecttestsutils',
+    'collecttestslibs'
   ]);
   ]);
 });
 });

+ 23 - 3
misago/frontend/misago/services/ajax.js

@@ -2,9 +2,17 @@
   'use strict';
   'use strict';
 
 
   var Ajax = function(_) {
   var Ajax = function(_) {
-    var cookieRegex = new RegExp(_.context.CSRF_COOKIE_NAME + '\=([^;]*)');
-    this.csrfToken = Misago.get(
-      document.cookie.match(cookieRegex), 0).split('=')[1];
+    var getCsrfToken = function(cookie_name) {
+      if (document.cookie.indexOf(cookie_name) !== -1) {
+        var cookieRegex = new RegExp(cookie_name + '\=([^;]*)');
+        var cookie = Misago.get(document.cookie.match(cookieRegex), '');
+        return cookie.split('=')[1];
+      } else {
+        return null;
+      }
+    };
+
+    this.csrfToken = getCsrfToken(_.context.CSRF_COOKIE_NAME);
 
 
     /*
     /*
       List of GETs underway
       List of GETs underway
@@ -70,6 +78,18 @@
     this.post = function(url) {
     this.post = function(url) {
       return this.ajax('POST', url);
       return this.ajax('POST', url);
     };
     };
+
+    this.patch = function(url) {
+      return this.ajax('PATCH', url);
+    };
+
+    this.put = function(url) {
+      return this.ajax('PUT', url);
+    };
+
+    this.delete = function(url) {
+      return this.ajax('DELETE', url);
+    };
   };
   };
 
 
   Misago.addService('ajax', function(_) {
   Misago.addService('ajax', function(_) {

+ 1 - 0
misago/frontend/test/index.html

@@ -12,6 +12,7 @@
   <div id="qunit-fixture"></div>
   <div id="qunit-fixture"></div>
   <script src="//code.jquery.com/qunit/qunit-1.18.0.js"></script>
   <script src="//code.jquery.com/qunit/qunit-1.18.0.js"></script>
   <script src="/dist/js/vendor.js"></script>
   <script src="/dist/js/vendor.js"></script>
+  <script src="/dist/libs.js"></script>
   <script src="/dist/utils.js"></script>
   <script src="/dist/utils.js"></script>
   <script src="/dist/js/misago.js"></script>
   <script src="/dist/js/misago.js"></script>
   <script src="/dist/tests.js"></script>
   <script src="/dist/tests.js"></script>

+ 66 - 0
misago/frontend/test/tests/unit/ajax.js

@@ -0,0 +1,66 @@
+(function () {
+  'use strict';
+
+  QUnit.module("Ajax", {
+    afterEach: function() {
+      $.mockjax.clear();
+    }
+  });
+
+  QUnit.test("service factory", function(assert) {
+    var container = { context: { CSRF_COOKIE_NAME: 'doesnt-matter' } };
+
+    var service = getMisagoService('ajax');
+    var ajax = service(container);
+
+    assert.ok(ajax,
+      "service factory has returned service instance.");
+  });
+
+  QUnit.test("ajax success", function(assert) {
+    $.mockjax({
+      url: '/working-url/',
+      status: 200,
+      responseText: {
+        'detail': 'ok'
+      }
+    });
+
+    var container = { context: { CSRF_COOKIE_NAME: 'doesnt-matter' } };
+
+    var service = getMisagoService('ajax');
+    var ajax = service(container);
+
+    var done = assert.async();
+
+    ajax.ajax('GET', '/working-url/').then(function(data) {
+      assert.equal(data.detail, 'ok', 'ajax succeeded on /working-url/');
+      done();
+    });
+  });
+
+  QUnit.test("ajax fail", function(assert) {
+    $.mockjax({
+      url: '/failing-url/',
+      status: 400,
+      responseText: {
+        'detail': 'fail'
+      }
+    });
+
+    var container = { context: { CSRF_COOKIE_NAME: 'doesnt-matter' } };
+
+    var service = getMisagoService('ajax');
+    var ajax = service(container);
+
+    var done = assert.async();
+
+    ajax.ajax('GET', '/failing-url/').then(function() {
+      assert.ok(false, 'ajax succeeded on /failing-url/');
+    }, function(rejection) {
+      assert.equal(rejection.detail, 'fail',
+        'ajax handled error from /failing-url/');
+      done();
+    });
+  });
+}());

+ 82 - 0
misago/frontend/test/tests/unit/runloop.js

@@ -0,0 +1,82 @@
+(function () {
+  'use strict';
+
+  QUnit.module("RunLoop");
+
+  var service = getMisagoService('runloop');
+
+  QUnit.test("run", function(assert) {
+    var container = {timesLeft: 5};
+    var runloop = service.factory(container);
+
+    var done = assert.async();
+
+    runloop.run(function(_) {
+      assert.ok(_.timesLeft, "#" + (6 - _.timesLeft) + ": runloop is called");
+      _.timesLeft -= 1;
+
+      if (_.timesLeft === 0) {
+        assert.equal(runloop._intervals['test-loop'], null,
+          "test loop was stopped");
+        done();
+        return false;
+      } else if (_.timesLeft < 0) {
+        assert.ok(false, 'runloop was not finished by false from function');
+      }
+    }, 'test-loop', 100);
+  });
+
+  QUnit.test("runOnce", function(assert) {
+    var container = {};
+    var runloop = service.factory(container);
+
+    var ranOnce = assert.async();
+    runloop.runOnce(function(_) {
+      assert.equal(runloop._intervals['test-run-once'], null,
+        "runloop item name was removed from intervals");
+
+      assert.equal(container, _, "test task was ran with container");
+      ranOnce();
+    }, 'test-run-once', 80);
+
+    assert.ok(runloop._intervals['test-run-once'],
+      "runloop item name was added to intervals");
+
+    var ranOnceOther = assert.async();
+    runloop.runOnce(function(_) {
+      assert.equal(runloop._intervals['test-run-once-other'], null,
+        "runloop item name was removed from intervals");
+
+      assert.equal(container, _, "other test task was ran with container");
+      ranOnceOther();
+    }, 'test-run-once-other', 100);
+
+    assert.ok(runloop._intervals['test-run-once-other'],
+      "runloop item name was added to intervals");
+  });
+
+  QUnit.test("stop", function(assert) {
+    var container = {};
+    var runloop = service.factory(container);
+
+    runloop.run(function() {
+      assert.ok(false, "runloop should be canceled before first run");
+      return false;
+    }, 'canceled-loop', 500);
+
+    runloop.stop('canceled-loop');
+
+    assert.equal(runloop._intervals['canceled-loop'], null,
+      "stop canceled named loop");
+
+    runloop.run(function() {
+      assert.ok(false, "runloop should be canceled before first run");
+      return false;
+    }, 'other-canceled-loop', 500);
+
+    runloop.stop();
+
+    assert.equal(runloop._intervals['other-canceled-loop'], null,
+      "stop canceled all loops");
+  });
+}());

+ 23 - 0
misago/frontend/test/tests/unit/serializers.js

@@ -0,0 +1,23 @@
+(function (Misago) {
+  'use strict';
+
+  QUnit.module("Serializers");
+
+  QUnit.test("datetime", function(assert) {
+    assert.equal(Misago.serializeDatetime(null), null,
+      "serialization of null datetime value returned null");
+    assert.equal(Misago.deserializeDatetime(null), null,
+      "deserialization of null datetime value returned null");
+
+    var test_moment = moment();
+
+    assert.equal(
+      Misago.serializeDatetime(test_moment), test_moment.format(),
+      "datetime was serialized to string");
+
+    assert.equal(
+      Misago.deserializeDatetime(test_moment.format()).format(),
+      test_moment.format(),
+      "datetime was deserialized to momentjs object");
+  });
+}(Misago.prototype));