Rufus Pollock avatar Rufus Pollock committed 974e2e7

[tests][m]: implement first basic tests of the sammy app using qunit based on sammy's own qunit based tests

* qunit-spec.js: quirkey's own bespoke 'spec' framework for qunit (required for tests in his style ...)

Comments (0)

Files changed (3)

_attachments/tests/runtests.html

 <head>
   <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
   <script type="text/javascript" src="vendor/qunit.js"></script>
+  <script type="text/javascript" src="vendor/qunit-spec.js"></script>
 
-  <script src="http://localhost:5984/_utils/script/jquery.js"></script>
-  <script src="http://localhost:5984/_utils/script/jquery.couch.js"></script>
-  <script type="text/javascript" src="test_storecouch.js"></script>
+  <script src="../script/vendor/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
+  <script src="/_utils/script/jquery.couch.js?0.8.0"></script>
+  <script src="../script/vendor/sammy/sammy.js" type="text/javascript" charset="utf-8"></script>
+  <script src="../script/vendor/sammy/plugins/sammy.mustache.js" type="text/javascript" charset="utf-8"></script>
+  <script src="../script/vendor/sammy/plugins/sammy.title.js" type="text/javascript" charset="utf-8"></script>
+  <script src="../script/vendor/showdown.js" type="text/javascript" charset="utf-8"></script>
+  <script src="../script/app.js" type="text/javascript" charset="utf-8"></script>
+
+  <!--script type="text/javascript" src="test_storecouch.js"></script-->
+    <script type="text/javascript" src="test_sammy_application.js"></script>
 </head>
 <body>
   <h1 id="qunit-header">CommentOnIt Tests</h1>
   <h2 id="qunit-banner"></h2>
   <h2 id="qunit-userAgent"></h2>
   <ol id="qunit-tests"></ol>
+
+  <div id="main"> 
+    <div id="container"> 
+      <div id="content" role="main"> 
+
+        <div class="page type-page hentry">
+          <div class="entry-content">
+            <!-- content goes here -->
+          </div><!-- .entry-content -->
+        </div>
+
+      </div> <!-- #content -->
+    </div> <!-- #container -->
+  </div>
 </body>
 </html>

_attachments/tests/test_sammy_application.js

+(function($) {
+  // $(function() {
+  with(QUnit) {
+      context('Sammy.Application','run', {
+        before: function () {
+          window.location.hash = ''
+          var context = this;
+          this.app = CommentOnIt.app;
+        }
+      })
+      .should('set the location to the start url', function() {
+        var app = this.app;
+        app.run('#/');
+        soon(function() {
+          equal(window.location.hash, '#/');
+          app.unload();
+        });
+      })
+      .should('trigger routes on URL change', function() {
+        var app = this.app;
+        app.run();
+        window.location.hash = '#/text/edit';
+        soon(function() {
+          equal(document.title.slice(0,13), 'Create - Text');
+          // TODO
+          // equal($('.entry-content').text(), 'test success');
+          app.unload();
+        });
+      })
+    }
+  // });
+})(jQuery);

_attachments/tests/vendor/qunit-spec.js

+(function() {
+
+  QUnit.Spec = function(name) {
+    this.before = false;
+    this.after = false;
+    QUnit.module(name);
+  };
+  
+  function extend(a, b) {
+    for ( var prop in b ) {
+      a[prop] = b[prop];
+    }
+    return a;
+  }
+
+  // RSpec style describe
+  // takes an arbitrary number of arguments that are contactenated as strings
+  // the last argument is the configuration object
+  // which can have before: after: callbacks
+  function describe() {
+    var args = Array.prototype.slice.call(arguments),
+    // configuration function
+      config = (args.length > 0 && args[args.length - 1]['before']) ? args.pop() : {},
+      spec = new QUnit.Spec(args.join(' '));
+    spec['before'] = config['before'] || config['setup'];
+    spec['after']  = config['after'] || config['teardown'];
+    return spec;
+  }
+
+
+  extend(QUnit.Spec.prototype, {
+    
+    // RSpec style test definition
+    it: function(name, callback, nowait) {
+      var spec = this;
+      var spec_context = {};
+      QUnit.test(name, function() { 
+        if (spec.before) spec.before.apply(spec_context);
+        callback.apply(spec_context, [this]); 
+        if (spec.after) spec.after.apply(spec_context);
+      });
+      return spec;
+    },
+
+    // Shoulda style test definition
+    should: function(name, callback, nowait) {
+      name = 'should ' + name;
+      return this.it.apply(this, [name, callback, nowait]);
+    },
+    
+    pending: function(name, callback, nowait) {
+      name = '<span style="color: #EB8531;" class="pending">DEFERRED: ' + name + '</span>';
+      return this.it.apply(this, [name, function () { QUnit.ok(true) }, nowait]);
+    },
+    
+    should_eventually: function(name, callback, nowait) {
+      return this.pending(name, callback, nowait);
+    }
+    
+  });
+
+
+  extend(QUnit, {
+    // aliases for describe
+    describe: describe,
+    context: describe,
+
+    // asserts that the method is defined (like respond_to?)
+    defined: function(object, method) {
+      return QUnit.ok(QUnit.is('Function', object[method]), method + ' is not defined on ' + object);
+    },
+
+    // asserts that the object is of a certain type
+    isType: function(object, type) {
+      return ok(QUnit.is(type, object), "expected " + object + " to be a " + type);
+    },
+    
+    // assert a string matches a regex
+    matches: function(matcher, string, message) {
+      return QUnit.ok(!!matcher.test(string), message || "expected: " + string + "match(" + matcher.toString() + ")");
+    },
+        
+    // assert that a matching error is raised
+    // expected can be a regex, a string, or an object
+    raised: function(expected_error, callback) {
+      var error = null;
+      try {
+        callback.apply(this);
+      } catch(e) {
+        error = e;
+      }
+      error_message = error && error.message ? error.message.toString() : error.toString();
+      message = "expected error: " + expected_error.toString() + ", actual error:" + error_message;
+      if (expected_error.constructor == RegExp) {
+        return QUnit.matches(expected_error, error_message, message);
+      } else if (expected_error.constructor == String) {
+        return QUnit.equal(expected_error, error_message, message);
+      } else {
+        return QUnit.deepEqual(expected_error, error, message);
+      }
+    },
+    
+    notRaised: function(callback) {
+      var error = '';
+      try {
+        callback.apply(this);
+      } catch(e) {
+        error = e;
+      }
+      message = "expected: no errors, actual error: " + error.toString();
+      QUnit.equal('', error, message);
+    },
+    
+    soon: function(callback, context, secs, many_expects) {
+      if (typeof context == 'undefined') context = this;
+      if (typeof secs == 'undefined') secs = 1;
+      if (typeof many_expects == 'undefined') many_expects = 1;
+      QUnit.expect(many_expects);
+      QUnit.stop();
+      setTimeout(function() {
+        callback.apply(context);
+        QUnit.start();
+      }, secs * 500);
+    },
+    
+    flunk: function() {
+      QUnit.ok(false, 'FLUNK');
+    },
+    
+    sameHTML: function(actual, expected) {
+      var strippedHTML = function(element) {
+        return $(element)
+          .wrap('<div></div>')
+          .parent()
+          .html()
+          .toString()
+          .replace(/(>)(\s+)(<)/g, "><");
+      };
+
+      actual = strippedHTML(actual);
+      expected = strippedHTML(expected);
+      equal(actual, expected, "HTML is equal");
+    }
+    
+  });
+
+
+})();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.