Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
when the url is a regex, we get it's parameters in the content
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Mathieu committed Mar 26, 2010
1 parent 1bebc2e commit 72d09e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
32 changes: 25 additions & 7 deletions moqueur.js
Expand Up @@ -6,7 +6,7 @@

$.mockAjax = function(params) {
$._mocked.push(params);
}
},

$.ajax = function() {
var args = arguments[0];
Expand All @@ -16,13 +16,31 @@
if (
($._mocked[i].url.constructor == String && args.url == $._mocked[i].url) ||
($._mocked[i].url.constructor == RegExp && args.url.search($._mocked[i].url) > -1)
)
if (args.success != undefined)
return args.success($._mocked[i].content);
else
return $._mocked[i].content;
) {
var content = $._mockAjax.parse_content($._mocked[i].url, args.url, $._mocked[i].content);

if (args.success != undefined)
return args.success(content);
else
return content;
}
};

return $._dup_ajax.apply($, arguments);
};
},


$._mockAjax = {

parse_content: function(regex, url, content) {
if (regex.constructor == RegExp) {
var occurencies = regex.exec(url);
for(i=1; i <= occurencies.length; i++) {
content = content.replace('$' + i, occurencies[i]);
}
}

return content;
}
}
})(jQuery);
2 changes: 1 addition & 1 deletion spec/fixtures/default_mocks.js
Expand Up @@ -5,5 +5,5 @@ jQuery.mockAjax({

jQuery.mockAjax({
url: new RegExp('^/regex/([0-9]+)$'),
content: "Request with regex."
content: "Request with regex. Param: $1"
});
6 changes: 6 additions & 0 deletions spec/lib/moqueur_spec.rb
Expand Up @@ -23,4 +23,10 @@
success: function() { return 'Executed Success method with regex'; }
})").should eql('Executed Success method with regex')
end

it 'should give the param in the content when regex' do
@page.execute_js("jQuery.ajax({
url: '/regex/1234'
})").should eql('Request with regex. Param: 1234')
end
end

0 comments on commit 72d09e6

Please sign in to comment.