Permalink
Browse files

Add Grunt; jshint fixes; more tests

  • Loading branch information...
1 parent 9b28521 commit c821f7fbc443d7e496e777667b2baab3212fd785 @jboyens jboyens committed Feb 19, 2013
Showing with 83 additions and 11 deletions.
  1. +2 −1 .gitignore
  2. +46 −0 Gruntfile.js
  3. +11 −10 lib/mock-ajax.js
  4. +11 −0 package.json
  5. +13 −0 spec/javascripts/webmock-style-spec.js
View
@@ -1,2 +1,3 @@
.idea/
-.rvmrc
+.rvmrc
+node_modules
View
@@ -0,0 +1,46 @@
+module.exports = function( grunt ) {
+ 'use strict';
+ //
+ // Grunt configuration:
+ //
+ // https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
+ //
+ grunt.initConfig({
+ // specifying JSHint options and globals
+ // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#specifying-jshint-options-and-globals
+ jshint: {
+ options: {
+ boss: true,
+ browser: true,
+ curly: true,
+ eqeqeq: true,
+ eqnull: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ globals: {
+ jasmine: false,
+ ajaxRequests: true,
+ ajaxStubs: true,
+ module: false,
+ exports: true
+ }
+ },
+ all: ['Gruntfile.js', 'lib/*.js', 'spec/*.js']
+ },
+ shell: {
+ ctags: {
+ command: 'ctags -R lib'
+ }
+ }
+ });
+
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-shell');
+
+ grunt.registerTask('default', ['jshint']);
+ grunt.registerTask('ctags', 'Generate ctags', ['shell:ctags']);
+};
View
@@ -51,27 +51,29 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
clearAjaxStubs: function() {
ajaxStubs = [];
}
- }
+ };
function extend(destination, source) {
- for (var property in source) destination[property] = source[property];
+ for (var property in source) {
+ destination[property] = source[property];
+ }
return destination;
}
- if (typeof window == "undefined" && typeof exports == "object") {
+ if (typeof window === "undefined" && typeof exports === "object") {
extend(exports, jasmineAjaxInterface);
} else {
extend(window, jasmineAjaxInterface);
}
// Fake XHR for mocking Ajax Requests & Responses
- FakeXMLHttpRequest = function() {
+ window.FakeXMLHttpRequest = function() {
ajaxRequests.push(this);
};
- extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest);
- extend(FakeXMLHttpRequest.prototype, {
+ extend(window.FakeXMLHttpRequest.prototype, window.XMLHttpRequest);
+ extend(window.FakeXMLHttpRequest.prototype, {
requestHeaders: {},
open: function() {
@@ -112,7 +114,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data: function() {
var data = {};
- if (typeof this.params !== 'string') return data;
+ if (typeof this.params !== 'string') { return data; }
var params = this.params.split('&');
for (var i = 0; i < params.length; ++i) {
@@ -187,7 +189,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
installTopLevel: function() {
jasmine.Ajax.mode = 'toplevel';
jasmine.Ajax.real = window.XMLHttpRequest;
- window.XMLHttpRequest = FakeXMLHttpRequest;
+ window.XMLHttpRequest = window.FakeXMLHttpRequest;
},
uninstallMock: function() {
@@ -214,7 +216,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.responseText = options.responseText;
};
-
var stub = new Stub(url);
ajaxStubs.push(stub);
@@ -225,7 +226,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
matchStub: function(url) {
for (var i = ajaxStubs.length - 1; i >= 0; i--) {
var stub = ajaxStubs[i];
- if (stub.url == url) {
+ if (stub.url === url) {
return stub;
}
}
View
@@ -0,0 +1,11 @@
+{
+ "name": "jasmine-ajax",
+ "version": "2.0.0",
+ "private": false,
+ "dependencies": {
+ },
+ "devDependencies": {
+ "grunt": "0.4.0"
+ "grunt-contrib-jshint": "~0.1.1"
+ }
+}
@@ -71,5 +71,18 @@ describe("Webmock style mocking", function() {
expect(stub).toBeDefined();
});
+
+ describe("with another stub for the same url", function() {
+ beforeEach(function() {
+ jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({responseText: "no", status: 403});
+ });
+
+ it("should use the latest stub", function() {
+ var stub = jasmine.Ajax.matchStub("http://example.com/someApi");
+
+ expect(stub.status).toEqual(403);
+ expect(stub.responseText).toEqual('no');
+ });
+ });
});
});

0 comments on commit c821f7f

Please sign in to comment.