Skip to content

Commit

Permalink
Add mocks for XHR and confirm; Improved flow for the iframe; Fix even…
Browse files Browse the repository at this point in the history
…ts and make fake submit events actually submit
  • Loading branch information
wycats committed Jul 4, 2008
1 parent f0a8c8d commit 22badf2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
45 changes: 22 additions & 23 deletions merb_screw_unit/app/views/layout/merb_screw_unit.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
<html>
<head>
<%= js_include_tag "jquery-1.2.3", "jquery.fn", "jquery.print", "screw.builder",
"screw.matchers", "screw.events", "screw.behaviors" %>
"screw.matchers", "screw.events", "screw.behaviors",
"screw.mock" %>

<%= css_include_tag "screw.css" %>
</head>
<body>
<iframe id="dom" src="<%= @spec_url %>" style="display: none"></iframe>
<script>
iframeWindow = $("#dom").contents()[0].defaultView;
runLivequeries = function() {
if(iframeWindow.$.livequery) {
queries = iframeWindow.$.livequery.queries;
for(var i = 0; i<queries.length;i++) {
queries[i].run();
}
}
}

<iframe id="dom" src="<%= @spec_url %>?<%= MD5.hexdigest(Time.now.to_s) %>" style="display: none"></iframe>
<script>
Screw.Unit(function() {
before(function() {
iframeWindow = $("#dom").contents()[0].defaultView;
iframeWindow.XMLHttpRequest = Screw.XHR;
runLivequeries = function() {
if(iframeWindow.$.livequery) {
queries = iframeWindow.$.livequery.queries;
for(var i = 0; i<queries.length;i++) {
queries[i].run();
}
}
};

before(function() {
$ = iframeWindow.$;
$.ajaxSettings.async = false;
$.fn.fireEvent = function(event) {
runLivequeries();
for(var i=0; i<this.length;i++) {
var node = this[i];
var data = iframeWindow.$.cache[iframeWindow.$.data(node)];
eventObj = data && data.events && data.events[event];
if(eventObj) {
for(prop in eventObj) eventObj[prop].call(node, document.createEvent("MouseEvent"))
}
}
this.trigger(event);
this.each(function() {
if($(this).is(":submit")) $(this).parents("form:first").submit();
});
return this;
}
};
})
<%= catch_content :for_layout %>
});
Expand Down
3 changes: 3 additions & 0 deletions merb_screw_unit/lib/merb_screw_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Spec; end
$:.unshift File.dirname(__FILE__)

load_dependency 'merb-slices'
load_dependency 'merb-assets'
require 'md5'

Merb::Plugins.add_rakefiles "merb_screw_unit/merbtasks", "merb_screw_unit/slicetasks"

# Register the Slice for the current host application
Expand Down
31 changes: 31 additions & 0 deletions merb_screw_unit/public/javascripts/screw.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Screw.Mock = {
confirmWith: function(val) {
Screw.Mock.confirmText = null;
iframeWindow.confirm = function(str) {
Screw.Mock.confirmText = str;
return val;
};
}
};
Screw.XHR = function() {};
Screw.XHR.returns = function(str, type) {
Screw.XHR.prototype.responseText = str;
Screw.XHR.prototype.headers = {"content-type": "application/json"};
if(type == "xml") Screw.XHR.prototype.responseXML = $(str).get();
};
Screw.XHR.prototype = {
abort: function() {},
getAllResponseHeaders: function() {},
getResponseHeader: function(header) {
return this.headers[header.toLowerCase()];
},
open: function(method, url, async, user, pass) {
Screw.XHR.url = url;
},
send: function(content) {
this.readyState = 4;
this.status = 200;
this.statusText = "OK";
},
setRequestHeader: function(label, value) {}
};

0 comments on commit 22badf2

Please sign in to comment.